Defining Virtual Hosts

Virtual hosts allow a server to host multiple websites known by different hostnames. The Red Hat configuration has an entire section dedicated to virtual hosts, but it is all commented out. It is there only to serve as an example. To use virtual hosts, you must first uncomment the NameVirtualHost directive to enable name-based virtual hosts. There are IP-based virtual hosts, but those consume valuable IP addresses. Name-based virtual hosts are recommended by the Apache developers, and are preferred by most administrators.

On the sample Red Hat system, the NameVirtualHost directive is commented-out. The line is:

#NameVirtualHost *

The asterisk on this line stands for any address assigned to any interface on the host. To make this more understandable, we will be more explicit. We remove the hash mark (#) to activate the line, and set the NameVirtualHost address to the primary address of our server:


Next, define the virtual hosts that will be served. For example, to host websites for and on the server, add the following lines to the httpd.conf file:

<VirtualHost> DocumentRoot /var/www/html/fish ServerName </VirtualHost>

<VirtualHost> DocumentRoot /var/www/html/mammals ServerName </VirtualHost>

Each VirtualHost directive defines a hostname alias to which the server responds. For this to be valid, DNS must define the alias with a CNAME record. The example requires CNAME records that assign the aliases of and When wren receives a server request addressed to one of these aliases, it uses the configuration parameters defined here to override its normal settings. Therefore, when it gets a request for, it uses as its ServerName value instead of its own server name, and uses /var/www/ html/fish as the DocumentRoot.

These are simple examples. Any valid configuration directive can be placed within a VirtualHost container.

Was this article helpful?

0 0

Post a comment