Apache Web-Serving with Mac OS X: Part 1by Kevin Hemenway
Editor's note: Most people know that Mac OS X ships with a built-in Apache web server, but don't realize that it's easy to configure and run. In fact, you can host a web site in minutes after completing just a few simple steps.
But that doesn't mean that Mac OS X is a lightweight in this category. It's not. In fact, you can drill down and get very serious with this state-of-the-art serving software.
In this first installment of a multi-part series, Kevin Hemenway shows you how to start serving web pages directly from your Mac. In the articles that follow, he'll show you the techniques that system administrators use for maintaing robust web sites. As you read Kevin's tutorials, you'll learn the functions of some of those mysterious folders deep within your Mac OS X hard drive, and will soon become your own "X" Sys Admin.
You stare at the screen, fingers twitching nervously. You've been telling the rest of the world how great Mac OS X is, basking in the sheer enjoyment of upgrading your Classic applications to jelly-powered delights. After much spouting, your boss has finally given you a chance to prove your saliva-laced rantings. The task is quite simple: "build the GatesMcFarlaneCo  intranet with features up the wazoo."
Once again, it's time to convince the herd about Mac superiority. What better way to begin than to use a well-respected web server that's proven its mettle time and time again?
Introducing Apache to the Mac faithful
Apache is regarded as the most popular web server available today. With its incredible portability and support for anything you'd ever want to do, Apple wisely decided to ship Apache with its Unix-based operating system. With this web-serving powerhouse at your fingertips, I'll explain how you can impress your boss and solidify your love for Mac OS X, all at the same time.
Getting started with Apache and Mac OS X
The easiest way to begin Apache web-serving is via your Mac OS X system preferences. Click your Apple Menu, choose "System Preferences," then select "Sharing". Within the Sharing preference panel you'll see a number of options, only one of which is of immediate value. See that Web Sharing label? Simply click the Start button beneath it to fire-up the built-in Apache web server.
Figure 1. Apple has cleverly disguised the Apache Web Server as the "Web Sharing" option in the "Sharing" system preference.
By default, the Apache server has been configured to use your Mac OS X "short" user name. (You can find your short user name in the Users preference panel. Click on "Edit User".) If your short user name is "morbus", for example, you can access your personal web site by opening any web browser on the local network and typing
http://127.0.0.1/~morbus/. Don't forget that last forward slash!
127.0.0.1 (also known as "localhost") is pretty special -- every computer has one. Both names represent the computer itself; by going to 127.0.0.1 in a web browser, you are accessing the local Apache web server you activated in the Sharing preference panel.
Now that you know an easy way to serve a site to local users, how do make your web pages available to folks on the World Wide Web?
The answer is found in your Sharing preference panel. Go there and note the IP address listed. Using that IP address is how outsiders can access your Apache server. If you see
126.96.36.199, visitors could access
http://188.8.131.52/~morbus/ and visit your personal web site. In a future article, I'll discuss how to block outside access to only those you deem worthy.
Your personal web space
If you went to your personal web site now, you'd see a generic introduction written by Apple describing Apache and how to use "Personal Web Sharing." We'll soon be deleting this page, but be sure to give it a read because it contains some helpful bits of information.
Much in the same way your personal web site is based on your user name, so is the location of your web space in the Mac OS X file system. Continuing our assumption of the "morbus" user name, our current web space lives at
If you browse there now, you'll see the introductory file (
index.html) as well as an
images/ subdirectory. Delete (or back up) everything, and use a text editor (such as BBEdit) to create a new
index.html file with the following contents:
<html> <body> <h1>Gleefully Served by Mac OS X</h1> </body> </html>
Upon saving, reload
http://127.0.0.1/~morbus/, and you'll see your rather bland, boring homepage. This certainly isn't very impressive -- downright pathetic, actually. You can't use server-side includes, can't install CGI programs to your
/Sites directory, and can't use PHP or
mod_perl. Plus, your inTRAnet is wide open to the InTERnet. Not so good, bub.
Turning on the magic
What we've done so far won't impress anyone. We have no features and an ugly web site address filled with tildes and unprofessional user names. We've got a long way to go before we can feel good about showing off our masterful Mac OS X box.
Our first step is to give the site a prettier URL. Above, we placed our
index.html file into our own user directory (such as
/Users/morbus/Sites/) -- now, we'll place a copy into Apache's
DocumentRoot is the default server (not user) location that Apache will serve documents from. For Mac OS X, this is located in the
This directory also has a default introductory file -- to see it, visit
http://127.0.0.1/ within your preferred web browser.
Localization or "content negotiation"
If you browse to
/Library/Webserver/Documents/, you'll inadvertently run across another nice feature of Apache: content negotiation. Depending on your visitor, Apache can serve up international versions of your site -- for example,
index.html.en for English speakers, or French (
and German (
index.html.de) versions. Apache for OS X comes with over 25 translated versions of the default
You can take advantage of this feature on your own pages. When you're creating the GatesMcFarlaneCo intranet in your native language, you can translate your pages into other languages and add the two-letter language encoding ("de" for German", "fr" for French, "es" for Spanish, etc.) to the file name. Any time international employees of GatesMcFarlaneCo access your intranet, they'll automatically be served the page that's in their native tongue.
For now, backup or delete all the
files you see in
/Library/Webserver/Documents/ and copy over the
index.html file we created a few steps ago. If you return to
http://127.0.0.1/, you'll see our "Gleefully Served"
This is a good point to catch your breath and play with what I've outlined here. You might want to build some nicer HTML pages to serve as your test site.
In the next installment of this series, I'll tackle CGI access for your Apache server. Until then, enjoy playing with your new Mac OS X web server.
 A rather stupid joke within a joke. McFarlane Toys is often considered the "one to beat" in action figures, and thus the loose association with Bill Gates. The combination of the two names resembles "Gates McFadden" who played Beverly Crusher on Star Trek: The Next Generation. Wil Wheaton, who played her TV son, Wesley Crusher, recently announced that he may be reprising his role with a cameo appearance in the final Star Trek: TNG movie. Wesley Crusher is often regarded as the most-despised Star Trek: TNG character, and that's where we make full circle back to Bill Gates. Glad you wondered?
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
Return to the Mac DevCenter.