Virtual Hosting With Apache

Overview

From the Apache 2.0 documentation:

The term Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Since most of our customers are running systems with only a single IP address we'll be dealing only with the configuring name based virtual hosts.

This document outlines how to host multiple web sites using an HTTPd server, a single dynamic IP address, and multiple hostnames in our Dynamic DNS or Custom DNS services. Specific info is being provided for Apache 1.3 and Apache 2.0.

Configuring in Apache 1.3/2.0

  1. The first thing to do is to download, compile (if necessary), and install the latest version of Apache if you haven't already. Obviously if your OS comes with a pre-built version of Apache you can probably stick with this even if it is a version or two out of date.

    Specific help with the installation of Apache is outside the scope of this document, please consult the accompanying documentation with your Apache distribution for assistance.

  2. Open the file httpd.conf in your favorite text editor. On Unix systems, this file can usually be found in one of

    	/etc/httpd/
    	/usr/local/etc/httpd/
    	/usr/local/apache/conf
    

    On Windows systems, this file can typically be found in

    	C:\Program Files\Apache Group\Apache\conf
    
  3. Apache 1.3 only: The first thing to look for is the line that says:

    	#BindAddress *
    

    You should uncomment this line (remove the # at the beginning), so that it reads:

    	BindAddress *
    

    Note: The BindAddress directive has been removed in Apache 2.0 along with the Port directive. The Listen directive is now used for this purpose.

  4. Next, locate and uncomment the line that says:

    	Listen 80
    

    Note: In this configuration Apache will listen on all bound IP addresses. The is the default desired behavior when running Apache in an environment where the IP address is dynamic and/or you have only a single network interface.

    Note also: If you are forced to use an alternate port because your ISP blocks port 80, this is where you define that alternate port in Apache.

  5. We now have to set up the actual Virtual Hosts. Locate the line that says:

    	#NameVirtualHost *
    

    Uncomment this line so that it reads:

    	NameVirtualHost *
    

    This will enable name-based Virtual Hosts.

  6. Now we can set up the Virtual Host settings for each individual hosts. The examples provided below are for the hostnames test1.dyndns.org and test2.dyndns.org. Replace these with your hostname(s). Two examples are provided, but this step can be extrapolated for as many hostnames as required. Insert the following code into your httpd.conf file:
    	<VirtualHost *>
    		ServerName test1.dyndns.org
    		DocumentRoot /path/to/test1/files
    	</VirtualHost>
    
    	<VirtualHost *>
    		ServerName test2.dyndns.org
    		DocumentRoot /path/to/test2/files
    	</VirtualHost>
    

    Optionally, you can also add the line:

    	ServerAlias *.test1.dyndns.org
    

    beneath the ServerName line - this is for users using the wildcard feature.

  7. Start Apache, it should work like a charm.
  8. Read more. Further reading on virtual hosts in Apache can be found in the Apache 2.0 documentation. Of particular usefulness is page which provides examples of different kinds of virtual host setups. While most customers won't need more than the above, reviewing this page from the Apache docs may give you a important insights into how virtual hosts work.