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:
- (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.
- 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 - 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.
- Download PHP 5.2.10, available here
- 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:
- Type in the terminal:
cd ~/Downloads && tar xfj php-5.2.10.tar.bz2
cd php-5.2.10
nano ext/iconv/iconv.c - Skip down to line 185 (Tip: <ctrl+c> shows current line)
- Remove the
libon#define iconv libiconvso that the code reads like this:
#ifdef HAVE_LIBICONV
#define iconv iconv
#endif - Hit <ctrl+o> and to save the file
- Hit <ctrl+x> to quit nano
- Type in the terminal:
- Now, PHP is ready for compilation. We will use a configure command relatively similar to the command extracted from phpinfo() earlier:
Remember to replace shortname in /Users/shortname to your system account's shortname. If you're not sure what that is, type./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 -j2whoamiin a terminal to find out. - 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 - 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 - 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 - This error occurs if the
#define iconv libiconvis 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
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:
Comments
Submitted by Anonymous on Mon, 09/28/2009 - 17:30 Permalink
Problem with configure command solved
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!!
Submitted by firewing1 on Tue, 09/29/2009 - 23:47 Permalink
Thanks!
Thank for the heads up, I updated the post so that the configure command now uses /Users/shortname instead of ~.
Submitted by Anonymous on Fri, 02/12/2010 - 00:02 Permalink
PHP Configure command
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.
Submitted by firewing1 on Fri, 02/12/2010 - 10:31 Permalink
Input code filter
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.
Submitted by Anonymous on Thu, 10/01/2009 - 17:20 Permalink
Great tutorial
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
Submitted by Anonymous on Fri, 10/09/2009 - 12:45 Permalink
Homebrew
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
Submitted by Anonymous on Tue, 10/20/2009 - 22:47 Permalink
PHP Installation
Thanks for sharing such a good info
Submitted by Anonymous on Thu, 10/22/2009 - 03:54 Permalink
Thank you
Hi Stewart
Thanks for this tutorial, it certainly made my life a lot easier!
Si
Submitted by Anonymous on Wed, 11/11/2009 - 16:08 Permalink
Thank you
now there's no more php running on my machine because there are two or three points which are not described good enough...
Submitted by firewing1 on Thu, 11/12/2009 - 00:33 Permalink
Error messages
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.soSubmitted by Anonymous on Mon, 12/07/2009 - 23:46 Permalink
Thank you
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.
Submitted by Cash Advance loans on Sat, 12/12/2009 - 14:19 Permalink
Much needed help
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.
Submitted by Anonymous on Mon, 12/14/2009 - 20:34 Permalink
I love you!
This made my life so much easier, thank you :)
Submitted by Anonymous on Thu, 12/17/2009 - 10:16 Permalink
getting this
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?
Submitted by firewing1 on Thu, 12/17/2009 - 15:50 Permalink
Your MySQL headers may be missing
What version of mysql do you have installed?
Submitted by Anonymous on Mon, 02/01/2010 - 06:26 Permalink
cd
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 */
Submitted by firewing1 on Mon, 02/01/2010 - 10:05 Permalink
diff?
Do you have a diff available? It would make it much easier to see what you changed in the file!
Submitted by I hate PHP but ... on Thu, 01/21/2010 - 00:57 Permalink
If you get to the end: and
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
Submitted by Devy on Sun, 06/06/2010 - 18:11 Permalink
Re: iconv patch
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.
Submitted by Mat on Mon, 06/07/2010 - 11:02 Permalink
Got stuck!
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!
Submitted by firewing1 on Fri, 06/11/2010 - 16:28 Permalink
Check for errors
Check for any errors as you execute command 5.1:
cd ~/Downloads && tar xfj php-5.2.10.tar.bz2cd 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.