2010 March 01

Simple VirtualHost Configuration for Apache

Objective: To set up a test Apache installation that could serve pages from a folder that is located somewhere else on a drive (e.g. C:\MySite). Another restriction is that the existing "htdocs" folder has to be served when the server runs on port 80, and if the server runs from, say port 8080, it should serve pages from the repository.

The following are the steps to take :

1. Inform Apache about your special directory

In httpd-vhosts.conf file located in conf/extras :

Don't forget to uncomment the entry Include conf/extra/httpd-vhosts.conf from the httpd.conf file

The original path is found similar to the following :

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

Make a copy for your custom directory:

<Directory "C:/MySite">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

2. Add an additional VirtualHost name for the new port

First tell Apache that you have a new 'interesting' port:

NameVirtualHost *:80
# add the following line
NameVirtualHost *:8080

Now add the VirtualHost entry. The original directory configuration can be found similar to the following:

    
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.local.com
    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
    ServerName localhost
    ServerAlias www.test-website.local.com
    ErrorLog "logs/test-website.local.com-error.log"
    CustomLog "logs/test-website.local.com-access.log" common
</VirtualHost>

Simply make a copy and add your directory path:

<VirtualHost *:8080>
    ServerAdmin webmaster@dummy-host2.local.com
    DocumentRoot "C:\MySite"
    ServerName localhost
    ErrorLog "logs/dummy-host2.local.com-error.log"
    CustomLog "logs/dummy-host2.local.com-access.log" common
</VirtualHost> 

3. Startup Apache with new instructions

Use httpd -C "Listen 8080 to start Apache at port 8080. The contents from the newly configured directory should be seen now.