A Networked Virtual Hosting Party on Mac OS X

June 28, 2006

by — Posted in General

There are plenty of articles on setting up virtual hosts on Mac OS X but I’d not found a simple one that showed you how to set up named virtual hosting that could be read across your LAN. The aim for me is to be able to type in e.g., http://myparty/, in any browser on our small local network and get to the locally hosted site.

My requirements are pretty minimal, needing only to have one or two other Macs running OS X (Tiger) access the development server. I’m not going to go into too much detail, partly as I’m too lazy, but also as your setup and needs will be different to mine anyhow. Instead I’ll just give a general run through and point to further reading.

Decide where we’re going to host the party

The first thing we need to establish is the main server which requires that that server have a static IP address. In my case this involved going to System Preferences > Network > TCP/IP and setting Configure IPv4 to “Using DHCP with manual address”. In that same dialog I then set an IP address of 192.168.1.100 and clicked “Apply Now”. That now means that this machine has a consistent address that others on the network will refer to.

Tell everyone where the party is

When the other OS X machines on the network type http://myparty/ in their browsers the first thing their machines do is check in the file /etc/hosts to see if there’s an IP address that matches “myparty”. If there isn’t things won’t go very far. So if you take a look at my one…

127.0.0.1   localhost
127.0.0.1   myparty
255.255.255.255 broadcasthost
::1             localhost

…you’ll see that http://myparty/ maps to my localhost IP address 127.0.0.1 which is fine as my machine is the host. In order that the other machines know where to go for “myparty” we have to edit their /etc/host files to send requests to the static IP address I set earlier:

127.0.0.1   localhost
192.168.1.100   myparty
255.255.255.255 broadcasthost
::1             localhost

Set up the virtual hosts

Now that everyone knows where myparty is being hosted we need to prepare. First you need to edit the Apache web server configuration file httpd.conf which by default lives here /private/etc/httpd/httpd.conf. I’ve installed Apache2 in /usr/local/ which is the more generally expected place for it to be and since this is my notepad I’m going to go with that. So first I need to open httpd.conf as the all powerful root user so I need to type…

$ sudo vim /usr/local/apache2/conf/httpd.conf

…whereas you could go…

$ open -a TextWrangler.app /private/etc/httpd/httpd.conf

…or whatever your personal setup/preference is. Once open you might like to use this little tip I picked up of having your specific settings in a separate file so e.g. at the end of my httpd.conf I have the following…

include /Users/nicklo/Sites/apacheconf/users

…that points to a directory which contains the file nicklo.conf. Inside that file are my virtual host settings. Having those settings separate from the main httpd.conf makes them more easy to backup along with all my other projects. It also potentially keeps them out of harms way in case a Software Update decides it’s time to fiddle with your httpd.conf file.

In keeping with the example my nicklo.conf could then have the following…

NameVirtualHost *:80

<Directory "/Users/nicklo/Sites">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName 127.0.0.1
    DocumentRoot /Users/nicklo/Sites
</VirtualHost>

<VirtualHost *:80>
    ServerName myparty 
    DocumentRoot /Users/nicklo/Sites/myparty
</VirtualHost>

You’ll notice the settings specific to “myparty” are the last ones. These basically say if you get any requests for “myparty” on any IP address (* wildcard) via port 80 then send them through to /Users/nicklo/Sites/myparty.

Party on!

All that’s left now is restart Apache via System Preferences > Sharing > Services > Personal Web Sharing or…

$ sudo apachectl graceful

…which is actually better as it will at least tell you if there are any issues rather than just sit there.

Actually, we we still have to build the myparty website/web application in /Users/nicklo/Sites/myparty/public_html but hey, that’s the easy part right?!

Now in an even more perfect world I would be able to tell you how to set up a proper DNS Server on the host machine, that would replace the somewhat hacky need for individually configured /etc/host files. However, right now I’m just too stupid and honestly, a bit scared, to even attempt that. If anyone can give me a simple and if possible metaphorical run through I’d be very interested.

Further Reading

Virtual Hosting on Mac OS X I need to give credit to the comments by “Mats” on that page for much of my setup above.

Staging websites on Mac OS X

Virtual Hosts for Dummies

Location, Location, Location: Tips for Storing Web Site Files

Apache Virtual Host documentation

VirtualHost Examples