Virtual Web Server Sandboxes on Ubuntu

Problem: Multiple virtual web sites are needed on an Ubuntu machine.

Solution: Set-up virtual servers in Apache and modify the /etc/hosts file so URLs of your choosing will resolve to the virtual sites that you create.

This procedure was tested using Ubuntu Hardy Heron.

For Apache’s documentation about name based virtual hosting, see:

Step One: Modify the hosts File

The /etc/hosts text file is owned by root and allows a simple means to resolve a URL that your type into your web browser to an IP number. In this case we want our example URL of to resolve to the IP address of the local machine, just like localhost. This is normally Edit the /etc/hosts file with:

sudo vi /etc/hosts

Find the line that reads: localhost

On the same line, after localhost, add a space and the name of your virtual site: localhost

That’s it for the hosts file. You do not need to restart anything.

Step Two: Set-up a Virtual Website on Apache

Apache has the handy ability to discern the URL from a browser’s address line and serve up the site that has been configured for it. This allows a server with a single IP address (like our to host several sites at once.

For each virtual web site on the server, create a text file in the /etc/apache2/sites-available directory. Use the following template to create the file (called in this example):

ServerAdmin username@localhost
DocumentRoot /home/username/public_html/
CustomLog /var/log/apache2/ combined

You will need to create this file as root, so:

sudo vi /etc/apache2/sites-available/

Now, you can use a Debian (Ubunto is a Debian variant) utility to easily create a link to this configuration file:

sudo a2ensite

The above line simply creates a link to the file in the /etc/apache2/sites-available directory. This allows you to enable and disable sites easily. The Debian disable counterpart to a2ensite is a2dissite (which just deletes the link).

Lastly, reload Apache:

sudo /etc/init.d/apache2 reload

After ensuring that you have something to sever in the site’s document directory, enter the URL you used in your browser’s address field and try it out!

Be Sociable, Share!