How to Set Up Apache Virtual Hosts on Ubuntu 12.04


Virtual hosts are usually used for running one or more domain off a single IP address. People who want to run different sites off of a single virtual private server can take advantage of this. The site’s visitors cannot make out the difference as the site displays different information for its visitors. As a matter of fact, any number of virtual hosts can be added to a VPS.

This tutorial guides you in setting up virtual hosts on Ubuntu.


System Prerequisites

This tutorial would require root privileges for the user. Check this tutorial for the server set up.

Another requirement is that apache should already be up and running on your virtual server. In case if you don’t have, download it using the command:

sudo apt-get install apache2

Creating a New Directory

To start with the setting up of virtual host, we will have to create a directory to store the website’s information. You can create a directory using the -p option, which generates the parents of the new directory by itself.

sudo mkdir -p /var/www/

You can see that the location is actually the document root in the apache configuration file. I will use in this tutorial which you can replace with actual one.

Now, you will have to assign a DNS approved domain or an IP address to verify if the virtual host is working or not. You can also use an unapproved domain name for this verification process on your local system. I will be covering that later in this tutorial.


Granting Permissions to Directory


In this step, you will grant ownership of the newly created directory to the user. You can type in the following command:

sudo chown -R $USER:$USER /var/www/

Also, you need to make sure everyone have read privileges on the new files:

sudo chmod -R 755 /var/www

That’s it with permissions.

Create the Page

Now, let’s move on with creating a page. For that, we will create index.html from the configurations directory:

sudo nano /var/www/

Add the following text to the html file:

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

Save and exit the file.


Setting Up the New Virtual Host File

You need to set up the apache configuration. Let’s make a copy of the file and name it after the domain name in the same directory.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/

Open up the new configuration file:

sudo nano /etc/apache2/sites-available/

You need to set up a virtual host in this.

Insert a line for the ServerName under ServerAdmin as shown:


In case if you wish to make your site accessible with www from url, you can add alternate names in the virtual host file as ServerAlias. Your virtual host file should look similar to this;

<VirtualHost *:80>        
ServerAdmin [email protected]        

In the next step, you need to fill in the correct Document Root. You can type in the correct extension of the directory that we created earlier.

DocumentRoot /var/www/

Make sure you have entered it properly. If it’s incorrect or not present, then you will not be able to do the virtual host set up.

Once you are done with the changes, save and exit the file.

Now, let’s activate the host using the built in apache shortcut:

sudo a2ensite

Restart Apache

Now that the virtual host is set up with the configuration changes that we made earlier, let’s proceed to restart the apache. Restarting apache will bring in the changes into effect.

sudo service apache2 restart

You will be given a warning which will look like this:

Could not reliably determine the server's fully qualified domain name, using for ServerName

This warning message is fine and you will be able to access your virtual host now.


Setting Up of Local Hosts

This step is absolutely optional. If you have already pointed your domain name to your server’s IP address, then you don’t have to set up local hosts. Virtual hosts should work fine.

This step will show you how to set up local hosts on your computer without connecting to a domain name.

You will need your computer’s administrative password for this. Make sure you are not on your droplet, but your computer.

Here I’m considering a Mac or Linux. Access the root user on the computer and open up the hosts file:

nano /etc/hosts

Add the local host details to the file. It should look like this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##       localhost 
#Virtual Hosts


This will redirect your browser to and will provide you the virtual host details for corresponding IP. Later on, when you are done, you may delete those details to avoid further confusion.


Access Your Virtual Host

Once you are done with the set up for virtual hosts, you can verify it online by typing the IP address on your browser.


You will be taken to the page with the text from the html file that we created earlier.

Similarly, for setting up more virtual hosts, you just have to repeat the steps mentioned above. However, you need to be careful with the set up the new document root with proper domain name and creation of new virtual host files.

support2 has written 111 articles

Leave a Reply