Serve local web pages via name not IP number

29.11.2008
Today's hint is about OS X 10.5's built-in Web server, and how you can make it easy to access sites you're running off that server. In case you weren't aware, the built-in OS X web server is the industry-standard web server that powers literally thousands of web sites around the world. As of OS X 10.5.5, it's Apache version 2.2.9.

So what can you do with OS X's built-in Web server? Almost anything, though the one thing you probably can't do is use it to serve web pages to the outside world, at least not easily. Many ISPs don't allow web servers to run on a standard home account, and even if they do, your firewall and/or router may make it difficult for users to reach your site. If you get around those problems, you'll still need to arrange for a domain name to point to your home machine--and again, your ISP may make that difficult. You can solve some of these issues by using a service such as , which lets you assign a domain name to an ever-changing home IP address. You'll still have issues if your ISP won't allow you to host sites, however.

So if you can't serve sites to the outside world, why then might you want to run a web server anyway? Perhaps, like me, your job requires you to maintain a web site, and you'd like to keep a copy of it locally for testing. Or perhaps you'd like to try out some of the popular blogging web applications (, ) or some of the available (, for instance, which powers ) on your own Mac before you decide to launch a site with a web hosting company. (As an aside, this is exactly how macosxhints.com got started--I installed Geeklog locally and ran it on my own Mac for a couple of months before I launched the site to the public. In many ways, the built-in OS X web server is the one piece of technology most responsible for my launching macosxhints.)

Whatever the reason, OS X makes it super easy to run a web server on your very own Mac. You can enable the built-in web server by clicking the Web Sharing button in the Sharing System Preferences panel. Once enabled, you'll see a couple of links in the area to the right of all the sharing options; one lists your computer's web site URL, and the other lists the URL for your personal site. For most users, these addresses will appear as a series of (IP address) numbers--in my case, they're listed as http://192.168.1.44 and http://192.168.1.44/~robg, respectively. If I open Safari or any other web browser and enter those URLs, I'll see the standard pages as shown in the image at right--that's the computer page at the top, and my personal page at the bottom.

While this works well enough, wouldn't it be nicer to access your locally-hosted sites via domain name, just like you do on the "real" Internet? With a couple of relatively easy edits in the Terminal, you can do just that. Apache includes support for what they call name-based virtual hosts, and by enabling those, you can name your server and its hosted sites anything you like. Here's what you need to do.

These instructions assume that you'll be installing your sites in your user's Sites folder, and that you'll place each different package you want to use (WordPress, Movable Type, etc.) in its own sub-directory, which is a pretty standard practice. They also assume you're using 10.5; this method will not work in 10.4, as it doesn't use Apache 2. It also assumes that you don't need to access any sites stored in the standard /Library -> WebServer -> Documents folder--I do have a relatively simple fix for that problem near the end of this hint, though.