11 June 2007 - 23:01[How to] host multiple offline websites with Apache

As an extension to my earlier tutorial, [How to] create an offline WAMP demo kiosk in Vista, I thought it would be worthwhile to discuss how to host more than one offline demonstration site on the same machine. These instructions will work on a Linux installation of Apache as well as our Windows installation from Apache2Triad.

This is useful if the extra sites you’re hosting have web-root-relative links (with leading slashes like "/files/document.html") that would otherwise point to http://localhost/files/document.html instead of http://localhost/site3/files/document.html, for example.

In this example, We’ll pretend you already have a site set up on localhost in your webroot. The easiest way to add new sites is with virtualhosts. To set up virtual hosts, we just need to add a bit to our httpd.conf file. Locate httpd.conf in your (apache2triad directory)/conf/ directory, and find the section for Virtualhosts. It should have a sample Virtualhost entry commented out for you already, but you need even less than that.

In your httpd.conf, add these lines:

NameVirtualHost *

<VirtualHost *>DocumentRoot C:/apache2triad/htdocsServerName localhost</Virtualhost>

<VirtualHost *>DocumentRoot C:/apache2triad/htdocs/site2/ServerName site2</Virtualhost>

<VirtualHost *>DocumentRoot C:/apache2triad/htdocs/site3/ServerName site3</Virtualhost>

…and add more blocks like this for any additional sites. Windows users, note the direction of the slashes! Don’t forget that you need to restart Apache any time you make a change to one of your conf files.

All you need to do now is add aliases to your hosts file for each virtual host you added.

In Windows, the hosts file is found in C:\Windows\system32\drivers\etc
Locate the line for 127.0.0.1 and add your “ServerName” aliases like this:

127.0.0.1    localhost    site2    site3

You can now access your sites by typing their ServerName aliases in your address bar. "site2" would take you to site2, for example, and all your root-relative links would work.

2 Comments | Tags: how to, internet, software, tech