Installing PHP 5.2.10 on OS X 10.6 Snow Leopard

Since my update to Snow Leopard, I was pleasantly surprised to find that Apple has updated PHP to version 5.3 and also included the GD extension. While I no longer have to rebuild the extension manually like on Leopard, these changes to PHP brought around a different problem: Drupal is currently not compatible with PHP 5.3 (#360605).

I've been trying to get my local Drupal installations working, and although the patch from post #84 works pretty well (when applied to a D6 CVS checkout), Ubercart is still nonfunctional. Since I am currently building and testing Ubercart-enabled sites, my only remaining option was to downgrade to PHP 5.2.10. I wanted to have the same extensions and options that Apple's PHP 5.3 build had, so I started by viewing the output of phpinfo() and copying the configure command. To compile PHP, locally installed copies of libpng, libjpeg and pcre are required so let's started with that:

  1. (Like in the Leopard tutorial, I assume you have installed the Xcode & related developer utilities and that all downloads are saved in the "Downloads" folder in your home). Visit the libpng, libjpeg and PCRE homepages and download the latest release available for both. As of writing, the most recent releases are libjpeg 7, libpng 1.2.39 and PCRE 7.9.
  2. Compile libpng and libjpeg statically:
    cd ~/Downloads && tar xfz libpng-1.2.39.tar.gz
    cd libpng-1.2.39
    ./configure --disable-shared --enable-static
    make && make install DESTDIR=`pwd`/localinstall

    cd ~/Downloads && tar xfz jpegsrc.v7.tar.gz
    cd jpeg-7
    ./configure --disable-shared --enable-static
    make && make install DESTDIR=`pwd`/localinstall

    cd ~/Downloads && tar xfj pcre-7.9.tar.bz2
    cd pcre-7.9
    ./configure --disable-shared --enable-static
    make && make install DESTDIR=`pwd`/localinstall
  3. Since PHP will be built with MySQL support, download and install MySQL x86_64 for OS X. As of writing, the latest version is 5.1.38.
  4. Download PHP 5.2.10, available here
  5. Next, PHP needs to be prepared for compilation. As detailed in PHP bug #49267, a small change is required to get PHP to compile on Snow Leopard:
    1. Type in the terminal:
      cd ~/Downloads && tar xfj php-5.2.10.tar.bz2
      cd php-5.2.10
      nano ext/iconv/iconv.c
    2. Skip down to line 185 (Tip: <ctrl+c> shows current line)
    3. Remove the lib on #define iconv libiconv so that the code reads like this:
      #ifdef HAVE_LIBICONV
      #define iconv iconv
      #endif
    4. Hit <ctrl+o> and to save the file
    5. Hit <ctrl+x> to quit nano
  6. Now, PHP is ready for compilation. We will use a configure command relatively similar to the command extracted from phpinfo() earlier:
    ./configure '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--sysconfdir=/private/etc' '--with-apxs2=/usr/sbin/apxs' '--enable-cli' '--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=/Users/shortname/Downloads/jpeg-7/localinstall/usr/local' '--with-png-dir=/Users/shortname/Downloads/libpng-1.2.39/localinstall/usr/local' '--enable-gd-native-ttf' '--with-ldap=/usr' '--with-ldap-sasl=/usr' '--enable-mbstring' '--enable-mbregex' '--with-mysql=/usr/local/mysql/' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-mysql-sock=/tmp/mysql.sock' '--with-iodbc=/usr' '--enable-shmop' '--with-snmp=/usr' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-xmlrpc' '--with-xsl=/usr' '--with-pcre-regex=/Users/shortname/Downloads/pcre-7.9/localinstall/usr/local'

    EXTRA_CFLAGS="-lresolv" make -j2
    Remember to replace shortname in /Users/shortname to your system account's shortname. If you're not sure what that is, type whoami in a terminal to find out.
  7. Finally, backup Snow Leopard's PHP extension so that PHP 5.3 can be restored later, and copy the PHP 5.2.10 extension in its place:
    sudo mv /usr/libexec/apache2/libphp5.so /usr/libexec/apache2/libphp5.so.orig106
    sudo cp libs/libphp5.so /usr/libexec/apache2/libphp5.so
  8. The final step is to restart Apache - this can be done by toggling Web Sharing in System Preferences, or alternatively via the apachectl command:
    sudo apachectl restart
  9. That's all! Now run phpinfo() and verify that PHP 5.2.10 is up & running. While I was trying to get this working, I stumbled accross two compile errors - for the sake of completeness, I've listed them below along with the failure cause:

    1. This error occurs if EXTRA_CFLAGS="-lresolv" is not used while compiling PHP:
      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
      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"collect2: , referenced from:
            ld returned 1 exit status_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
    2. This error occurs if the #define iconv libiconv is not changed to #define iconv iconv
      Undefined symbols:
        "_libiconv", referenced from:
            __php_iconv_strlen in iconv.o
            _php_iconv_string in iconv.o
            _php_iconv_string in iconv.o
            __php_iconv_strpos in iconv.o
            __php_iconv_appendl in iconv.o
            __php_iconv_appendl in iconv.o
            _zif_iconv_substr in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _zif_iconv_mime_encode in iconv.o
            _php_iconv_stream_filter_append_bucket in iconv.o
            _php_iconv_stream_filter_append_bucket in iconv.o
      ld: symbol(s) not found
      collect2: ld returned 1 exit status
Rating: 

Comments

One problem I ran into with this was the php configure command didn't like paths starting with ~/Downloads. I just replaced every instance of ~/Downloads in the configure command with /Users/myusername/Downloads and it worked like a charm. Obviously replace "myusername" with your user name. Hope that helps someone. Otherwise, great tip! Glad to have PHP 5.2 running on Snow Leopard!!

Thank for the heads up, I updated the post so that the configure command now uses /Users/shortname instead of ~.

The configuration command that is provided has some html in it which needs to be edited. There are a few instances of and which should be removed before running in the Terminal window. Easy to miss but necessary.

Sorry about that! I switched to using Drupal's code filter module and forgot that it escapes html tags inside <code> blocks... Should be fixed now.

thank you for EXTRA_CFLAGS="-lresolv" make -j2 :)

