Home FAQs / How-To's Beginners Guide to Virtual Hosting
Beginners Guide to Virtual Hosting PDF Print E-mail
User Rating: / 9
PoorBest 
Written by Gregg   
Tuesday, 02 December 2008 15:51

So you want to use the Virtual Hosting feature in Apache and you have not read the documentation, have read the documentation and do not understand, have configured your Virtual Hosts and it is simply not working, this guide is for you.

Since most of us do not have an available IP address for each and every virtual host, you will be using the Name Based Virtual Hosting option. This is the easiest form of virtual hosting Apache offers and allows one to have numerous websites, all at different domain names, running from just one IP address.

The key to getting Name Based Virtual Hosting to work is right in the Apache Documentation under the small section "Main host goes away." In most cases people miss this because it is not exactly what turns out happening. In most cases the main host does not go away, but the first virtual host (vhost) configured is served up the main hosts website and not the one we are expecting.

Let's first take a look at what that section says;

Main host goes away

If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.

OK, great, so what does it mean? I think the second problem to this section is that there are two terms used for one thing, global and main. If you replace the word "global" with the word "main" it may be making more sense to you now. If not, no worries, let's have some examples.

To set up our examples let's assume we have our main website being served to the world at www.mysite.com. We want to have a blog and a forum at different addresses, blog.mysite.com and forum.mysite.com. In our Apache configuration we have are main site already configured since it is what we had to begin with. In our httpd.conf file we have;

httpd.conf code:
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.mysite.com:80

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "C:/Apache2/htdocs"

Begining with Apache 2.2.0 the Virtual Host section of the configuration was moved to a seperate file in C:\Apache2\conf\extra called httpd-vhosts.conf. Let's open this file in our editor and have a look.

httpd-vhosts.conf code:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any block.
#
<VirtualHost *:80>
ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
DocumentRoot "/Apache22-dev/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
DocumentRoot "/Apache22-dev/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

How wonderful, most of the work has been done for us. Virtua hosting is turned on with the "NameVirtualHosts:80" line and all we need to do is just replace the example entries with our own. So let's do that, assuming for our example that the blog is located in the filesystem at C:\www\blog and C:\www\forum

httpd-vhosts.conf code:
#
<VirtualHost *:80>
ServerName blog.mysite.com
ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
DocumentRoot "C:/www/blog"
</VirtualHost>

<VirtualHost *:80>
ServerName forum.mysite.com
ServerAdmin This e-mail address is being protected from spambots. You need JavaScript enabled to view it
DocumentRoot "C:/www/forum"
</VirtualHost>

The first thing you may notice is there are some lines in the file example that are missing now that I've modified the file. What will happen if we do not include ErrorLog and CustomLog in these vhosts? Well, errors will be added to the main error log as well as all access logging will be in the main access log.

There is a problem however.

What is the problem? The problem is we are going to run into what was discussed earlier, our first vhost, blog.mysite.com will be served up with our main website. So using the information we've gathered from the documentation we need to add one vhost before the blog vhost noting that the documentation says we need the ServerName and DocumentRoot of the main host. In reality, that is all we need since the rest of the information is already configured in the main section of httpd.conf.

httpd-vhosts.conf code:
#
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot "C:/Apache2/htdocs"
</VirtualHost>

<VirtualHost *:80>
ServerName blog.mysite.com

We are going to also need to give these directories on the file system access or we will be getting a 403 access denied error when we try to view our blog and forum. Simply add;

httpd-vhosts.conf code:
#
<Directory "C:/www/blog">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

<Directory "C:/www/forum">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Note: you may wish to use Options and AllowOverrides, change my "none" to whatever you want to use.

All that is left for us to do now in uncomment the line in httpd.conf that will include our vhosts configuration into Apache and restart Apache. In httpd.conf go near the end of the file and you will find;

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Remove the # from in front of the "Include conf/extra/httpd-vhosts.conf" and restart Apache. Our main site should be the same at http://wwww.mysite.com. Our blog should show up at http://blog.mysite.com and our forum at http://forum.mysite.com.

You have now successfully configured your Apache for Virtual Hosting. Any new hosts you simply add below what have now in the httpd-vhosts.conf file. Always remember to add the Directory contains for these new hosts as well.

Last Updated on Monday, 15 December 2008 00:00