Third party cookies may be stored when visiting this site. Please see the cookie information.

Linux Apache Virtual Hosts (vhosts)

Introduction

Virtual 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.

Compatibilty

To 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 virtual host servers.

The good news is that almost all browsers currently in use will work with named based virtual hosting. This even includes older versions of browsers running on now obsolete operating systems. There is no reason to not use this on the basis of lack of support in client browsers.

From a server point of view most web server software supports virtual hosting, although there are some light-weight servers that may not support this. 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 Websites

For this example I will use the two domains www.watkissonline.co.uk and www.firstaidquiz.com. These are no longer hosted on the same server, but the theory still stands.

Adding a Virtual Host to Apache 2.0

The virtual host configuration is normally stored in a separate subdirectory from the normal apache configuration files. On Mandriva Linux this is /etc/httpd/conf/vhosts/. The configuration file we will be using is Vhosts.conf, the other configuration files have other uses such as

allowing individual users to have their own virtual webservers. For this example we are just looking at adding a second website to a server.

Ubuntu uses a different format by splitting the files into one per virtual host in the directories /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ , where the latter normally contains symlinks to the files in the sites-available directory.

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 /var/www/html, but could be anything. When we add an extra domain you will need another directory. If you don't want to change the current directory then you could have entries for /var/www/html2 (or slightly more meaningful) /var/www/firstaidquiz, or the new directory could be a sub directory of the current website (e.g. /var/www/html/firstaidquiz/) although this would have the effect of also allowing www.watkissonline.co.uk/firstaidquiz to serve up the pages for one domain from the other, which may not work as expected if the paths in the html document are relative to the root of the nameserver. A better way of doing this may be to use /var/www/watkissonline and /var/www/firstaidquiz particularly if you have planned to use virtual hosts in advance and so can make the changes before the site goes live.

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 /var/www/watkissonline and that we are now adding the second website as /var/www/firstaidquiz.

Vhosts.conf

With 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):



################# Named VirtualHosts

#NameVirtualHost 111.222.33.44

#<VirtualHost 111.222.33.44>

#ServerName www.domain.tld

#ServerPath /domain

#DocumentRoot /web/domain

#</VirtualHost>

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:



################# Named VirtualHosts

NameVirtualHost *:80



# Virtual Host for www.watkissonline.co.uk

<VirtualHost *:80>



ServerName www.watkissonline.co.uk

ServerAlias watkissonline.co.uk

DocumentRoot /var/www/watkissonline

</VirtualHost>

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 defined 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 is not need to include support for browsers that are unable to handle 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:



# Virtual Host for www.firstaidquiz.com

<VirtualHost *:80>

ServerName www.firstaidquiz.com

ServerAlias firstaidquiz.com

DocumentRoot /var/www/firstaidquiz

</VirtualHost>

Permissions

Depending 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:



#

# This should be changed to whatever you set DocumentRoot to.

#

<Directory ...

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:



<Directory /var/www/firstaidquiz>



#

# This may also be "None", "All", or any combination of "Indexes",

# "Includes", "FollowSymLinks", "SymLinksifOwnerMatch", "ExecCGI", or "MultiViews".

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

    Options -Indexes FollowSymLinks MultiViews



#

# This controls which options the .htaccess files in directories can

# override. Can also be "All", or any combination of "Options", "FileInfo", 

# "AuthConfig", and "Limit"

#

    AllowOverride All



#

# Controls who can get stuff from this server.

#

    <IfModule mod_access.c>



      Order allow,deny

      Allow from all

    </IfModule>

</Directory>

More Information

There 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.


» PenguinTutor Facebook page