HomeApache Page 2 - Configuring and Using Virtual Hosts in Apache
Edit the Main Configuration File - Apache
Begin creating and managing virtual hosts with Apache with this clear explanation from Dan Wellman. He explains what virtual hosts are and illustrates a basic way of setting up virtual hosts.
True to its cutting edge character, Apache HTTP server was one of the first servers to begin providing support for virtual hosts and fortunately, configuring them is relatively easy. The mod_vhost_alias Apache module comes as standard with both the compiler and pre-compiled distributions. So no matter how you installed the software, it should be present on your system. For the purpose of this article, I’m assuming that you have already configured Apache for running your main host.
There is a little work to be done in Apache’s main configuration file httpd.conf. Open this file by either browsing to the directory Apache is installed in and opening httpd from within the conf directory or, on a Windows platform, by selecting Edit the Apache http.conf configuration file from the start menu.
Scroll right down to the bottom of the file to section 3, the virtual hosts section. Everything that you need to do in Apache to get a virtual host up and running is done within this section. There is already a commented out virtual host configuration for reference; you can leave this example in place for referring to later or you can remove the hash symbols at the start of each line and adjust the example -- it’s really up to you.
The virtual host directives take precedence over the main server configuration, so to make sure that requests to your main host are not confused with requests to your virtual host, you have to add the main host as the first virtual host. This will ensure your main host stays as the default host. For the following example, I’ve used a main server name of site1.co.uk and I’m adding a virtual host called site2.co.uk, highly imaginative I know but it serves (no pun intended) to illustrate the point. As it’s simpler, we’ll look at name based virtual host configuration first.