HOWTO/Enable PHP on Mac OS X

The information on this page is outdated and may not be accurate anymore, since I no longer require (or have any use for) PHP on the Mac.


When I installed Mac OS X 10.2.4 a while back, sure enough, it changed my httpd.conf. Even though it is a snap to mv httpd.conf.applesaved httpd.conf, 10.2.4 included Rendezvous support via Apple's own version of mod_rendezvous and sports a slightly modified layout, so a HOWTO seemed to be in order (especially for any newbies around).

Later on, I did this on 10.3.2 again and annotated this with the differences, and this time I'm doing it for Tiger/10.4, where it's even simpler.


The bundled PHP version on Tiger (10.4) is 4.3.10, which is fairly up-to date. In Panther (10.3.2) is 4.3.2 and In 10.2.4 It was 4.1.2, which was looking a bit stale even as it came out, but what the heck. It's bundled, and works fine for most simple scripting.

The lack of extra PHP extensions (you get mySQL, PCRE and XML support compiled in) will prompt anyone even half-serious about PHP development to go out and get Marc Liyanage's PHP port or a Fink version, but for most people the bundled PHP will be more than enough - and enabling it is a matter of editing a single configuration file.

All it takes is removing a couple of comments from the Apache configuration in Tiger and Panther (and adding a few extra lines in 10.2):

Enabling the PHP Module

We'll use the vi editor, since I'm used to it. I don't actually use anything else in Terminal, but I've added the command keys for non-vi folk (you can press Esc and type :q! to exit vi safely at any time if you get into trouble).

Since this is a protected system file, we must sudo to edit it (enter your own password when prompted):

$ sudo vi /etc/httpd/httpd.conf

Find and uncomment both the following lines (just move the cursor over the # and press x to delete a single character).

They should be near the top of the file, and even if the LoadModule directive has something different in front, the important part is uncommenting the one pertaining to PHP and its corresponding AddModule:

#LoadModule php4_module        libexec/httpd/
#AddModule mod_php4.c

...and make them look like this:

LoadModule php4_module        libexec/httpd/
AddModule mod_php4.c

Important: If you're running Tiger or Panther, you can skip the next two steps and relaunch Apache (restart Personal Web Sharing in System Preferences), since Apple has already added the relevant bits to httpd.conf.

Just type :wq to write and quit.

Adding the index document

If you're on 10.2, you need to add index.php to the default document list by modify the DirectoryIndex directive in httpd.conf to read:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.php

Just go to the appropriate line and press A (Shift-a) to append to it. Press Esc to exit edit mode.

Mapping PHP extensions to types:

Now scroll down to the bottom of the file, and just before the Include /private/etc/httpd/users statement (nice touch, separating each user permissions file), insert the following lines:

<IfModule mod_php4.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .php4
AddType application/x-httpd-php-source .phps

To insert a single blank line and enter edit mode, press o. Never mind extra blank lines - just copy the section above and paste it in. Exit and save by typing :wq.

Restarting Apache:

That's it. Now you can go over to System Preferences, Sharing, Personal Web Sharing and (re)start the web server, or type:

$ sudo /System/Library/StartupItems/Apache/Apache restart
Restarting Apache web server
/usr/sbin/apachectl restart: httpd restarted

And you're done.


Just go over to your ~/Sites folder and create a test.php file containing:

<script language="php">

And try to access http://localhost/~your_username/test.php. You should see the PHP information page listing the active configuration.


Apple's Rendezvous module has, of course, been renamed to Bonjour in Tiger.

Here are my notes for its config on 10.2: (something I've moved to a separate page):

<IfModule mod_rendezvous_apple.c>
RegisterUserSite all-users