I use Homebrew for package management in OSX which is awesome, but it doesn’t provide php. The homebrew philosophy is to not provide things that Snow Leopard already does a good job of providing out of the box.

However, as a heavy php developer, you might want a more recent version of php than the latest Snow Leopard provides. Or you might want to compile in readline support which doesn’t come in the Snow Leopard version of php (at least at the time of writing).

Why do you want readline support? It gives you access to a php interpreter by typing “php -a” — which is really awesome for quickly testing things. In fact, I use it often just to get unix timestamps:

$ php -a
Interactive shell

php > echo strtotime('next friday');

You could compile your own version and put it in /usr/local. But I wanted to stick with the standard location where Snow Leopard installs php.

First, you should install readline from Homebrew: brew install readline

I’m also using Homebrew’s version of mysql. I point the mysql socket in the server and client section of /etc/my.cnf to /var/mysql/mysql.sock – “socket = /var/mysql/mysql.sock”. Therefore the php configure command points to that location for the mysql socket. You made need to change that section of the configure command if you put it elsewhere.

This php configure command uses the mysqlnd driver for mysqli which I highly recommend. It also installs the mysql extension for backwards compatibility. You might not need it.

I also compile in pdo_mysql and a bunch of other extensions that I find essential (most of which are included in the php executable provided by Snow Leopard).

You can create a “config.nice” file, put the configure script contents in there and then run the following and you are good to go:

make install

Anyway… here is the script:

#! /bin/sh
# Created by configure

'./configure' \
'--prefix=/usr' \
'--mandir=/usr/share/man' \
'--infodir=/usr/share/info' \
'--sysconfdir=/private/etc' \
'--with-apxs2=/usr/sbin/apxs' \
'--enable-fpm' \
'--with-config-file-path=/etc' \
'--with-libxml-dir=/usr' \
'--with-openssl=/usr' \
'--with-kerberos=/usr' \
'--with-zlib=/usr' \
'--enable-bcmath' \
'--with-bz2=/usr' \
'--enable-calendar' \
'--with-curl=/usr' \
'--enable-exif' \
'--enable-ftp' \
'--with-gd' \
'--with-jpeg-dir=/usr/local' \
'--with-png-dir=/usr/X11' \
'--enable-gd-native-ttf' \
'--with-ldap=/usr' \
'--with-ldap-sasl=/usr' \
'--enable-mbstring' \
'--enable-mbregex' \
'--with-mysql=mysqlnd' \
'--with-mysqli=mysqlnd' \
'--with-pdo-mysql=mysqlnd' \
'--with-mysql-sock=/var/mysql/mysql.sock' \
'--with-iodbc=/usr' \
'--enable-shmop' \
'--with-snmp=/usr' \
'--enable-soap' \
'--enable-sockets' \
'--enable-sysvmsg' \
'--enable-sysvsem' \
'--enable-sysvshm' \
'--with-xmlrpc' \
'--with-iconv-dir=/usr' \
'--with-xsl=/usr' \
'--enable-zend-multibyte' \
'--enable-zip' \
'--with-pcre-regex=/usr' \
'--with-readline=/usr/local/Cellar/readline/6.1' \

So with that, you’ll have compiled php exactly as Apple does but with your own custom php version, readline support, and mysql support.

IMPORTANT NOTE: Any time you install a new version of xcode or update the OS, it will likely overwrite your custom php binary. Fortunately, as long as you keep your php source directory intact after you compile, all you need to do is go back there and run “make install” and it restores your custom php file.

Affinitive Dev Blog

Ramblings from the dev team at Affinitive, a word of mouth and social media marketing and technology solutions pioneer and Facebook Preferred Marketing Developer (PMD). Also see @Affinitive, @RMarscher, and @BobTroia.