Question

Hosting 3 Sites On the Same Droplet using subdomains

Hello members, can one host several projects for instance 3, on the same droplet using subdomains? I’m using Apache as web server. I would wish to serve three sites as follows:

sub_1.example.com sub_2.example.com sub_3.example.com

Your assistance will be highly appreciated.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
July 12, 2023
Accepted Answer

Hey @samunyi90,

Yes, you can definitely host multiple sites on the same DigitalOcean Droplet using subdomains. Apache allows this through a feature called VirtualHosts. A VirtualHost can be configured for each subdomain. Below are the general steps you would need to take:

  1. Create a Directory For Each Subdomain’s Content: Create a new directory in /var/www/ for each subdomain where you’ll keep the respective website’s files:
mkdir -p /var/www/sub_1.example.com/public_html
mkdir -p /var/www/sub_2.example.com/public_html
mkdir -p /var/www/sub_3.example.com/public_html

Make sure you set appropriate permissions for these directories.

  1. Create Apache Configuration Files: For each subdomain, you’ll need to create a new configuration file in /etc/apache2/sites-available/. For example:
sudo nano /etc/apache2/sites-available/sub_1.example.com.conf

Inside this file, you would put:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName sub_1.example.com
    DocumentRoot /var/www/sub_1.example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Do the same for sub_2 and sub_3, just replace sub_1 with sub_2 and sub_3 respectively in the above steps.

  1. Enable the New Virtual Host Files: Apache comes with a tool to enable and disable these site files. Use the a2ensite tool to enable each of the sites:
sudo a2ensite sub_1.example.com.conf
sudo a2ensite sub_2.example.com.conf
sudo a2ensite sub_3.example.com.conf
  1. Restart Apache: Once you have enabled the necessary configurations, you need to restart Apache to apply the changes:
sudo systemctl restart apache2
  1. Update DNS Settings: You’ll also need to create DNS A records for each subdomain that points to your droplet’s IP address. This will be done through your DNS provider (where you bought your domain).

That’s it! You now have three sites being served from the same Droplet, each from their own subdomain. If you have SSL certificates, you can configure each VirtualHost to also listen on port 443 and point to the respective certificates. This setup can be repeated for any number of sites you need to host.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more