OK, so this article probably doesn’t have a ton of mass appeal, but since upgrading to Snow Leopard I’ve run into a major issue with the way PHP is compiled in this distribution. Overall, I’m incredibly satisfied with the way everything’s set up by default in Snow Leopard (PHP 5.3 with bundled GD, mysqlnd, etc.), but the big glaring hole in everything was the lack of Freetype support. I’ve been working on a graphing library for my day job, and as such found the need to place text in my images (strange, right?). Anyway, I finally took the plunge and figured out how to get that sucker compiled in there. Here’s what you have to do…
Basically, what we’re going to do is recompile PHP 5.3 exactly the same as it came, but with proper Freetype support. Before we get into anything, here’s a few disclaimers:
- This worked for me… it may not work for you. If you never make it to the “make install” step of the PHP build, you won’t screw up your system so you can feel relatively safe trying this. However, I can’t support you if something goes wrong. Don’t worry tho, you shouldn’t run into many issues
- The steps in this tutorial are munged together from various other sources on the web. I’d like to credit these people if possible, but I don’t have a list of all my various sources… if your stuff is on here, lemme know and I’ll make sure you’re properly credited and linked to
- You MUST have the xcode developer tools installed for any of this to work. You’ll know right away if you don’t, as “make” will be an unknown command for you. These are on your Snow Leopard install DVD if you don’t have them…
OK, moving on…
Download Source Files
We’re going to need to grab a few things to get started. So, fire up a terminal, and type in the following:
sudo mkdir /src sudo chown [your username]:staff /src cd /src mkdir pcre mkdir php cd pcre curl ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz -O cd ../php curl http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-53/php-5.3.0.tar.bz2 -O
What we’ve got is the PHP 5.3 package from Apple, and the PCRE library… everything else we need is already on our system. Let’s get compiling…
Compile PCRE
We need to compile PCRE into a local directory (not system-wide) so the PHP compiler has access to the headers it will need. This is pretty easy, just run the following:
cd /src/pcre tar -zxvf pcre-7.7.tar.gz cd pcre-7.7 ./configure --disable-shared --enable-static make && make install DESTDIR=/src/pcre/pcre-local
That’s it! Now we can start playing around with PHP
Compile PHP
Basically, what we’re going to be doing here is recompiling PHP 5.3 with the same config parameters as it was originally built with, but changing the things we need for GD and PCRE. It’s important that you follow all the steps outlined here, or you’ll get some weird compile errors. Also, if you’re a bit uncomfortable about recompiling, remember that you’re not going to mess anything up until you actually run the “make install” command. So, if the compile fails for any reason, your current PHP install isn’t going to get messed up in any way.
So, let’s get started:
cd /src/php bunzip2 php-5.3.0.tar.bz2 && tar -xvf php-5.3.0.tar cd php-5.3.0
Now, we’ll need to edit one file to fix a compile error we’ll run into. Open /src/php/php-5.3.0/ext/iconv/iconv.c in your editor of choice, and head to line 186. We need to change the line from:
“#define iconv libiconv”
to
“#define iconv iconv”
Once you’ve done that, we’re good to go. Assuming you’re still in /src/php/php-5.3.0:
MACOSX_DEPLOYMENT_TARGET=10.6 CFLAGS="-arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch x86_64 -g -Os -pipe" CXXFLAGS="-arch x86_64 -g -Os -pipe" LDFLAGS="-arch x86_64 -bind_at_load" export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET ./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=/usr/local/lib \ --with-png-dir=/usr/X11R6 \ --with-freetype-dir=/usr/X11R6 \ --with-xpm-dir=/usr/X11R6 \ --with-ldap=/usr \ --with-ldap-sasl=/usr \ --enable-mbstring \ --enable-mbregex \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --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-iconv-dir=/usr \ --with-xsl=/usr \ --with-pcre-regex=/src/pcre/pcre-local/usr/local
If everything completed without error, you’re ready to compile (again, if this fails, nothing is messed up with your current PHP installation):
export EXTRA_CFLAGS="-lresolv" make
This will take a few moments, so go grab a coffee or something If this step completed properly (i.e you get some message about “don’t forget to run make test”), you’re ready to finish up. One thing I like to do is back up my current php.ini file, but if you haven’t tweaked it (or created it for that matter), you don’t need to do this:
sudo cp /etc/php.ini /etc/php.ini.bak
Now, you can install our fresh PHP build:
sudo make install
That’s it! Now, you can copy your old php.ini file back, and restart apache:
sudo cp /etc/php.ini.bak /etc/php.ini sudo apachectl restart
If you set up a phpinfo() somewhere in your doc root, you should now see something like the following in the GD section of it:
Enjoy