Apache’s Virtual Hosting Capabilities

When you set up Apache on an Internet host it can respond to an HTTP request for that host. For example, if you set up Apache on a host called server1.doman.com, Apache will serve HTTP requests for this host.

However, if you set up your DNS records such that two other hostnames (say www. mycompany-domain.com and www.friendscompanydomain. com) point to the same machine, you can have Apache serve these two domains as virtual Web sites.

In such a case www.mycompany-domain.com is considered the primary Web hostname (main server) and the rest of them will be considered as virtual Web site or virtual hosts. Apache allows virtual hosts to inherit configuration from the main server, which makes the virtual host configuration quite manageable in large installations where some contents can be shared.

For example, if you decided only to have a central CGI repository and to allow virtual hosts to use the scripts stored there, you do not need to create a ScriptAlias directive in each virtual host container. Simply use one in the main server configuration and you’re done. Each virtual host can use the alias as if it was part of its own.

Apache’s configuration file httpd.conf separates virtual host configuration from the main server configuration using the <VirtualHost container>. However, many directives in the main server configuration (that is, any directive outside of the <VirtualHost container> container) still apply to virtual hosts that do not override them.

There are three ways you can create virtual Web sites using Apache, as discussed in the following list:

  • Name-based: Name-based virtual Web sites are very common. Such a configuration requires that you have multiple hostnames pointed to a single system. You can create multiple CNAME or A records in DNS to point to a single host. Because this method does not use IP addresses in Apache configuration, it is easy to port if you change your IP addresses for your Web server.
  • IP-based: This method requires IP addersses in Apache configuration and thus makes it easy to port when IP addresses need to be changed.
  • Multiple main servers: This method involves using multiple primary Web server configurations. This method is only recommended if you must keep separate configuration file per virtual hosts. This is the least recommended method and is hardly used.