Hosting multiple domains on Windows with Apache HTTP Server and the VirtualHost directive

This post is especially helpful for programmers who are building several Web sites with PHP, and need to replicate multiple domains using a single instance of Apache HTTP Server. Although most of this tutorial is applicable to Mac OS X and Linux, it is intended for Windows XP. The primary difference would be the method in which HTTP Server’s configuration file handles directory paths on Windows.

You will find that each Web site has needs that are specific to the real domain where they will be hosted. It becomes increasingly important as you begin to develop applications on your local environment, that the directory structure and permissions match these domains accurately. Even if your application is hosted on a Linux machine, your local Windows application should have a similar architecture.

Simply placing several folders for each Web site under the Apache HTTP Server document root will not suffice. Log files, the cgi-bin and other data files should not be indexed, and need to be reachable only by the server or your scripts. Many applications like Wordpress are even dependent on where the application installation takes place in relationship to the domain’s root folder.

A simple way to overcome these roadblocks is to use the VirtualHost directive, which is defined in Apache HTTP Server’s configuration file. The configuration file can be found in C:\your-apache-installation-path\conf\ and will be named httpd.conf. For the purpose of this tutorial, it might be good to note that I am using Apache HTTP Server version 2.2.8.

Before you make edits to the configuration file though, setup the location and directory structure for your virtual Web sites. This location should NOT be under the default document root location for HTTP Server, which is typically C:\your-apache-installation-path\htdocs\. Instead, you should put these Web sites under C:\apache_www\ (or a name of your choosing), and each immediate sub-folder will then represent a single domain. As a best practice, each folder name should be the name of the virtual domain to which it coincides.

Below is a screenshot of Windows Explorer, with all directories expanded to show the sub-folders underneath. In order to better illustrate a “live” architecture, I have chosen the Web site domain names myapp.com and myblog.com. These names are arbitrary, and do not necessarily need to end with a domain extension. I could have just as easily named them thing1 and thing2.

Place a single index.html in each www folder, which will be publicly viewable when you type the domain into the browser address bar. I suggest putting different text into the source of each index.html, so that you know the domain is properly pulling content from the correct folder.

One thing to note regarding your virtual domain names, is that they should not be identical to your real domains, or existing domains that you visit. This is because it is necessary to point these virtual domains to your localhost IP, which means they will mirror the content in your document roots. If the virtual domains were identical to real domains, then hitting the URL for the domain would pull content from your running instance of Apache HTTP Server, and not the content from the server where they would otherwise be hosted.

To point these virtual domains you need to first edit the Windows hosts file. You can find the Windows hosts file at C:\WINDOWS\system32\drivers\etc, and you can open it with Notepad or your favorite text editor. The hosts file is comparable to a Domain Name Server (DNS), in that it provides a domain name to IP mapping on computer networks. The screenshot below illustrates how our domains map to the localhost IP, which is 127.0.0.1. Make these changes, and save the file.

Now that the directory structure is complete, and you have mapped the domain names to your localhost IP, it is time to modify HTTP Server’s configuration file. As noted earlier, the configuration file can be found in C:\your-apache-installation-path\conf\ and will be named httpd.conf. Open the configuration file in a text editor, and copy-and-paste the text below at the bottom of the file.

httpd.conf
# Begin listening for virtual hosts.

NameVirtualHost *:80

# Begin virtual host directives.

<VirtualHost *:80>

# myblog.com virtual host.

ServerAdmin webmaster@myblog.com
DocumentRoot "c:/apache_www/myblog.com/www"
ServerName myblog.com
ServerAlias *.myblog.com
ErrorLog "c:/apache_www/myblog.com/logs/log"
ScriptAlias /cgi-bin/ "c:/apache_www/myblog.com/cgi-bin/"

<Directory "c:/apache_www/myblog.com/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

</VirtualHost>

<VirtualHost *:80>

# myapp.com virtual host.

ServerAdmin webmaster@myapp.com
DocumentRoot "c:/apache_www/myapp.com/www"
ServerName myapp.com
ServerAlias *.myapp.com
ErrorLog "c:/apache_www/myapp.com/logs/log"
ScriptAlias /cgi-bin/ "c:/apache_www/myapp.com/cgi-bin/"

<Directory "c:/apache_www/myapp.com/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

</VirtualHost>

For these settings to take affect, you will need to stop and start HTTP Server. How to do this will depend on whether or not you installed the server as a service. If you have an Apache Service Monitor in the Task Manger bar, then you should be able to double-click the icon. This will bring up the monitor, and you can then stop and start the service. Type each of the virtual domains you created into the browser address bar, and you should see the text from the index.html for that domain.

The best place to read about the VirtualHost and Directory directives that you pasted into the configuration file is the Apache HTTP Server documentation. The most important consideration with virtually hosted domains is typically the Directory security settings. You want the www to be viewable by the public, but log files and the cgi-bin should only be available to your scripts. The official documentation discusses some of these security concerns.

Leave a Comment

Comments are moderated. No profanity. Only <a>...</a>, <blockquote>...</blockquote>, and <code>...</code> are allowed.

Seperate paragraphs by pressing the "Enter" key twice, or press it once to break to a new line.

1 Comment

#01, May 12 2008

Abhisek

Excellent! Can’t wait to try out!