You can use Apache to serve Web pages for a single site, such as http://www.threeroomco.com , as described earlier in this chapter. Suppose, though, that you want to host two or more sites on one computer. Is this possible? Yes! In fact, Web hosting services use this feature extensively, to host dozens, hundreds, or more Web sites from a single Web server. The bulk of the configuration for handling these virtual domains is the same as for a simpler Apache configuration, but there are some special details to which you must attend .
Why Use a Virtual Domain?
A virtual domain allows a Web server to respond differently depending upon the name with which it's addressed. (This requires you to point several different hostnames to the Web server computer in DNS records for one or more domains.) There are several possible uses for such a configuration, including the following:
As a general rule, the capacity of Apache to handle domain hosting tasks is certainly most useful to large organizations and ISPs that might want or need to handle multiple domains. Smaller-scale uses can certainly be important for those who happen to be in appropriate situations, though.
Virtual Domain Configuration Options
There are two common methods of handling virtual domain hosting. One assigns different document root directories based on the requested domain name, and the other allows you to set up a collection of (possibly differing) options for each virtual domain.
One key virtual domain configuration option is VirtualDocumentRoot . This directive lets you specify a directory name to be used as the root of a request to the server, dependent upon the name used. To that end, you can include several variables in the directory name you give to VirtualDocumentRoot , as outlined in Table 20.1.
For instance, consider the following directive:
This results in the server using subdirectories of /home/httpd named after the entire hostname by which the server is referenced. If a call comes in for http://www.threeroomco.com/index.html , the server looks for a file called /home/httpd/www.threeroomco.com/index.html . This approach is very useful if you want to host many sites, although it can create a large number of subdirectories directly under the base directory ( /home/httpd in this example). If you know you'll be serving domains of a given length, you might prefer something that further breaks up the directory structure, like this:
Table 20.1. Variables Used to Specify Hostname-Specific Home Directories
In a configuration like this, a call to retrieve http://www.threeroomco.com/index.html causes Apache to look for /home/httpd/com/threeroomco/index.html . Alternatively, you might want to alphabetize your virtual site directories with a configuration like this:
With this configuration, a URL of http://www.threeroomco.com/index.html causes Apache to return /home/httpd/t/www.threeroomco.com/index.html , if it exists. The %-2.1 variable returns the first ( .1 ) character of the next-to-last ( -2 ) component of the hostname, so the directories are stored in subdirectories by their domain names' first letters .
No matter what your precise VirtualDocumentRoot directive, you should set UseCanonicalName to Off , thus:
When UseCanonicalName is set to On , as is the default with most configurations, Apache uses what it believes its hostname to be when doing relative references within Web sites. For instance, if the index.html page includes a call to load products.html , Apache will try to retrieve products.html from its canonical name. With a virtual domain, this lookup will fail. Setting UseCanonicalName to Off changes Apache's behavior to use the hostname associated with the virtual domain instead.
Another approach to virtual hosting is to explicitly define each virtual host. You do this by using two special directives:
Two directives that you're likely to include within the <VirtualHosts> block are ServerName (to set the name to which the block applies) and DocumentRoot . You can also customize other features, such as enabling CGI. As an example, consider the following lines, which set up a server to handle two virtual Web sites:
NameVirtualHost * <VirtualHost *> ServerName www.threeroomco.com DocumentRoot /home/httpd/threeroomco/html ScriptAlias /cgi-bin/ "/home/httpd/threeroomco/cgi-bin/" </VirtualHost> <VirtualHost *> ServerName www.pangaea.edu DocumentRoot /home/httpd/pangaea-u/html </VirtualHost>
When a server so configured is accessed via the www.threeroomco.com name, it serves files stored in /home/httpd/threeroomco/html as static files, and enables CGI scripts in /home/httpd/threeroomco/cgi-bin . When the server receives a request addressed to www.pangaea.edu , on the other hand, it serves the files from /home/httpd/pangaea-u/html . This latter configuration doesn't support CGI scripts. If a request comes in using another name or the raw IP address, the first configuration takes precedence, so such requests go through the www.threeroomco.com configuration in this example.
Compared to VirtualDocumentRoot , <VirtualHost> configurations make it easy to customize virtual hosts in unique ways, or to place their files in arbitrary directories. The advantage of the VirtualDocumentRoot approach is that you don't need to reconfigure the server to add a new domain; you need only create a new directory. In practice, most sites use the <VirtualHost> approach, but you can pick whichever is more appropriate for your purposes.