Example Virtual Host Apache Configuration

by Stephen Fluin 2010.07.14

Someone asked for an example of Apache Virtual Hosts configuration. All of my virtual hosts have the same format as the following two examples:

<VirtualHost *:80> DocumentRoot "/var/www/smp/public" ServerName stonedmonkeyproductions.com ServerAlias stonedmonkeyproductions.com *.stonedmonkeyproductions.com ErrorLog /var/www/logs/stonedmonkeyproductions-error_log CustomLog /var/www/logs/stonedmonkeyproductions-access_log combined </VirtualHost> <VirtualHost *:80> DocumentRoot "/var/www/ta/public" ServerName trollattack.com ServerAlias trollattack.com *.trollattack.com ErrorLog /var/www/logs/trollattack-error_log CustomLog /var/www/logs/trollattack-access_log combined </VirtualHost>

Types of Virtual Host Configurations

Name Based Virtual Hosts

There are two main types of virtual host configuration. The first called "Name Based" virtual hosting matches the examples above. In this case, the domain name provided as part of the request determines the site that apache matches. Name based hosting does not work with SSL because Apache doesn't know the domain name requested until AFTER the encryption negotiation is complete.

Address Based Virtual Hosts

The second type of virtual host is address based hosting. This will only be used when the server has many IPs. The server uses the interface (server ip or port) of the incoming request to determine the site to respond with. This is almost always used to establish SSL hosting in combination with name based hosting.

Example address based virtual hosts:

<VirtualHost 111.22.33.44> ServerName www.customer-1.com DocumentRoot /www/hosts/www.customer-1.com/docs ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin </VirtualHost> <VirtualHost 111.22.33.44> ServerName www.customer-2.com DocumentRoot /www/hosts/www.customer-2.com/docs ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin </VirtualHost>

Dynamic Virtual Hosts

The third type (which is new to me) is mass or dynamically configured hosts. These hosts use the IP or Domain name to determine the DocumentRoot of the site. Read the Apache spec here. Example of what would go in your httpd.conf below:

UseCanonicalName Off VirtualDocumentRoot /usr/local/apache/vhosts/%0

The %0 when used with VirtualDocumentRoot replaces %0 with the domain name.


permalink