The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
[ HOME | TOPICS | INDEX | WEB RESOURCES | BOOKS | CONTRIBUTE | SEARCH | FEEDBACK | FAQ | FORUMS ]

Things look quiet here. But I've been doing a lot of blogging at dan.langille.org because I prefer WordPress now. Not all my posts there are FreeBSD related. I am in the midst of migrating The FreeBSD Diary over to WordPress (and you can read about that here). Once the migration is completed, I'll move the FreeBSD posts into the new FreeBSD Diary website.

Apache - virtual hosts 26 September 1998
Need more help on this topic? Click here
This article has 2 comments
Show me similar articles
See also Apache topics.

If you want to know more about virtual machines, see http://cybernut.com/guides/virtual.html

Virtual hosts
A web server is both software and hardware.  The machine on which the web site resides is referred to as a web server.  The software which runs the web site is also referred to as a web server.  In our case, Apache (the web server) runs on our FreeBSD box (also, our web server).  But a given web server can run more than one web site.  It can achieve this multi-use by using virtual hosting.

Virtual hosting is the ability for a website to act as if it is the only web site on the machine.  Given that some web site can be idle for long periods of time, allowing many web sites to share the same hardware is a great way to reduce costs. 

A very good explanation appears within the Apache documentation.  For more detail, please refer to that document.

Name-based virtual hosts
For this example, we'll be making use of name-based virtual hosts.  Again, the Apache documentation explains this term very well.  We will also be making use of their example.

In this example, I've registered two names both of which translate to my FreeBSD box.   For more information on how I actually did this, see httpd/dns.   The two host names in question are:

The server machine has one IP address, 192.168.0.45 which resolves to the name test.freebsddiary.org.  Here is what I've ensured is in my configuration file (/usr/local/etc/apache/httpd.conf):

NOTE: See the notes after this example.

...
Port 80

ServerName test.freebsddiary.org

NameVirtualHost 192.168.0.45

<VirtualHost 192.168.0.45>
    DocumentRoot /usr/local/www/data/freebsddiary
    ServerName   test.freebsddiary.org
    ErrorLog     /var/log/freebsddiary-error.log
    TransferLog  /var/log/freebsddiary-access.log
</VirtualHost>

<VirtualHost 192.168.0.45>
    DocumentRoot /usr/local/www/data/freebsddiary.yi.org
    ServerName   freebsddiary.yi.org
    ErrorLog     /var/log/freebsddiary.yi.org-error.log
    TransferLog  /var/log/freebsddiary.yi.org-access.log
</VirtualHost>

DocumentRoot is the location of the html files for the given virtual website.

Newer versions of Apache can use combined logs.  Search for CustomLog in your Apache configuration file in order to see if you can use this format instaed.   In which case, these entries will be helpful as an example:

ErrorLog        /var/log/freebsddiary-error.log
CustomLog       /var/log/freebsddiary-access.log combined

The log and error files will be recorded in the directory /var/log/.   But you can put them anywhere you want.

If you specify an IP address for VirtualHost and supply the ServerName, then you can avoid failures should DNS be unavailable when the configuration file is parsed.  For more detail, please see Issues Regarding DNS and Apache at the Apache website.

This also assumes that you have pointed freebsddiary.yi.org and test.freebsddiary.org to the same IP.  In this case, 192.168.0.45.

So far so good.  However, I've been unable to publish to my Apache Fp webserver.   I think that's because I've not told Apache which users can publish.   For details on how I did that, see Apache - who can publish what?.

NOTE: I found that specifying NameVirtualHost as shown above, caused a problem with the version of Apache I was using.  So I removed it and virtual hosts still worked.

NOTE: Although I am adding an IP address below, there is an easier way. Thanks to James A. Peltier for pointing this out.

You should also see Apache - virtual hosts (continued).

An easier way 10 September 2002

As mentioned above, there is an easier way. Instead of declaring your virtual hosts with a predefined IP address, you can use * instead. For example, I started out with this:

<VirtualHost 192.168.0.56>
But I could use this instead::
<VirtualHost *>

Similarly, you can use * in the NameVirtualHost declaration instead of an IP address:

NameVirtualHost *

Share
Need more help on this topic? Click here
This article has 2 comments
Show me similar articles