Server Codex


Software, Hardware, Tutorials, News and Reviews

Useful Apache Tweaks

Pre-requisite reading: Mac Serving With Apache, PHP, and MySQL

“More command line gruntwork? Didn’t I get a Mac so I could avoid this stuff?”

The great thing about Mac OS X is that server software written for UNIX systems don’t need to be re-written for the Mac, which of course means we now have access to a lot of great server software that before was out of our reach. So, yes, entering commands into the Terminal isn’t as handy as ticking off checkboxes in a nice GUI app’s preferences. But as with all our tutorials, the following instructions are written such that a monkey could follow them. (Let us know if you have trouble following our tutorials. We’ll get a dumber test monkey.)

Apache configuration in Jaguar is already well covered via other sources, so we’re just going to focus on a few specific needs: allowing per-folder configuration overrides and adding new file extensions to the DirectoryIndex attribute. While that may sound complicated, the changes are actually quite simple and will make things easier for us in the future.

Apache configuration settings are stored in the httpd.conf file, which is located in /etc/httpd. Before we make any changes, the first step is to back up the httpd.conf file to our home directory, so we can always keep a copy of Apache’s factory settings. Launch the Terminal application and copy and paste the following lines, hitting return after each line:

cd /etc/httpd
cp httpd.conf ~/

Go to the Finder and duplicate the httpd.conf file that we just copied to the Home folder. Store the duplicate in a safe place, in case you need to restore it later. Using TextEdit or your preferred text editor, open the httpd.conf file that should still be sitting your Home folder. About a third of the way into the httpd.conf file, you should see a block of text that says:

# This controls which options the .htaccess files in directories can
# override. Can also be "All", or any combination of "Options", "FileInfo",
# "AuthConfig", and "Limit"
AllowOverride None

Changing the word None above to All will allow us to make folder-specific changes to the Apache configuration. Once you’ve done that, scroll down a little and find the following block:

# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
<IfModule mod_dir.c>
DirectoryIndex index.html

Add index.php and default.php to the end of the DirectoryIndex line, so it reads:

# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
<IfModule mod_dir.c>
DirectoryIndex index.html index.php default.php

This change tells Apache that if it doesn’t find an index.html file, then it should look for index.php and default.php — in that order. This will become useful later on when we install web applications written in PHP.

Save and close the document. Now we’re going to copy our modified file back to its original location. Switch to the Terminal application, copy and paste the following line, hit return, and enter your password when prompted (which you will follow with another return, of course):

sudo cp ~/httpd.conf /etc/httpd/

Stop and re-start Personal Web Sharing (i.e., Apache) in System Preferences > Sharing. You should now be able to override Apache’s settings on a folder-by-folder basis via .htaccess files, and web folders with index.php or default.php files will have these files loaded automatically when entered in a browser.

Again, while perhaps not earth-shattering, mind-numbing excitement, these simple changes will eliminate a lot of hassles that can crop up when installing web applications.

Leave a Comment