Fixing PHP after an OSX Update

Apple is very protective of their OS.

So it should come as no surprise to find your carefully crafted PHP installation blown all to heck after an OS update.  Those of us that develop on custom PHP source builds have grown to dread any minor revision change (we’re the ones that can be found still running 10.5.2)

If that bouncing Software Update icon in the dock strikes fear into your heart and interrupts your dreams at night.  This is for you.

This solution requires you to have a hand-compiled version of PHP lying around somewhere on your OSX file system.

Most of this has been reverse-engineered from these excellent instructions on build PHP from source from John Malis:

Enabling, Compiling and Installing PHP on Mac OS X Leopard

There are three issues at play here.  Many OSX updates do the following things:

  • Replace the stock Apache standard modules
  • Replace your Apache binary
  • Replace your PHP command line too (aka /usr/bin/php)

So, we just need to revert those changes! Lets get started!

(Standard disclaimer: These instructions are for fairly stock versions of OSX with Intel processors, your mileage may vary)

Step 1: Backup!

# Backup the main PHP Apache HTTPD module
$ cd /usr/libexec/apache2
$ sudo cp

# Backup Apache HTTPD Server Daemon
$ cd /usr/sbin
$ sudo cp httpd httpd.uni

# Backup PHP Command Line interpreter
$ cd /usr/bin
$ sudo cp php php-apple

# Stop Apache
$ sudo apachectl stop

Step 2: Replace the PHP module and the PHP & HTTP binaries

# copy the compiled module from your source to the apache2 directory
$ cd /src/php-5.2.6/ # replace with the directory of your own php source
$ sudo cp libs/ /usr/libexec/apache2/

# Replace PHP Command Line Interface
$ sudo cp sapi/cli/php /usr/bin/php

OS X ships with a universal binary version of apache2 that we must trim in order to work with a 32-bit version of PHP. See here for more info

$ cd /usr/sbin

# Strip out the 64-bit support
# NOTE: These steps only apply if you are running an Intel processor
# with 32-bit versions of php and apache2. 
$ sudo lipo httpd -thin i386 -output httpd.i386 # Intel

# Copy to httpd
$ sudo cp httpd.i386 httpd # Intel

Thats it!

To test it, you can restart Apache2 and test to make sure that your cli has the version number you expect

# Restart Apache
$ apachectl -t
Syntax OK
$ php -v
PHP 5.2.6 (cli) (built: Jul  7 2008 23:43:57)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
$ sudo apachectl start