Setup Apache Virtual Hosts on UBUNTU 14.04 LTS


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 very powerful and flexible. Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.

These allow the admin 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.


Before you start, you need to create non-root as explained in step 1 to 4 here.

You will need to have Apache installed server. If you have not, you need to use following command to install apache on server.

sudo apt-get update
sudo apt-get install apache2


Step 1.  — Create the Directory Structure

Document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory.

Within root directory, create public_html folder for site related files.

To create directory, user following commands

sudo mkdir -p /var/www/


Step 2. — Access Permissions
sudo chown -R $USER:$USER /var/www/

The $USER variable will take the value of the user you are currently logged in.

sudo chmod -R 755 /var/www


Step 3— Create IndexPage for Each Virtual Host
nano /var/www/


    <title>Welcome to!</title>
    <h1>Success!  The virtual host is working!</h1>


Step 4 — Create Virtual Host File

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.
Create the Virtual Host File

Copy conf file

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

Edit file in editor

sudo nano /etc/apache2/sites-available/

The file look like,

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.


After this, we need to add two directives ( i.e. ServerNameServerAlias )


We need to change virtual host file is the location of the document root for domain.

DocumentRoot /var/www/

Virtual host file should look like this:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

save and close the file.


Step 5 — Enable the New Virtual Host Files

Use the a2ensite tool to enable the site.

sudo a2ensite

When you are finished, you need to restart Apache to make these changes take effect:

sudo service apache2 restart

You will most likely receive a message  something like following :

* Restarting web server apache2
 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using Set the 'ServerName' directive globally to suppress this messag

This is a harmless message that does not affect our site.

Step 6 — Set Up Local Hosts File
sudo nano /etc/hosts

Add ip as per domain name   localhost   guest-desktop

This will direct any requests for on our computer and send them to our server at

save and close file.

Step 7— Test virtual host