## Virtual Hosting with Apache ## Jim Mock This article is somewhat of a continuation of my article in Issue #1 on setting up Apache. Virtual Hosts!?!? What are they? The term Virtual Host refers to the practice of maintaining more than one server on one machine, as differentiated by their apparent hostname. For example, it is often desirable for companies sharing a web server to have their own domains, with web servers accessible as www.company1.com and www.company2.com, without requiring the user to know any extra path information. The information below will help you set up virtual hosts. Now I know what they are, how do I set one up? There are two ways to set up virtual hosts using Apache. The first way is by using IP-based virtual hosts, and the second is by using name-based virtual hosts. Both are set up in Apache's httpd.conf file. IP-based Virtual Hosts As the term IP-based suggests, each IP-based virtual host must have a different IP address. This can be achieved if the machine has several physical network connections, or by the use of IP aliases. IP aliases can be set up by either using /etc/rc.conf and/or ifconfig. See man ifconfig and the /etc/rc.conf file for more information on setting up IP aliases. There are two ways to configure Apache to support multiple hosts. This can be done either by running a separate httpd daemon for each hostname or by running a single daemon which supports all the virtual hosts. The second method is what will be covered here. If you want information on the first method (multiple daemons), see the information on Apache's web site at http://www.apache.org/. Using a single daemon For the following examples, a single httpd will service the requests for the main server and all of the virtual hosts. The VirtualHost directive in httpd.conf is used to set all of the configuration directives to different values for each virtual host. For example: ServerAdmin webmaster@domain.com DocumentRoot /usr/home/bob/public_html-www.domain.com ServerName www.domain.com ErrorLog /var/log/vhost-logs/www.domain.com-error_log TransferLog /var/log/vhost-logs/www.domain.com-access_log ServerAdmin webmaster@domain.org DocumentRoot /usr/home/joe/public_html-www.domain.org ServerName www.domain.org ErrorLog /var/log/vhost-logs/www.domain.org-error_log TransferLog /var/log/vhost-logs/www.domain.org-access_log It is recommended that you use an IP address instead of a hostname. The reason for this is potential problems could arise if the name server of the domain is not responding. See the Issues Regarding DNS and Apache at http://www.apache.org/docs/dns-caveats.html for more information. Nearly any configuration directive can be put in the VirtualHost directive with the exception of ServerType, StartServers, MaxSpareServers, MinSpareServers, MaxRequestsPerChild, BindAddress, Listen, PidFile, TypesConfig, ServerRoot, and NameVirtualHost. User and Group may be used if the suEXEC wrapper is used. For more information on suEXEC, see http://www.apache.org/docs/suexec.html. Name-based Virtual Hosts While using IP-based virtual hosts works very well, it is not always the best solution, because a dedicated IP address is needed for every virtual host and this could cause problems depending on how many IP addresses you have. For this reason, name-based virtual hosts can be used. The benefits of using name-based virtual hosts support are a practically unlimited number of servers, ease of configuration and use, and requires no additional hardware or software. The main disadvantage is that the client must support this part of the protocol. The latest versions of most browsers do, but there are still old browsers in use who do not. This can cause problems, for visitors using older browsers. I'll mention a possible solution to this below. Using name-based virtual hosts is quite easy, and resembles the IP-based method. The notable difference between the two is the use of the NameVirtualHost directive that specifies the IP address that should be used as a target for name-based virtual hosts. For example: NameVirtualHost 192.168.0.1 ServerAdmin webmaster@domain.com DocumentRoot /usr/home/bob/public_html-www.domain.com ServerName www.domain.com ErrorLog /var/log/vhost-logs/www.domain.com-error_log TransferLog /var/log/vhost-logs/www.domain.com-access_log ServerAdmin webmaster@domain.org DocumentRoot /usr/home/joe/public_html-www.domain.org ServerName www.domain.org ErrorLog /var/log/vhost-logs/www.domain.org-error_log TransferLog /var/log/vhost-logs/www.domain.org-access_log To make this work, all that is needed is to make sure that the names www.domain.com and www.domain.org are pointing to the IP address of 192.168.0.1. As I mentioned earlier, some older browsers may not work properly with name-based virtual hosts. These browsers will always be sent the pages from the primary name-based virtual host (the first virtual host in httpd.conf for a specific IP address). A possible work around exists with the ServerPath directive. For example: NameVirtualHost 192.168.0.1 ServerAdmin webmaster@domain.com DocumentRoot /usr/home/bob/public_html-www.domain.com ServerName www.domain.com ServerPath /domain ErrorLog /var/log/vhost-logs/www.domain.com-error_log TransferLog /var/log/vhost-logs/www.domain.com-access_log This means that any request beginning with /domain will be served from the virtual host www.domain.com. This means that the pages can be accessed as http://www.domain.com/domain/ for all browsers, although newer browsers that send a Host: header can also access it as http://www.domain.com/. In order to make this work, put a link on your primary name-based virtual host's page to http://www.domain.com/domain/ and then in the virtual host's pages, be sure to use either relative links (file.html or ../icons/image.gif) or links containing the prefacing /domain/ (http://www.domain.com/domain/files/file.html or /domain/files/file.html). This may require a bit of modifying existing HTML or making sure you stick to the guidelines, but following the guidelines above, your pages should work with both old and new browsers. Either of the above configurations should work, depending on the resources you have available (i.e., IP addresses for IP-based virtual hosts). For more in depth descriptions and for descriptions of the directives available and what they do, please visit the Apache web site at http://www.apache.org/. - Jim $Id: vhosts.txt,v 1.1 2000/02/16 08:07:43 jim Exp $