Host Multiple Websites on an Ubuntu 18.04 Server – Part II

In our previous article, we have configured two sample sites testing.com and example.com on ubuntu 18.04 server. You can check it at Host Multiple Websites on an Ubuntu 18.04 Server Part-I. At last, we have added sample web contents for each of the domains in the index.html file. Let’s see further steps to follow and complete this configuration.

Create the virtual hosts configuration files for our two sites

When Apache is first installed on Ubuntu 18.04 server, it creates a default virtual host file at /etc/apache2/sites-available/000-default.conf.

We need to copy that file and use it to configure our testing.com and example.com virtual hosts. To do this, run below
command on a terminal window:

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

Apache configuration files by default end with a “.config” file extension.

Once you have copied the files, open the first virtual hosts file with a nano editor to edit its content using below command:

$ sudo nano /etc/apache2/sites-available/testing.com.conf

Then you have to overwrite the values with the text below:

  1. <VirtualHost *:80>
  2. ServerAdmin admin@testing.com
  3. ServerName testing.com
  4. ServerAlias www.testing.com
  5. DocumentRoot /var/www/testing.com/public_html
  6. ErrorLog ${APACHE_LOG_DIR}/error.log
  7. CustomLog ${APACHE_LOG_DIR}/access.log combined
  8. </VirtualHost>

As you can see above, we have referenced the directory /var/www/testing.com/public_html as a document root because that will be the place where we will save our testing.com website’s files.

We need to follow the same procedure for our example.com virtual host

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

Then overwrite the files with the following contents:

  1. <VirtualHost *:80>
  2. ServerAdmin admin@example.com
  3. ServerName example.com
  4. ServerAlias www.example.com
  5. DocumentRoot /var/www/example.com/public_html
  6. ErrorLog ${APACHE_LOG_DIR}/error.log
  7. CustomLog ${APACHE_LOG_DIR}/access.log combined
  8. </VirtualHost>

Enable the two virtual hosts

We created two configuration files for our domains. Now, we need to enable them using the following commands:

$ sudo a2ensite testing.com.conf
$ sudo a2ensite example.com.conf

Restart Apache for the changes to take effect

Once you add a virtual host on your Ubuntu 18.04 server, you will need to restart apache service using below command :

$ sudo service apache2 restart

Edit the local hosts file on your computer

Your virtual hosts should be up and running without any issues. However because we used dummy values for testing purposes, we need to edit our local hosts file (on the local computer). This will allow both the domains to resolve to the correct public IP address of our Ubuntu 18.04 server. Assuming your public Ubuntu 18.04 server’s IP address is 111.111.111.111, you will have to add below entries on your local computer.

If you are running Linux then you need to edit the /etc/hosts file using the command below:

$ sudo nano /etc/hosts

Then add the below entries to the file and save.

  1. 111.111.111.111 example.com
  2. 111.111.111.111 testing.com

On other hand, if you are running Windows, you need to edit the file c:\windows\system32\drivers\etc\hosts using a text editor like notepad or wordPad and append the two entries above as shown below.

Make sure you have replace 111.111.111.111 with the real public IP address of your server.

Test your virtual hosts on your browser

Finally, you need to access example.com and testing.com from your browser and if you followed the steps correctly, you should see the content we created for the virtual hosts as shown below.

Example.com

example-Host Multiple Websites on an Ubuntu 18.04 Server

That’s all! We are done with the setup of these two websites on a single Ubuntu 18.04 server. Remember, you can replicate this idea to host an unlimited number of virtual hosts.

LEAVE A COMMENT

RELATED POSTS