Setup Apache Virtual Host

Virtual Hosts are used to run more than one domain from a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.

In previous tutorial we’ve learned how to pointing your domain to the VPS you owned. But if you’ve multiple domain to host on your VPS, pointing domain to the VPS still haven’t solved the problem since each domain will be had the same page. In this case Apache Web Server and Virtual Host will be the answer.

The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.

Apache breaks its functionality and components into each unit that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.

These designations allow the administrator to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than one site off of a single VPS.

Each domain that is configured will direct the visitor to a specific directory holding that site’s information, never indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

In this guide, we will walk you through how to set up Apache virtual hosts on an Ubuntu VPS. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting.

Step One – Create a New Directory

Make sure your apache is up and running with this command

sudo service apache2 start

The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. This location will be your Document Root in the Apache virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.

sudo mkdir -p /var/www/html/

Step Two – Grant Permissions

We need to grant ownership of the directory to the user, instead of just keeping it on the root system.

sudo chown -R www-data:www-data /var/www/html/

the www-data:www-data is default user:group for apache2 web server.
Additionally, it is important to make sure that everyone will be able to read our new files.

sudo chmod -R 755 /var/www

Now you are all done with permissions.

Step Three – Create The Home Page

We have our directory structure in place. Let’s create some content to serve.

We’re just going for a demonstration, so our pages will be very simple. We’re just going to make an index.html page for the site.

We can open up an index.html file in our editor by typing:

sudo nano /var/www/html/

In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:

    <h1>Success: You Have Set Up a Virtual Host</h1>

Hit CTRL + X then Y to save and quit editing the file.

Step Four – Create the New Virtual Host File

The next step is to set up the apache configuration. We’re going to work off a duplicate—go ahead and make a copy of the file (naming it after your domain name) in the same directory:

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

Open up the new config file:

sudo nano /etc/apache2/sites-available/

The file will look something like this (I’ve removed the comments here to make the file more approachable):

DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log

As you can see, there’s not much here, each of its line functionalities are:

  • ServerAdmin, directive to an email that the site administrator can receive emails through.
  • ServerName, establishes the base domain that should match for this virtual host definition. This will most likely be your domain.
  • ServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www
  • DocumentRoot, directive pointing to the directory we created.

Save and exit the file with CTRL + X then Y.

Step Five – Enable the Site

We’ve made a lot of the changes to the configuration, and the virtual host is set up. However none of the changes that we made will take effect yet. We need to enable the site in apache2 then restart apache2

sudo a2ensite
sudo service apache2 reload

Step Six – (Optional) Remove Default Site

If you’re using your main domain which where your reverse DNS pointing to, you need to disable the 000-default.conf site which are enabled by default on apache2. To do so use this code below.

sudo a2dissite 000-default.conf
sudo service apache2 reload


All Done! You can go to visit your domain site and see the success html we created there :D. To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the proper domain name, and then creating and activating the new virtual host file.