HomeApache Page 3 - Configuring and Using Virtual Hosts in Apache
Virtual Host - 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.
To begin, remove the comment symbol from the start of the NameVirtualHost directive. By default this should already be followed by *:80, this means the server will listen to any and all IP addresses on port 80 (the default port for web services).
Next you can either add the directive <VirtualHost www.site1.co.uk> to the very bottom of the file, or remove the hash symbol and overtype the example. Remember, this needs to match the server name defined in section 2 of the configuration file.
Next, explicitly state the server name:
ServerName www.site1.co.uk
The next line of code defines the folder that normal HTML documents are served from; add DocumentRoot "pathtoyourdocumentroot" to the next line. The document path will typically be something like www/var/html/site1 on a UNIX platform.
Lastly, add ServerAdmin webmaster@site1.co.uk to give a contact email address on error documents and add the closing </VirtualHost> tag.
Now, you need to define the actual virtual host; copy the block of text that you have just created and paste it directly below it. Now just change the values containing site1 to site2 and ensure the document root statement matches your directory set up. Now save the file and restart Apache to update its configuration.
Simply using the technique described above will not let web users access your virtual site; in order for them to use the site URL, it will need to be included on the servers DNS record. This is relatively easy and usually requires you to simply add the virtual host name to the existing line of the DNS record for your IP address. When using IP based virtual hosts, you will need to add the IP address and the virtual server name below your existing IP address and host name.
Configuring IP based virtual hosts is usually similar although you use the IP address for your virtual host in the opening <VirtualHost> tag, for example;
<VirtualHost 123.45.6.78>
Next state the site name in the ServerName directive, e.g.
ServerName www.site2.co.uk
You then add the remaining ServerAdmin etc, directives. In addition to this, in order to make Apache bind to the appropriate IP addresses and server names, you will need to add a listen directive to section 1 of the httpd.conf file. There should already be one present, so scroll up through the file until you find it and add Listen 123.45.6.78:80 (or whatever happens to be the IP address you have been allocated.) Once you have saved the configuration file and restarted the server, and the DNS records have been updated, this should then work.
When setting up IP based virtual hosts, you will need to have already acquired your IP addresses. These can be obtained normally from your ISP.