One of the more powerful features of Apache is the ability to run multiple web servers on one machine. This functionality is accomplished using the VirtualHost functionality found within the httpd.conf file. There are two types of virtual hosts that can be configurednamed virtual hosts and IP virtual hosts. With named virtual hosts, you can host multiple TLDs on a single IP, while with IP virtual hosting, you can host only one virtual host per IP address. In this section, we will give examples of each, and list some common configuration options.
For those who have only one site to host or have multiple IPs for all sites they wish to run, IP-based virtual hosting is the best configuration choice. Consider the following example where the Virtual Brewery decides to host a web site for its Virtual Vineyard. The following is the minimum amount of configuration that would need to be added to the httpd.conf file in order to create the new web site.
Listen www.virtualvineyard.com>:80 . . <VirtualHost www.virtualvineyard.com> ServerAdmin webmaster@vbrew.com DocumentRoot /home/www/virtualvineyard.com ServerName www.virtualvineyard.com ErrorLog /var/www/logs/vvineyard.error_log TransferLog /var/www/logs/vvineyard.access_log </VirtualHost>
You would also want to make sure that www.virtualvinyard.com was added to your /etc/hosts file. This is done because Apache will need to look up an IP address for this domain when it starts. You can rely entirely on your DNS, but should your DNS server be unavailable for some reason when the web server restarts, your web server will fail. Alternately, you can hardcode the IP address of your server at the beginning of the configuration in the <VirtualHost> tag. Doing so may seem more efficient, however, should you wish to change your web server IP address, it will require changing your Apache configuration file.
In addition to the configuration options listed in the example, any of the options discussed earlier in the chapter can be added to the VirtualHost groups. This provides you with maximum flexibility for each of your separate web servers.
The configuration of name-based virtual hosting is very similar to the previous example, with the exception that multiple domains can be hosted on a single IP address. There are two caveats to this functionality. The firstperhaps the biggest drawbackis that SSL can be used only with a single IP address. This is not a problem with Apache, but rather with SSL and the way certificates work. The second potential drawback is that some older web browsers, such as those without the HTTP 1.1 specification, will not work. This is because name-based virtual hosting relies on the client to inform the server in the HTTP request header of the site they wish to visit. Nearly any browser released within the past few years, however, will have HTTP 1.1 implemented, so this isn't a problem for most administrators.
Proceeding to an example configuration, we will use the same example given earlier in the chapter, except this time the Virtual Brewery has only one public IP address. You will first need to inform Apache that you are using named virtual hosting, and then provide the detail on your sites as is shown in this example.
NameVirtualHost 172.16.0.199 <VirtualHost 172.16.0.199> ServerName www.vbrew.com DocumentRoot /home/www/vbrew.com </VirtualHost> <VirtualHost 172.16.0.199> ServerName www.virtualvineyard.com DocumentRoot /home/www/vvineyard.com </VirtualHost>
For the sake of clarity, the additional options were omitted, but any of the previously discussed options can be added as necessary.