Hack 88 Serving Up a Web Site with the Built-In Apache Server


With the Apache web server under the hood, OS X is a web powerhouse right at your very fingertips.

Apache is one of the most popular, if not the most popular, web server available today. It has support for literally anything you want to do. Jaguar ships with Version 1.3.26 of Apache, and all you need to do to start it up is to click a single button in your System Preferences application. Here's how.

Open up the System Preferences application. If it's not on your Dock, then you can find it in the Applications folder of your hard drive. Once launched, click on the Sharing button, then click on the Personal Web Sharing checkbox, as shown in Figure 8-13. Don't let the name Personal Web Sharing fool you. This is the full-strength Apache web server running, no matter how innocent or lightweight it sounds in the preference panel.

Figure 8-13. Turning on Personal Web Sharing

That's it. You can now point a web browser at your machine's IP address, hostname, or http://localhost and see the default Apache home page ? yes, the one with the big "Seeing this instead of the Web site you expected?" caption. This rather bland default home page is located in /Library/WebServer/Documents, the home of your web site on this machine. Easy huh?

88.1 User Sites

If you've ever had a web site hosted at an Internet Service Provider (ISP), you're probably used to having a URL that looks a little something like this: http://www.myisp.com/~me, where me is your login name. In addition to the main web site you now have running on your Macintosh, each user ? remember, whether you're the only user or not, OS X is a multiuser system ? also has his very own site.

The files for your user site live in the Sites folder in your home directory [Hack #1]. Point your web browser at http://localhost/~me, where me is the Short Name[Hack #1] associated with your user account. You'll be greeted with a friendly "Your website here" message, along with further instructions on building your own web site. That page is actually a file called index.html sitting in your Sites folder; edit it using your favorite text or HTML editor and reload the page in your browser to see the results.

Why use user sites when you have a perfectly good main site at http://localhost (/Library/WebServer/Documents)? Perhaps you share your computer with others and each of you wants your own web site. Maybe you want a space to experiment with content and functionality without reflecting that out to the world. I've configured my Apache server [Hack #89] such that, while my main site is readily viewable by anyone on the network, my user site is restricted only to viewing from my computer itself. This gives me a place to fiddle with all sorts of things without worry of anyone else stumbling across my experiments.

88.2 Behind the Scenes: the Configuration Files

But what's really going on here? Let's look behind the scenes and see what the Unix core of Mac OS X is doing.

When you click that innocent-looking Personal Web Sharing button in the System Preferences application, what happens is that a flag gets changed in the /etc/hostconfig file. This file tells Mac OS X which services should be started. Mine looks like this:

% more hostconfig
# /etc/hostconfig
# This file is maintained by the system control panels

# Network configuration

# Services

By looking at this file and seeing the line WEBSERVER=-YES-, Mac OS X knows that Apache should be on. If you are observant, you'll have noticed that in the System Preferences screenshot in Figure 8-13, I have my Remote Login service turned on, as well as Personal Web Sharing. This corresponds to the -YES- flag being set for the SSHSERVER entry. We'll be back to this file in [Hack #82] as we set up the mail services for our server.

The other file of interest is Apache's main configuration file, found at /etc/httpd/httpd.conf. The default file in Jaguar is sufficient for most people. But, if you need to do something with Apache that isn't enabled by default, all you need to do is edit this file [Hack #89] and restart the web server.

?James Duncan Davidson