|
Linux Apache Virtual Hosts (vhosts)IntroductionVirtual Hosting is the ability to run several different websites on the same server. There are a number of ways that this can be achieved, using different port numbers for the webservers (instead of the default port 80); using different IP addresses aliased to the same server; or by using the information contained int the http header to determine which site the user is trying to access. The most common technique is the latter and is called "Name Based Virtual Hosts". This is the method I will describe here. Virtual Hosting is not required if you are serving up the same pages for different domains. For example if you had two domains one ending with .com and the other .co.uk and they were for the same site then you can just point the DNS entries at the same server and they will both serve up the same pages. If you also have other websites on the same server then additional configuration will be needed to allow the same site to be served up for both domains. CompatibiltyTo understand the implications for why this may not work with all browsers we need to look at a little bit of history. The ability to use name based virtual hosts was not included in the original specifications for the World Wide Web. In the original design each server would have a unique IP address and there was normally a one-to-one relationship between the domain fully qualified hostname name and the ip address. The original specification of the http protocol (version 1.0) does not include the address of the site that you are trying to access. This means that if users have a really old browser then they will not be able to access the virtual hosts on the server. Fortunately the protocol did allow for extensions and http version 1.0 browsers can still access virtual hosts as long as they use these extensions. The current version of http is version 1.1 which includes the hostname natively so will work with virtal host servers. The good news is that almost all browsers currently used will work with named based virtual hosting. This even includes older versions of browsers running on now obsolete operating systems. Unless you know that you have users that need to access the site using very old browsers you can safely run in this mode. From a server point of view all good web server software supports virtual hosting. I will describe the technique for Apache. I have tested this using an Apache version 2 server, but I have also configured virtual hosting on 1.3 servers. The biggest difference is that Apache 2 has support for a few more features within virtual hosting, the main one being VLOG entries as a means of directing different virtual hosts to different logs. This is not required for this example but could be useful if using virtual hosting in conjunction with loginfo - apache log analysis tool. The WebsitesFor this example I will use the two domains www.watkissonline.co.uk and www.firstaidquiz.com. These are (at the time of writing) hosted on the same web server, but the real configuration for this server differs from that used here. Adding a Virtual Host to Apache 2.0
The virtual host configuration is normally stored in a seperate subdirectory from the normal apache configuration files. On Mandriva Linux this is
DocumentRoot Directories
For each virtual server we need a DocumentRoot directory that specifies where to serve the different websites from. On a webserver without virtual hosts
this is often After creating the new directory ensure it has the correct permissions to allow the content to be uploaded to the site and for the webserver (normally apache:apache) to be able to read the pages.
We will assume that we already have www.watkissonline.co.uk installed in Vhosts.confWith the currect directories setup we can now look at updating the Vhosts.conf file. There is an example of how this is configured within the
Vhosts.conf file which is shown below (note all these entries are prefixed with a # as these are only comments):
It is important that the existing domain is included in the Vhosts.conf file even though it may be working before named virtual hosts are added.
For the existing domain watkissonline.co.uk create the following entry:
The NameVirtualHost entry only occurs once for all the domains. It specifies the IP address and Port Number to apply the name virtual hosts to. With the entry *:80 it will apply to all IP addresses, but only on port 80. So if you are using secure ssl or have any other servers on another port then these won't been affected. The remaining details are contained within a VirtualHost container. The entries in this section will only apply to this one virtual host. The first VirtualHost entry again specifies that this will be for any ipaddress on port 80. The ServerName entry specifies the virtual host, and then the DocumentRoot specifies which directory the files are contained in that we created above. For an existing entry the DocumentRoot entry should be the same as defiend in httpd.conf (or httpd2.conf). I have also added a ServerAlias entry which means that this section will also be used if the request is for the hostname watkissonline.co.uk (note: without the www). I have not included the ServerPath setting as it is only for the browsers that don't support name based virtual hosts, as I mentioned above there are very few browsers that would not work with named virtual hosts.
The rest of the virtual hosts should be below this first one. The top Virtual Host will be used as the default if the hostname doesn't match any others. The second entry may look like:
PermissionsDepending upon where you put the new DocumentRoot directory you may need to add a new access entry in your httpd.conf, httpd2.conf or commonhttpd.conf (depending upon distribution) file. There should be a current entry:
If the new virtual host is a subdirectory of that directory then it should work. If not then you may need to create another <Directory> section. The easiest way is to copy the current one and just add in the new DocumentRoot:
More InformationThere are a lot more parameters that can be configured. Full details are available from the Apache http Server Documentation and in particular the Apache Virtual Host Information. |
Watkissonline.co.uk Online Baby Shop (UK)
First Aid Quiz Online Shop (UK)