How to set up multiple domain in one droplet or IP in Ubuntu server

Apache web server is one of the most powerful and flexible web servers and this the reason why it is highly popular. One of those features is to host multiple domains or sites on a single interface or IP by using matching mechanism. One cannot figure out that the same server is used for other domains also. Each configured domain then directs you to the specific directory which holds the site information.

Now, let’s go through the guide on how to set up multiple domains in one droplet or IP.

Process to set up multiple domains –

In your server, already there would a domain under /var/www/html directory. Now you want to add one more domain in that droplet. Let’s say the domain name is example.com

Now,

1. Make a directory for the new domain.

You have to make a directory in the server to hold the site data.

Open your Terminal and run the following code.

sudo mkdir -p /var/www/example.com/html

Go to /var/www  directory to check the newly created example.com folder.

2. Grant Permissions

sudo chown -R $USER:$USER /var/www/example.com/html

Also, modify the permission for the directory.

sudo chmod -R 755 /var/www/example.com

3. Create an index demo page

You can create a demo index.html page by using code or by directly uploading the index.html file in /var/www/example.com directory.

Now create a index.html file.

nano /var/www/example.com/html/index.html

In this file, create a simple code as mentioned below –

<html>
  <head>
    <title>Welcome to example.com!</title>
  </head>
  <body>
    <h1>Success!  The example.com virtual host is working!</h1>
  </body>
</html>

Then save and close the file.

4. Create New Virtual Host Config Files

Copy paste the following code and create a Virtual host file

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Now edit the file

sudo nano /etc/apache2/sites-available/example.com.conf

or directly go to /etc/apache2/sites-available to download and edit the file.

Paste the following code –

<VirtualHost *:80>
 ServerName example.com
 ServerAlias www.example.com
 #Redirect / http://www.example.com/
 ServerAdmin admin@example.com
 DocumentRoot /var/www/example.com/html

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

5. Enable the New Virtual Host Files

Now Enable the New Virtual Host Files.

sudo a2ensite example.com.conf

Check whether Apache is configured correctly or not by running the following code –

apachectl configtest

If Syntax OK restart the server by running the following code. If not then modify the error.

sudo service apache2 restart

Now your new domain is ready. Point your server IP to your new domain IP.  It takes some time to point the IP. Hit the URL after some time and find your new domain mapped under the same server.

Contact me if you have any queries or if you require any professional assistance related to your web world, I’d be happy to help.