Apache – virtual hosts

Apache – virtual hosts

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 (added on 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 *

2 thoughts on “Apache – virtual hosts”

  1. Hello

    I am writing this because i have a little problem
    with my apache. And I thought that some can help me.
    The thing is that I set few webservers
    however i do not know how I can get
    http://www.tomcieplinski.com:81 to work . I got to work
    tomcieplinski.com:81.
    Please have look.

    NameVirtualHost 192.168.1.2:81
    NameVirtualHost 67.82.35.207:81

    <VirtualHost 192.168.1.2:81 67.82.35.207:81>
    DocumentRoot /home/www-root/tactech.net
    ServerName http://www.tactech.net
    DirectoryIndex index.html welcome.html
    </VirtualHost>

    This works no problem ! Either http://www.tactech.net:81 or
    tactech.net:81

    <VirtualHost 192.168.1.2:81 67.82.35.207:81>
    DocumentRoot /usr/share/squirrelmail
    ServerName mail.tactech.net
    DirectoryIndex index.php
    </VirtualHost>

    This works fine too.

    <VirtualHost 192.168.1.2:81 67.82.35.207:81>
    DocumentRoot /home/bsienkiewicz/www-root
    ServerName blazej.tactech.net
    DirectoryIndex index.html
    </VirtualHost>

    This does NOT work —-> do not know why .

    <VirtualHost 192.168.1.2:81 67.82.35.207:81>

    DocumentRoot /home/www-root/slaveofthesysx.com
    ServerName http://www.slaveofthesysx.com
    ServerAlias slaveofthesysx.com
    DirectoryIndex index.php index.html welcome.html
    index.htm index.shtml

    </VirtualHost>

    This works just fine.

    <VirtualHost 192.168.1.2:81 67.82.35.207:81>
    DocumentRoot /home/www-root/tomcieplinski.com
    ServerName http://www.tomcieplinski.com
    ServerAlias tomcieplinski.com
    DirectoryIndex index.html
    </VirtualHost>

    And here I can only get tomcieplinski.com:81 I do knot
    know why when I type in a web browser
    http://www.tomcieplinski.com:81 I get nothing no webpage
    whatsoever.

    LockFile "/var/lock/httpd.lock"
    CoreDumpDirectory "/etc/httpd"

    Thank you for Your help.

    Tom C

Leave a Comment

Scroll to Top