15.6 Configuring Apache Virtual Hosts

The Apache HTTP server supports virtual hosts, meaning that it can respond to requests that are directed to multiple IP addresses or host names that correspond to the same host machine. You can configure each virtual host to provide different content and to behave differently.

You can configure virtual hosts in two ways:

  • IP-based Virtual Hosts (host-by-IP)

    Each virtual host has its own combination of IP address and port. The server responds to the IP address with which the host name resolves. Host-by-IP is needed to server HTTPS requests because of restrictions in the SSL (Secure Sockets Layer) protocol.

  • Name-based Virtual Hosts (host-by-name)

    All virtual hosts share a common IP address. Apache responds to the request by mapping the host name in the request to ServerName and ServerAlias directives for the virtual host in the configuration file.

To configure a virtual host, you use the <VirtualHost hostname> container. You must also divide all served content between the virtual hosts that you configure.

The following example shows a simple name-based configuration for two virtual hosts:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName websvr1.mydom.com
  ServerAlias www.mydom-1.com
  DocumentRoot /var/www/http/websvr1
  ErrorLog websvr1.error_log

<VirtualHost *:80>
  ServerName websvr2.mydom.com
  ServerAlias www.mydom-2.com
  DocumentRoot /var/www/http/sebsvr2
  ErrorLog websvr2.error_log

For more information, see http://httpd.apache.org/docs/2.2/vhosts/.