Apache name-based virtual hosting made easy

May 27, '03 08:20:00AM

Contributed by: patrickgibson

If you're a web developer who works on numerous sites, it can be very beneficial to simulate a production environment on your local machine. Doing so typically requires a virtual host, that is a configuration for a particular website with a specific DocumentRoot.

Using Apache and Apple's NetInfoManager, it's possible to setup simple name-based virtual hosts that run only on your machine. For example, if you are a developer of macosxhints.com, you might have a development version on your local machine. Wouldn't it be handy to preview your development version by pointing your browser at http://macosxhints/ (note the lack of ".com")? This way, all your absolute image references (starting with /) and such would work just as they do in the production environment.

I have written a script that lets you add such a virtualhost to your local machine really easily. You simply type:

sudo /path/to/virtualhost.sh

... where /path/to/ is the path to where you installed virtualhost.sh. I like to place mine in /usr/local/bin, as it's convention, and I have added this to my path, but you're free to install it wherever you want. If you want to just test it out in your current directory, type:

sudo ./virtualhost.sh

Follow the instructions, and within a minute you'll have yourself a working virtualhost! For more information, see my website or to dive right in and download the script. If you have any questions, problems, or bug reports, drop me a line.

[robg adds: While I haven't tested this script yet, I did verify that there's no malicious code of any sort. If you try it, please post your experiences, as it seems like a real time saver!]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030523024719522