Configuring and Using Virtual Hosts in 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.

Introduction

Virtual hosts are an integral part of the Apache HTTP server; after all they have an entire section in the httpd.conf file devoted exclusively to them. But what are they and what can they be use for?

In Internet terms, your web server is the host that serves the web pages that make up your site. Your server and the web browsers that are used to access your site interact in what is known as the host/client relationship.  If your server hosts just one site, it would be known as a dedicated server, but these are usually only necessary for running large sites with high traffic. 

Virtual hosts are separate hosts that are run from the same server as a main host, or site. This means therefore that one server can host several separate web sites simultaneously, each accessed via separate DNS names.  Visitors to your site will be unable to distinguish between virtual or dedicated sites.  The main difference between hosts and virtual hosts is that hosts have unique IP addresses and virtual hosts do not necessarily have individual IP addresses. There are in fact two types of virtual host; IP based and non-IP or name based.  IP based, as the name states do not share an IP address, non-IP based do. IP based virtual hosts are useful for when you want to manage more than one site on the same server so that all hosts inherit the characteristics defined by your main host, but for some reason, such as when using SSL for example, a unique IP address is necessary.

The fact that virtual hosts are not required to have unique IP addresses is largely what brought them about in the first place; because of the limitations of IPv4 (Internet Protocol version 4) and the explosive growth of the Internet, something was required to solve the problem of the rapidly dwindling supply of available unique IP addresses. Thus, server software began to incorporate support for non-IP based virtual hosts. Something to make note of at this point is that HTTP1.1 was also implemented with support for virtual hosts and there are still some browsers out there that don’t support HTTP1.1, so therefore Apache has to handle requests from older browsers differently. Instead of receiving hostname as part of the HTTP header and opening the correct homepage in that way, it has to serve a list of available hosts. This situation will probably only arise very infrequently however, as even browsers that only support HTTP1.0 will usually have an extension to add the hostname to the HTTP header.

Virtual hosts are not the only solution to the problem of IPv4 limitations, as a point of interest, IPv6, the latest Internet Protocol incarnation, is already being used by some servers and browsers.  This version is based on a 128bit IP address format instead of the old 32bit format used by IPv4, which increases the number of unique IP addresses exponentially.  

{mospagebreak title=Edit the Main Configuration File}

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.  

{mospagebreak title=Virtual Host}

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.

{mospagebreak title=Find File System Called Hosts}

I have shown you only the most basic way of setting up virtual hosts; you can use almost any directive inside a <VirtualHost> container, and these directives will override or work alongside the equivalent directive in the main server configuration.

The only limit to how many virtual hosts you can create are the limitations of the physical machine that Apache is running on. Apache uses file descriptors, integers denoting files that are open, for any log files, in addition to another twenty used by the server itself. On most UNIX platforms, the limit to the number of file descriptors in use is 64, which can be used up very quickly. Changes to the configuration of the operating system can increase this, but a more suitable solution is not using the ErrorLog or TransferLog directives within the <VirtualHost> blocks, which will make Apache write only to the main hosts log files and therefore reduce the number of log files in use. Additionally, some operating systems will need to have an extra network interface card for each IP address.   

If you are using Apache on a Windows XP desktop PC, for testing PHP or CGI for example, you now need to find a system file called hosts, which will be under the following path: C:WINDOWSsystem32driversetc. Open the file with Notepad and you should see something like this:

# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10      x.acme.com                 # x client host
127.0.0.1       site1 site2

The IP Address for a local host is always 127.0.0.1. To make it work you just need to add the virtual host name to the same line as the main host name, separated by a tab-space. This is similar to mapping your server’s DNS settings.

You should now have enough information to begin creating and managing virtual hosts with Apache. There are many configurations that can be used, so experiment until you find one that matches your particular needs.

Google+ Comments

Google+ Comments