Icon Patrick Gibsonpatrickgibson.com
news and such projects, etc. movies writings hyperlinks contact
search:
news and stuff

Friday, May 16, 2003

Virtual Hosting on Mac OS X

This now lives on Github.
I am moving this project over to Google Code for easier maintenance and issue-tracking. http://code.google.com/p/virtualhost-sh/
Download [Snow] Leopard-compatible version now! Note that this version only works for [Snow] Leopard. For previous versions of Mac OS X that use Net Info Manager (everything before Leopard), download this version.
Warning: High Geek Content! I've read several articles on the internet (mostly over at the O'Reilly's MacDevCenter) about virtual hosting in Mac OS X. We at Coverall Crew do extensive virtual hosting locally in our development environment. Using it, we simulate a production environment, which is a huge plus for us. At any rate, my point is that I've never really been satisfied at how hard most people make this process. So I decided to do something about it...

Note: It has been brought to my attention that in the book, Mac OS X Hints: Jaguar Addition by David Pogue and Rob Griffiths (editor), a section is covered on setting up virtualhosts. If you have read this article and incorporated the changes it suggests, please comment out said changes before running the script. This includes the "NameVirtualHost 127.0.0.1" line, and any <VirtualHost 127.0.0.1>...</VirtualHost> blocks you may have added. In my opinion, this article makes things messy, and generally does virtualhosting in an inelegant way.

Enter virtualhost.sh -- the most powerful and easy shell script for Mac OS X that sets up a virtualhost into your system. Using NetInfoManager, virtualhost.sh lets you easily create a named virtual host that is only accessible locally.

Here's how to use it:

Update: Do to popular demand (including my own), I have added removal functionality. To remove a virtualhost you've added, run the script like so:

sudo /path/to/virtualhost.sh --delete <virtualhost>

You can install this script wherever you like. I put mine in /usr/local/bin, strictly because of habit from previous Unix systems. I've tried to test this as much as possible, but if you experiencing any problems, drop me a line.

Known limitations:
Several people have asked me if you can use this script to setup a live site. This is currently not possible. The majority of users using this script on their local machine will have a dynamically-assigned IP address. Because Apache's name-based virtualhosting requires you to enter the IP address you're using, a dynamically-assigned address would mean that you would have to update your Apache configuration every time your address changes. Additionally, this script uses NetInfoManager (/Applications/Utilities/NetInfoManager) to make the hostname resolveable. A real IP address with a real hostname does not need an entry in NetInfoManager, and the presense of one could be problematic if the IP address ever changed (I believe NetInfoManager is consulted before a DNS request). At any rate, if you wish to change the default IP address used by the script, you can do a search and replace like so:

cat virtualhost.sh | sed s/127.0.0.1/aaa.bbb.ccc.ddd/ > virtualhost-new.sh
... where aaa.bbb.ccc.ddd is the IP address you want to use.

That all said, if you do happen to have a hostname and a static or dynamic address that never changes, and you wish to enable a name-based virtualhost, you can do what I do:

  1. Locate the virtualhost file:
    cd /etc/httpd/virtualhosts
  2. Copy the generated file to a new file:
    cp mysite mysite.ext
  3. Edit the file, and replace the IP address in the <Virtualhost 127.0.0.1> directive with the address you wish to use, and the ServerName mysite with ServerName my.real.hostname.com (where my.real.hostname.com is your real hostname, and not literally what I wrote).
  4. Edit /etc/httpd/httpd.conf, and add a NameVirtualHost statement right below the one the script originally added (at the bottom of the file):
    NameVirtualhost 127.0.0.1
    NameVirtualHost aaa.bbb.ccc.ddd
    ... where aaa.bbb.ccc.ddd is the IP address you're using.
  5. Give Apache an ol' restart (apachectl restart) and then type your URL into your browser to make sure it worked.

Several people have asked about donations of some kind. Now of course, this is completely unnecessary, but if you'd still like to show your appreciation somehow, I wouldn't say no to a PayPal donation or Amazon gift. ;)

Posted at 18:06
« The Incredible Machine | Back to News and Such | New Beginnings »

back

Retrix Hosting