How to set up Apache Virtual Hosts on Ubuntu 16.04

How to set up Apache Virtual Hosts on Ubuntu 16.04

Spread the love

Apache is a free and open source web server, it is the most popular web server in the world, and is commonly used in Linux servers. It is developed and maintained by Apache Software Foundation, over half of all servers around the world are running this fast and secure web server. In this tutorial, we will show you how to set up Apache virtual hosts on Ubuntu 16.04, to host as many websites as you want, the limit is your server’s resources like RAM, disk space, etc.

What is virtual host?

Virtual host is used to host a number of websites on the same web server. The web server could be Apache, nginx, Litespeed, etc. Apache supports two types of virtual host:

Name based virtual hosts
IP based virtual hosts

The name based virtual host is commonly used to host multiple websites on the same server, whereas in IP based virtual host we can only configure one website on one IP address. In this tutorial, we will show you how to create name based virtual hosts. We will host and on a server, please replace them with your actual domain names.

How to create virtual host

Let’s SSH in to the server and proceed to the next step.

ssh root@Your_IP_Address -p7022

Create directory for domains’ webroot

mkdir -p /var/www/html/{,}/{public_html,logs}
set up Apache Virtual Hosts on Ubuntu 16.04

The command above will create four directories, two of them will be our domains’ document root, and the other tw will be used to store our apache log files:


Create test pages

Now, let’s create a test page for

nano /var/www/html/

<center><h1>This is!</h1></center>


Save and exit nano with Ctrl + O then Ctrl + X

Let’s copy the index test page to

cp /var/www/html/ /var/www/html/

Okay, the index test page has been copied, but we need to edit the index page for

sed -i 's/' /var/www/html/

We have successfully created an index test page for both domain names. Let’s give permission to user www-data, the default user who runs the apache.

chown -R www-data: /var/www/html/
Apache Virtual Hosts on Ubuntu 16.04

Create a virtual host

Now, let’s create a virtual host configuration file for The virtual host configuration file should be ended with .conf extension

nano /etc/apache2/sites-available/
DocumentRoot /var/www/html/

ErrorLog /var/www/html/
CustomLog /var/www/html/ combined

The virtual host configuration file for has been created, now we only need to copy and edit it for

cp /etc/apache2/sites-available/ /etc/apache2/sites-available/
sed -i 's/' /etc/apache2/sites-available/

We will give you a short explanation about the content of virtual host configuration file.

<VirtualHost *:80>
The virtual host is listening on port 80


The ServerAdmin sets the contact address that the server includes in any error messages it returns to the client. You can specify your email address here, or even remove the line.


This directive is needed, it tells Apache that Apache will process the domain request. Apache will search for virtual host files and process all request if domain/subdomain match with the one configured on ServerName or ServerAlias directive.

DocumentRoot /var/www/html/

This directive is also important, we need to tell Apache where the document root of is. If we don’t specify it, apache will load the default document root, which is in /var/www/html

ErrorLog and CustomLog are the directives to save the log of a domain name hosted on the server.

Enable virtual host

In Ubuntu, we can simply issue the following commands to enable the virtual hosts


Setting up Apache Virtual Hosts on Ubuntu 16.04






Those commands will enable the virtual hosts, but we also need to reload/restart Apache after enabling/disabling the virtual host. The command actually creates a symbolic link in /etc/apache2/sites-enabled. You can also run the following commands instead of invoking the ‘a2ensite’ command:

ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/
ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

Now, restart apache to activate the new configuration.

systemctl restart apache2

That is all, we successfully created two Apache virtual hosts for the two domains. You can now open your web browser and navigate to the domain name of the newly created virtual hosts.

How to Setup Apache Virtual Hosts on Ubuntu 16.04

How to do I set up Apache Virtual Hosts on Ubuntu 16.04


How to set up Apache Virtual Hosts on Ubuntu 16.04Of course, you don’t need to set up Apache Virtual Hosts on Ubuntu 16.04, if you use one of our Linux Server Management Services – in which case, our technical support team will help you creating the virtual hosts immediately. They are available 24/7, and can cater to any questions or requests.

PS. If you liked this post on How to set up Apache Virtual Hosts on Ubuntu 16.04, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *