Skip to Content

How to Setup Apache Virtual Hosts on Ubuntu 16.04 LTS

Apache Virtual Hosts allow you to host more than one website on a single server or machine. In this tutorial, you will learn how to create a Virtual Host on Ubuntu 16.04.

This tutorial assumes you have already installed Apache on a Ubuntu 16.04 Desktop or Server. For more information on setting up Apache on Ubuntu, read the following article: How to Setup a WordPress Development Environment on an Ubuntu Desktop Machine.

With Apache installed, connect to the Ubuntu machine via SSH or open a Terminal session (if using Ubuntu Desktop) and then follow the steps below to setup a new Virtual Host.

Step 1: Create Directory

The first thing we need to do is create a directory to store all our web sites files.

Assuming the domain name for our web site is example.com, let’s create a directory called example.com in /var/www.

sudo mkdir /var/www/example.com

Step 2: Create Test HTML File

Create a HTML file called index.html in the root of the directory we just created in the previous step and add the following HTML.

sudo vim /var/www/example.com/index.html

Add the following code to the file and then save it.


example.com
example.com





Step 3: Create the Virtual Hosts File

Apache comes with a default Virtual Host file called 000-default.conf that we can use as a template for new Virtual Hosts. This is the default virtual hosts that acts as a catch all for domains that are pointed to the server but are not assigned to a virtual host.

Copy the default Virtual Host with the following command:

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

Open the virtual host file:Advertisements

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

The file should look like the following:


 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

As you can see by the DocumentRoot directive, the files are served from /var/www/html. We need to change this to the directory we created in the first step. For example:

DocumentRoot /var/www/example.com

We also need to add a new directive that maps the example.com domain to this virtual host. This is done with the ServerName and ServerAlias directives. For example:

ServerName example.com
ServerAlias www.example.com

The final file should look like the following:


 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/example.com
 ServerName example.com
 ServerAlias www.example.com
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close the file.

Step 4: Enable the Virtual Host

Enable the virtual host using the a2ensite tool:

sudo a2ensite example.com.conf

Apply the changes to Apache

sudo service apache2 reload

Step 5: Test the Site

Note: The example above shows the site being accessed from the same machine (Ubuntu Desktop). In order to point the example.com domain to the local machine. You can edit the hosts file (/etc/hosts) so that it has the following line: 127.0.0.1 example.com

Conclusion

In this post we learned how to create an Apache Virtual Host on a Ubuntu machine so that you can run multiple websites. We did this by copying the default virtual host file and making the necessary changes to host our domain name. You can repeat this process to host as many sites as the server can handle. For more information on what Apache configuration options are available visit the Apache Virtual Host documentation.