Setting Up Multiple Apache Local Web Sites On Your Computer

If you do web development on WAMP or LAMP, but not really adept in general network configurations, your typical local development web content might have URLs similar to as follows:

  • http://localhost/projecta
  • http://localhost/projectb
  • http://localhost/projectc
  • http://localhost/projectc-beta2

Wouldn’t it be nicer if those projects have URLs such as:

  • http://projecta
  • http://projectb
  • http://projectc
  • http://projectc-beta2

Continue reading on if you’re interested to find out how this can be achieved on both Windows and Linux computers. This writeup will cover setting up the hosts as well as configuring Apache (versions 1.3.x) to support the hosts.

This tutorial is designed for those who have administrative priviledges on the development computer (under Administrators group on Windows, or have root access for Linux). More often than not, if you’re not using a shared computer, you’ll most likely have such access.

Setting Up the Fake Hostnames

A Primer on Hostnames: Hostnames are names for computers connected on a network. For HTNet, its hostname is www.heritage-tech.net (and also heritage-tech.net). For the nitty-gritty details, please refer to Hostname on Wikipedia.

The fake hostnames such as projecta, projectb, projectc and projectc-beta2 can be set in either of these hosts configuration files (also referred to as hosts files) depending on your operating system:

  • For Windows: C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS (assuming your Windows installation is in C:\WINDOWS).
  • For Linux: /etc/hosts

If you were to open these files in a text editor, you’ll see content similar to the following:

127.0.0.1        localhost
192.168.1.18     testhost.testdomain.com testhost

All hosts files will have the localhost entry which resolves to 127.0.0.1, also known as the loopback address. Basically, what the first line in the host file illustrated above tells us is to route all network packets addressed to localhost to the computer you’re using.

So to get our fake hostnames working, they each should have an entry resolving to 127.0.0.1 in your hosts file. Example as follows:

127.0.0.1        localhost
127.0.0.1        projecta
127.0.0.1        projectb
127.0.0.1        projectc
127.0.0.1        projectc-beta2
192.168.1.18     testhost.testdomain.com testhost

To test the configuration, you should ping the fake hosts and if you had followed the steps above correctly, you should get a response from 127.0.0.1 for the projectaprojectbprojectcand projectc-beta2 hosts. This concludes the host resolving section of this tutorial.

Configuring Apache

The next step involves configuring Apache to handle requests to the hosts defined above. To do this, you’ll need to edit your Apache installation’s configuration file; httpd.conf. On a default Apache for Windows installation, this file will be in C:\Program Files\Apache Group\Apache\conf\. For Linux, the location of the httpd.conf file usually depends on the distribution. On Slackware Linux, it’s in /etc/apache/.

Open the httpd.conf file in your favourite text editor and look for a line that starts with DocumentRoot. This line sets the location for the files that will be served as your default web site.

On Linux, it will look something like the following (Note that the actual location may differ on your setup):

DocumentRoot "/var/www/htdocs"

Whereas on Windows, it will be similar to (Note that the actual location may differ on your setup):

DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"

Also note that for Windows, Apache can also use a normal slash (/) instead of the typical backslash (\) in the directory path. You should also enclose directory paths in quotes. From now on, we’ll refer to these directories as DocumentRoot.

If the directories where you want to serve content for the hosts created earlier reside within the DocumentRootdirectory, you merely need to define a VirtualHost declaration for each of them. Let’s assume that the files for http://projecta will be in the directory named projecta under the DocumentRoot directory (which will be /var/www/htdocs/projecta/ for Linux or C:\Program Files\Apache Group\Apache2\htdocs\projecta\ for Windows).

Therefore, at the end of your httpd.conf file, append the following lines to create the projecta virtual host:

For Linux:

<VirtualHost *>
DocumentRoot /var/www/htdocs/projecta
ServerName slackbox
</VirtualHost>

For Windows:

<VirtualHost *>
DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs/projecta"
ServerName slackbox
</VirtualHost>

If the files are served from a directory outside of the DocumentRoot, you need to create a Directory declaration for each of the directories in addition to the VirtualHost declaration.

For example, let’s assume that files from projectb will be served from /web/projectb (for Linux) or C:\web\projectb (for Windows). Therefore, the following lines should be appended to your httpd.conf file:

For Linux:

<Directory "/web/projectb">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

<VirtualHost *>
DocumentRoot /web/projectb
ServerName projectb
</VirtualHost>

For Windows:

<Directory "C:\web\projectb">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

<VirtualHost *>
DocumentRoot "C:\web\projectb"
ServerName projectb
</VirtualHost>

Testing the Setup

Once all of the above is done, you can now test your configuration. However, you need to restart the Apache service in order to apply the changes made above. Here’s how to do it if you’ve installed Apache as a service (9 times out of time, this is how it’s normally installed):

For Linux (distros using BSD-style init like Slackware):

/etc/rc.d/rc.httpd restart

For Linux (distros using sysv-style init like Red Hat):

service httpd restart

For Windows:

  1. Run: services.msc
  2. Look for Apache in the services list and select it
  3. Click the Restart Service button

If you’ve followed through this guide closely, you should now have a working Apache installation with multiple named local sites.

Was this guide useful for you? Please digg it or drop me a comment or two.

Update: This guide is for Apache 1.3.x. To get local VirtualHosts to work with Apache 2.0.x, please click here.

Leave a Reply