Virtual hosting in apache
Virtual hosting allows the Apache Web server to host multiple Web sites as part of its own. In effect, the server can act as several servers, each hosted Web site appearing separate to outside users. Apache supports both IP address–based and name-based virtual hosting. IP address–based virtual hosts use valid registered IP addresses, whereas name-based virtual hosts use fully qualified domain addresses. These domain addresses are provided by the host header from the requesting browser. The server can then determine the correct virtual host to use on the basis of the domain name alone. Note that SSL servers require IP virtual hosting. See httpd.apache.org for more information.
You can configure Apache to run a separate daemon for each virtual host, separately listening for each IP address, or you can have a single daemon running that listens for requests for all the virtual hosts. To set up a single daemon to manage all virtual hosts, use VirtualHost directives. To set up a separate daemon for each host, also use the Listen directive.
A VirtualHost directive block must be set up for each virtual host. Within each VirtualHost block, you place the appropriate directives for accessing a host. You should have ServerAdmin, ServerName, DocumentRoot, and TransferLog directives specifying the particular values for that host.
You can use any directive within a VirtualHost block, except for ServerType (1.3), StartServers, MaxSpareServers, MinSpareServers, MaxRequestsPerChild, Listen, PidFile, TypesConfig, ServerRoot, and NameVirtualHost.
Virtual hosting definition
Although you can use domain names for the address in the VirtualHost directive, using the actual IP address is preferable. This way, you are not dependent on your domain name service to make the correct domain name associations. Be sure to leave an IP address for your main server. If you use all the available IP addresses for your machine for virtual hosts, you can no longer access your main server.
IP-based virtual hosts blocks: one using an IP address, and the other a domain name that associates with an IP address:
Virtual Host examples
To implement name-based virtual hosting, use a VirtualHost directive for each host and a NameVirtualHost directive to specify the IP address you want to use for the virtual hosts. If your system has only one IP address, you need to use that address. Within the VirtualHost directives, you use the ServerName directive to specify the domain name you want to use for that host. Using ServerName to specify the domain name is important to avoid a DNS lookup. A DNS lookup failure disables the virtual host. The VirtualHost directives each take the same IP address specified in the NameVirtualHost directive as their argument. You use Apache directives within the VirtualHost blocks to configure each host separately. Name-based virtual hosting uses the domain name address specified in a host header to determine the virtual host to use.
Dynamic Virtual Hosting
If you have implemented many virtual hosts on your server that have the same configuration, you can use a technique called dynamic virtual hosting to have these virtual hosts generated dynamically. The code for implementing your virtual hosts becomes much smaller, and as a result, your server accesses them faster. Adding yet more virtual hosts becomes a simple matter of creating appropriate directories and adding entries for them in the DNS server.
To make dynamic virtual hosting work, the server uses commands in the mod_vhost_alias module (supported in Apache version 1.3.6 and up) to rewrite both the server name and the document root to those of the appropriate virtual server (for older Apache versions before 1.3.6, you use the mod_rewrite module). Dynamic virtual hosting can be either name-based or IP-based. In either case, you have to set the UseCanonicalName directive in such a way as to allow the server to use the virtual hostname instead of the server’s own name. For name-based hosting, you simply turn off UseCanonicalName. This allows your server to obtain the hostname from the host header of the user request. For IP-based hosting, you set the UseCanonicalName directive to DNS. This allows the server to look up the host in the DNS server.
You then have to enable the server to locate the different document root directories and CGI bin directories for your various virtual hosts. You use the VirtualDocumentRoot directive to specify the template for virtual hosts’ directories. For example, if you place the different host directories in the /var/www/hosts directory, then you could set the VirtualDocumentRoot directive accordingly.
The %0 will be replaced with the virtual host’s name when that virtual host is accessed. It is important that you create the dynamic virtual host’s directory using that host’s name.