Last night I figured out how to get virtual hosts working locally with OS X’s built in Apache web server (Web Sharing). I was very close but what I didn’t know was virtual host declarations in the httpd.conf settings are only half of the process. On non-OS X machines, the second part involves modifying the computer’s “hosts” file to include the additional host names. OS X uses NetInfo to manage all that information and the edits need to be made using the NetInfo Manager application in the Utilities folder.
This site, Enabling Virtual Hosts on MacOS X contained nearly everything I needed to know and confirmed that most of my early explorations were well-directed. The one major hangup I had was that the two virtual hosts were both returning “403 Forbidden” errors when I tried to view anything in those directories. I spent about an hour twiddling with settings and chmod’ing the files and directories over and over before I came across this perfect explanation in Apache’s documentation:
In the case where file system permission are at fault, remember that not only must the directory and files in question be readable, but also all parent directories must be at least searchable by the web server in order for the content to be accessible.
I had mistakenly placed the site directories inside my Documents folder, which Apache didn’t have access to read. I’m still new at this Unix stuff, so I didn’t think to check the parent folders of the site directories, even though the concept is consistent with file sharing under OS 9. Anyway, moving the two website directories into the Sites folder, to which Apache has full read access, immediately cleared up the forbidden messages.
For the two test sites I’m working with, I decided to change their top-level domain from “.com” to “.joe”. So now if I want to see my local copy of this site I just open http://www.joemaller.joe in any web browser on this machine. The URL http://www.joemaller.com still goes to the live site on my ISP’s servers.
Using these modifications it’s possible to tell your computer to ignore the internet and load any URL from a local copy. Cool as this is, it has the potential to really screw up web access. Back up the configuration files and keep track of what you’re doing.
update: I forgot to link to Oreilly’s incredible series Apache Web Serving With Mac OS X, which I’ve been consulting repeatedly.