Note: The following entry refers to Apache 1.3. Much of the content of this post still applies, but if you are running Apache 2, please refer to the updates.
Mac OS X comes with the robust, industry standard web server Apache preinstalled. When you turn on "Personal Web Sharing" in the System Preferences Sharing pane, you are actually enabling the Apache server which runs on many Unix- and Linux-based web servers across the internet.
Apache's startup configuration is stored in /etc/httpd/httpd.conf Take a good look at this file. If you plan on doing any real web serving, you'll want to get familiar with the format and many of the directives.
Before editing this file, *make a backup copy*. You will have to use the Terminal to edit this file and have super user (root) access to make changes and restart the apache server. Any time you edit /etc/httpd/httpd.conf, you can restart Apache with the following command:
# apachectl graceful
Alternatively, you can use the "sudo" command:
$ sudo apachectl graceful
You will be prompted for the "user" password who needs to have admin access in order to be able to execute the sudo command.
Web developers usually build and test their web sites with a web server running on the local host. This means having multiple sites under a single document root. On Mac OS X, the default location for DocumentRoot (specified in /etc/httpd/httpd.conf) is /Library/WebServer/Documents. If you're building multiple web sites, the best way to set this up is to configure virtual hosts. Let's take an example using two fictitious web sites "example1.com" and "example2.com".
First off, add the following to /etc/httpd/httpd.conf
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot /Library/WebServer/Documents/example1.com
ServerName example1.local
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot /Library/WebServer/Documents/example2.com
ServerName example2.local
</VirtualHost>
The first VirtualHost allows you to continue using "localhost" as part of a URL. Without this entry, localhost will fail to resolve to 127.0.0.1.
We've created virtual servers called "example1.local" and "example2.local", but your domain name server will not be able to resolve these into IP addresses, so you must add the following line to /etc/hosts
127.0.0.1 example1.local example2.local
Alternatively, you can just add the entries to the end of the localhost line. Lastly, restart the Apache server and you should be good to go.
$ sudo apachectl graceful
Check out my Analog page for a primer on setting up this excellent utility to help you analyze your web server's access log.
Information on setting up a secure ssl (https) server can be found at MacOSXHints or Apple's Developer Site.
There are some differences in the two procedures above, but they are essentially the same. Basically it involves enabling mod_ssl in Apache and setting up a self-signed Certificate of Authority using openssl which comes with Mac OS X.
So far I haven't got it working, but I'll update this page when it is.
Apache 2 represents a significant update to the venerable web server, and I highly recommend it. If you are installing or configuring Apache 2 under Mac OS X or Linux, please note the following changes:
Apache 2 is installed under /usr/local/apache2. The controller is still called apachectl, but for clarity and consistency, I like to create a symbolic link called apache2ctl which points to the apache2 controller script.
apachectl is now apache2ctl. Same arguments (start/stop/restart).
/etc/httpd/httpd.conf
has become
/etc/apache2/apache2.conf
You will also want to have a look at the following files which contain directory-specific directives:
/etc/apache2/sites-available/default
/etc/apache2/sites-enabled/000-default
I've also posted detailed instructions on installing Apache 2 on Mac OS X 10.4 (Tiger)