How to setup Virtual Hosts in Apache

This guide will explain what virtual hosts are and how to set them up using Apache on an Ubuntu Linux machine.

If you have one single IP address for your server and want to be able to host multiple websites you have a couple of options. You can either setup each website with sub-domains or you can setup virtual hosts using Apache and serve multiple domains from the same machine and a single web server. Most hosting companies that offer shared web hosting use virtual hosts for this. Virtual hosts are also quite useful when setting up a local web development environment on your computer. I’ll show you how to setup virtual hosts using Apache on an Ubuntu Linux machine.

In this example we will setup two virtual hosts on your local machine – and Both of these will point to different location in your filesystem and host different sites. Let’s begin by installing Apache and its support packages. From the command line, issue the following command:

# sudo apt-get install apache2-utils apache2-common

You might already have these packages installed. If you do, when you run the above command you’ll be told as much. Next, check to make sure that Apache is working fine. To do this launch a web browser on your machine and go to the url http://localhost. You should see a simple page informing you that you are running the Apache web server. If not, please check on the web for help on installing Apache.

There are a number of steps involved in this process. Here’s a breakdown of what we’re about to do. First, we’ll configure Apache with the two new virtual hosts that we want to setup. Then we’ll create the root directories for both of these new virtual hosts. After this we’ll add an entry into the system’s /etc/hosts file to point to the domains of these virtual hosts. Finally, we’ll create a simple file in each of these virtual host’s root directory to identify which domain it is associated with. Then we test.

With the web server installed and working we now move onto the configuration of the web server. Create a new file /etc/apache2/sites-available/vhosts.conf in your favorite text editor (mine’s vim). Add the following text:

<VirtualHost *:80>
DocumentRoot /var/www/

<VirtualHost *:80>
DocumentRoot /var/www/

Save the file. Now create the two directories mentioned in the config:

# sudo mkdir /var/www/
# sudo mkdir /var/www/

Add the two domains to your hosts file. Open the file /etc/hosts in a text editor and add the following line at the end:

Now activate the vhosts.conf config in Apache.

# sudo cd /etc/apache2/sites-enabled/
# ln -s ../sites-available/vhosts.conf

We’re almost done now. We just have to restart Apache:

# sudo /etc/init.d/apache2 restart

And add a couple of test files:

# echo “Hi I’m Calvin” > /var/www/
# echo “Hi I’m Hobbes” > /var/www/

Now keep your fingers crossed. It’s time to make the magic work. Launch your web browser and go to the url It should say “Hi I’m Calvin”. And then This should say “Hi I’m Hobbes”.

You should now have a working set of virtual hosts. You can add as many of these as you like. If you would like to run this on a remote machine you can replace the “*:80” in the vhosts.conf file with the IP address of the machine, such as “”

If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for

Home » Linux » How to setup Virtual Hosts in Apache

3 thoughts on “How to setup Virtual Hosts in Apache”

  1. Finally a tutorial that worked. I couldn’t have made it with the apache documentation site. Thanks.

  2. usually, you don’t create a single file /etc/apache2/sites-available/vhosts.conf with information about all vhosts, but you create a new file for each vhost (and link to it from …/sites-enabled), so that you can enable/disable them each independently. after all, that’s why …./sites-* are directories. also, there are some scripts with apache-magic like a2ensite which assume a file for each vhost

Leave a Comment

Your email address will not be published.