In my case php-cli binary was installed as php.dSYM and I haв to manually move it to php

I'm maintaining some build scripts as part of the Homebrew project and my main use for PHP 5.2 is Drupal development.

I've written up a HOWTO at http://boztek.net

Thanks for sharing such a good info

Hi Stewart

Thanks for this tutorial, it certainly made my life a lot easier!

Si

now there's no more php running on my machine because there are two or three points which are not described good enough...

Which points did you have trouble with? Also, what is the error message that your machine displays when starting Apache? There should never be no PHP installed on your machine, since the procedure backs up the old php module. If something goes bad, just restore /usr/libexec/apache2/libphp5.so.orig106:
mv /usr/libexec/apache2/libphp5.so.orig106 /usr/libexec/apache2/libphp5.so

Thank you very much for this helpful tutorial. It helped me to avoid using macports php52 port just to have php52 on SL for sake of my Drupal work.
One note for those trying to get php5.2 with latest pcre 8.0. It seems its directory struc was a bit different. The configure command for PHP step gives msg at end- 'couldn't locate pcre.h in ~/Downloads/pcre-8.0/usr/local'...So, i fell-back on 7.9..everything went like charm as mentioned in this article.

I have been looking for a way to do that ever since I upgraded to snow leopard. You have saved me from my drumming headaches.

This made my life so much easier, thank you :)

getting this error...

/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c: In function ‘zm_startup_mysqli’:
/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c:637: error: ‘MYSQL_RPL_MASTER’ undeclared (first use in this function)
/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c:637: error: (Each undeclared identifier is reported only once
/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c:637: error: for each function it appears in.)
/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c:638: error: ‘MYSQL_RPL_SLAVE’ undeclared (first use in this function)
/Users/ys/Downloads/php-5.2.10/ext/mysqli/mysqli.c:639: error: ‘MYSQL_RPL_ADMIN’ undeclared (first use in this function)

do you know what's wrong?

What version of mysql do you have installed?

cd /usr/local/include/mysql
toor@com[/usr/local/include/mysql] vi mysql.h

... ...
enum mysql_protocol_type
{
MYSQL_PROTOCOL_DEFAULT, MYSQL_PROTOCOL_TCP, MYSQL_PROTOCOL_SOCKET,
MYSQL_PROTOCOL_PIPE, MYSQL_PROTOCOL_MEMORY
};
/*
There are three types of queries - the ones that have to go to
the master, the ones that go to a slave, and the adminstrative
type which must happen on the pivot connectioin
*/
enum mysql_rpl_type
{
MYSQL_RPL_MASTER, MYSQL_RPL_SLAVE, MYSQL_RPL_ADMIN
};

typedef struct character_set
{
unsigned int number; /* character set number */
unsigned int state; /* character set state */
const char *csname; /* collation name */
const char *name; /* character set name */

Do you have a diff available? It would make it much easier to see what you changed in the file!

If you get to the end: and you end up with a funky error about libpng
when you try to run:
EXTRA_CFLAGS="-lresolv" make -j2

Run this:
1. cd ~/src/
2. curl http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-53/... -O
3. tar xzf libpng-1.2.37.tar.bz2
4. cd libpng-1.2.37
5. ln -s `which glibtool` ./libtool
6. ./configure -enable-shared && make && sudo make install

and then go back to php:

1. cd ~/Downloads/php*
2. EXTRA_CFLAGS="-lresolv" make -j2

apparently, the source patch method to get libiconv to work is very hacky. It appears to be only working for PHP 5.2.10 source package. I have PHP 5.2.13, the line number of the below line is shifted and following the patch instruction here does NOT work!
#define iconv libiconv

As an alternative method, you should try to use MacPorts. (www.macports.org). Once it's installed, use port's variants to get PHP compiled with additional libraries.

$ sudo port install php52 +mysql5 +pear

Use "port -v variants php52" to find out all available variants.

I get stuck on step 5.2.

I don't see any line numbers in nano - but I think this is because the php 5.2 folder does not exist at root - should I move the folder there? Sorry I am fairly new to all this!

Check for any errors as you execute command 5.1:

cd ~/Downloads && tar xfj php-5.2.10.tar.bz2
cd php-5.2.10
nano ext/iconv/iconv.c

If nano is showing a blank page then the file doesn't exist, so something probably went wrong during the extraction or cd (Change Directory) command.