• 5 min read
  • 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: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. Hitand to save the file
      5. Hitto 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