Compiling PHP on Snow Leopard

UPDATE (Oct, 2010): These issues were resolved. You can now compile PHP 5.2 OR PHP 5.3 without following these steps

I have been wanting to do a clean install on my laptop for the last couple of months and decided that I would just wait for 10.6 to avoid having too much downtime from work. I like to compile my dev stack from source, and as expected this turned out to be the only problematic area for my upgrade. As a whole the transition was very smooth - both MySQL and Apache compiled without a hitch. However, I had a couple of issues with PHP (5.2) that took a little more work (nothing some Google fu couldn’t solve).

Both of these problems are related to shared libraries that PHP relies on:

libresolv

Running make resulted in the following error:

Undefined symbols:
  "_res_9_dn_expand", referenced from:
      _zif_dns_get_mx in dns.o
  "_res_9_search", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_check_record in dns.o
  "_res_9_dn_skipname", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_get_mx in dns.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [libs/libphp5.bundle] Error 1

Thanks to a great StackOverflow thread I was able to get around this fairly easily by editing 2 lines in the Makefile after running the configure script:

EXTRA_LIBS = -lresolv [... all of the other flags that are already here]
EXTRA_LDFLAGS = -lresolv [... all of the other flags that are already here]

libiconv

An error was also thrown in reference to libiconv. Apparently there is something strange with Apple’s included libiconv libraries. I was pretty easily able to get around this by compiling a local copy of libiconv from the source at gnu.org:

# download source, extract, enter directory
./configure --prefix=/usr/local
make
sudo make install

Then, just add –with-iconv=/usr/local to your configure options so PHP will link to your copy instead of the Leopard one.

Final configure command:

./configure --prefix=/usr/local/php52 --with-apxs2=/usr/local/apache2/bin/apxs\
 --enable-shared=all --with-zlib --with-curl --with-freetype-dir=/usr/local\
 --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/local --with-gd\
 --with-mcrypt --with-mysql=/usr/local/mysql\
 --with-mysqli=/usr/local/mysql/bin/mysql_config\
 --with-pdo-mysql=/usr/local/mysql/bin/mysql_config --with-iconv=/usr/local

Note: I did find that these issues have been resolved in the PHP 5.3.1 branch and will most likely be backported to 5.2 at some point, so make sure to check future versions before taking this route.

Hopefully this helps someone else out until these issues are resolved by either the PHP team or Apple. Do let me know if I have missed something stupid here or if there is an easier way to resolve these issues.