Monthly Archives: January 2007

Setting up multiple virtual servers on Apache for web development

I like to develop and test on my local machine and then to upload to the live website server when testing is completed. A little problem I’ve experienced is that my local Apache server only has one IP address and one website root. I guess this is not so much a “problem” but a typical condition.

With only one web server, I cannot test more than one website at a time unless I put each site in a sub directory in the web root. This actually works fine except that I am then forced to use relative path names in the files on the site, which can make for more convoluted logic in some cases and make it more difficult to share common code throughout the site.

In any case, I’d like multiple web servers to test with. I could use separate machines, but that would be a waste of resources and be awkward. It is possible to run multiple instances of Apache, but that would not solve name resolution and would not gain any benefits. I’d like the system to be logical and easily extensible so I can add more sites as required.

The solution I found is to keep a single IP address and a single Apache web server. I just need to resolve more than one URL to my single existing IP address and then have Apache (listening on port 80 as usual) have multiple virtual servers and take use the correct directory for a given web site name.

There are two steps to this solution. Name resolution and creating virtual servers.

Step One: Name Resolution

Usually name resolution is done by a DNS server. This server takes a URL like “” and returns an IP number (the Internet Protocol address is actually a number, but we humans find numbers more difficult to remember than text addresses). In addition to the DNS server, the operating system has a text file that can be used to resolve URLs.

I need my operating system (Linux in my case) to resolve URLs that I specify to my machine’s IP number. This is very easy. I just need to edit the file called “/etc/hosts” to add the names I want to resolve. In this file I found an entry for “localhost” which is standard name for the default Apache web server on a local machine. The entry was: localhost

As my machine came, typing “http://localhost” into a web browser would serve up documents in the standard web server on the machine. So I added a couple of lines to the hosts file so this part of it no read: localhost

I needed to be root to be able to edit and save the hosts file.

Step Two: Setting up a Virtual Server

I found that Apache will handle virtual servers easily. You just need to modify one setup file called “\etc\apache2\httpd.conf”.

I found the section for virtual hosts in httpd.conf and added a few more lines. I read about virtual hosting on the Apache site. I learned that the first virtual host that is specified is used by default in case the URL is not resolved by Apache. I wanted to use the existing directory in this case. I added the following lines to the httpd.conf file:

DocumentRoot /home/andrewault/public_html/

DocumentRoot /home/andrewault/public_html/

DocumentRoot /home/andrewault/public_html/

The first block of code makes the existing server the default virtual server. The next two blocks each add a new virtual server. Note that I use the target URL except that instead of “www” I use”my”. This allows be to keep the same name and keep my system organized.

Notes: You have to restart Apache for these changes to be effected. I just stopped and restarted the Apache daemon, but you could just restart the computer. Also, as with the hosts file, you need to be root to edit the httpd.conf file.


Apache Documentation for Name-based Virtural Servers