VirtualHosts on a Mac

Article:
VirtualHosts

Updated: December 18, 2007

This article is about VirtualHosts. This is having multiple web sites on a single machine. If you're the web master for several web sites, you'll find this very useful.

So what's the problem?

It's very desirable to be able to host more than one web site on a web server. Most web sites have directories such as /images which contain resources which are referenced by many pages. So if you have multiple web sites, they'll all be looking for their /images in the same location. So the image /images/logo.jpg will be the same on every web site. Not good!

So, it'd be cool to be able to say:
http://striders => (folder) /Users/rmills/striders
http://clanmills => (folder) /Users/rmills/clanmills

And that's exactly what you can do with VirtualHosts.

So, I'm able to maintain several web sites on a single machine and all of them know where to find their /images directory and all the other resources they wish to use.

Here are some additional web sites which you may find useful:

MacOSX-Hints Discussion Threadforums.macosxhints.com/showthread.php?t=77602
Utilityhttp://patrickgibson.com/utilities/virtualhost/
Apache 1.3 Docshttp://httpd.apache.org/docs/1.3/vhosts/
Apache 2.2 Docshttp://httpd.apache.org/docs/2.2/vhosts/
Useful Windows sitehttp://johnbokma.com/windows/apache-virtual-hosts-xp.html
PHP Windows sitehttp://apptools.com/phptools/virtualhost.php

By the way:
There are additional URLs to interesting sites about this in the the thread on:
forums.macosxhints.com/showthread.php?t=77602.

Article

Before you begin - setting up your hosts file

You've got to understand about the hosts file. /etc/hosts on UNIX and c:\windows\system32\drivers\etc\hosts on Windows. This file provides a lookup table for a name and IP address. On my machine, this looks like:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost

192.168.2.101   laptop
192.168.2.102   powerbook
192.168.2.103   home
192.168.2.104   ali
192.168.2.105   imac
192.168.2.105   striders
192.168.2.105   clanmills
192.168.2.105   heather

So, when you type in the address http://clanmills the browser discovers that the IP address of 'clanmills' as 192.168.2.105 and does a GET on the HTTP Server at that location (on port 80).

Virtual Hosts work because the HTTP Server (Apache on a Mac) sees RequestHeaders something like:

GET / HTTP/1.1
User-Agent: Mozilla/5.0 ... deleted ...
Referer: http://clanmills/
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: clanmills:80

The HTTP Server is able to use the Host: request to apply the request to /Users/rmills/clanmills. Ingenious, isn't it?

1 Virtual Hosts - Apache (2.2.6) on MacOS-X 10.5

You have to change two files:
/etc/apache2/httpd.conf and /etc/apache2/extra/httpd-vhosts.conf

In the file /etc/achache2/httpd.conf, you'll have to uncomment the line 468:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

This in consequence causes /etc/apache2/extra/httpd-vhosts.conf to be read. And I commented off everything in httpd-vhosts.conf and replaced it with:

#
# 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 <VirtualHost> block.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot "/www/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
#    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log common"
#</VirtualHost>
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host2.example.com
#    DocumentRoot "/www/docs/dummy-host2.example.com"
#    ServerName dummy-host2.example.com
#    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log common"
#</VirtualHost>

# -------------------------------
# rmills Added clanmills virtual host 09.06.07
#
NameVirtualHost *

<VirtualHost *>
    ServerName     localhost
    DocumentRoot   /Library/WebServer/Documents
</VirtualHost>

<VirtualHost *>
    ServerName     clanmills
    DocumentRoot   /Users/rmills/clanmills
    Options        FollowSymLinks MultiViews Includes
    DirectoryIndex index.html default.shtml
    AccessFileName .htaccess
    <Files *>
        Options +Includes
    </Files>
    <Directory *>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *>
    ServerName    striders
    DocumentRoot /Users/rmills/Striders
    Options        FollowSymLinks MultiViews Includes
    DirectoryIndex index.html default.shtml
    AccessFileName .htaccess
    <Files *>
        Options +Includes
    </Files>
    <Directory *>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *>
    ServerName    heather
    DocumentRoot /Users/rmills/heather/httpdocs
    Options        FollowSymLinks MultiViews Includes
    DirectoryIndex index.html default.shtml
    AccessFileName .htaccess
    <Files *>
        Options +Includes
    </Files>
    <Directory *>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

The effect of all of this is:
http://localhost still works
http://clanmills -> /Users/rmills/clanmills
http://striders -> /Users/rmills/striders
http://heather -> /Users/rmills/heather/httpdocs

2 Virtual Hosts - Apache (1.3.33) on MacOS-X 10.4

I was able to put all the changes into /etc/httpd/httpd.conf. Look for (around line 1080) the statement:

NameVirtualHost *:80

And replace it with a block of <VirtualHost> code similar to the Apache 2.2 code.

3 Virtual Hosts - Apache on Windows

http://www.imagevertex.com/rmills/

I haven't needed to do this. However, I believe you can do something very similar to the MacOS-X procedures above - depending on whether you have Apache 1.x or 2.x installed on your machine. I have listed a couple of URLs at the top of this article which deal with Windows.

Shortcut to IIS Control on Windows 4 Virtual Hosts - IIS on Windows XP

I've never discovered how to do this. If you know, please email and I'll be happy to update this article and acknowledge your help.

I maintain two web sites on my windows machine. I have clanmills.com in the directory c:\web\clanmills and stevenscreekstriders.org in c:\web\striders. So I keep a shortcut on my desktop to open the IIS Control Panel. I created this by opening the control panel/Administrative Tools. You'll find an icon for "Internet Information Services" there. I alt-dragged (copied) the icon to the desktop. The "Magic" is to set the shortcut target = "%SystemRoot%\System32\inetsrv\iis.msc"

So when I want to change from one host to another, I open the IIS Control Panel and drill down to Web Sites/Web site/Properties/Home directory and set Local Path. I know this is really dumb and tedious - however it works!

Comments?

I'm very happy to accept comments, feedback and suggestions for any of my articles. I'm always happy to hear you - especially if you have constructive suggestions. And I'm particularily pleased if you can let me know about corrections.


Home ......... About

Page design © 1996-2007 Robin Mills / webmaster@clanmills.com

Updated Tuesday December 18, 2007