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 – calvin.dev and hobbes.dev. 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:
Save the file. Now create the two directories mentioned in the config:
# sudo mkdir /var/www/calvin.dev
# sudo mkdir /var/www/hobbes.dev
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/calvin.dev/index.html
# echo “Hi I’m Hobbes” > /var/www/hobbes.dev/index.html
Now keep your fingers crossed. It’s time to make the magic work. Launch your web browser and go to the url http://calvin.dev. It should say “Hi I’m Calvin”. And then http://hobbes.dev. 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 “10.2.10.6:80”