How to setup Virtual Hosts in Apache

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. 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 “”

  • youam

    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

  • Divas virtuālās uzņēmējas sadaļas? Jums ir divas atsevišķas bloki virtuālā saimniekiem?
    I apskatīja dokumentācija atkal, ka jūs norādīja uz mani, un es nedomāju
    redzēt, ka jebkur. Man bija iespaids, jums vajadzēja vienu bloķēt, lai
    katru virtualhost. Nedomāju, ka es saprotu to, ko jūs domājāt. Man bija tikai viena
    virtualhost bloku, bet kopš es izlasīju šo e-pastu, man ir citā tikai
    jautri, bet kaut kā es nedomāju, ka’s what you mean.

  • doe

    Thanks, this is the simplest and easiest to understand tutorial i have found on the subject

  • Teo

    Ciao a tutti, ho letto questa guida a mi ha lasciato un po’ perplesso…Youam, posso chiedere direttamente a te ? Anche io avrei 2 siti da configurare su un unico ip, ma non vanno…non c’è verso !!! Mi aiuti ?


  • Pingback: hosts | hong0050()

  • Pingback: setup virtual hosts « Super Web Hosting Co. Inc.()

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

  • Pingback: apache2 virtualhosts()