Mac OS X has included a version of PHP for several years, but it has been overshadowed for a long time by the version supplied by Marc Liyanage of Entropy (www.entropy.ch). Marc's version was often ahead of the bundled version, and had support for features such as GD that were not catered for in the Mac OS X implementation.
Since the launch of Leopard, however, the Entropy version has seemed to suffer from a few problems and many people have gone back to the bundled version of PHP. There have been a number of published methods for getting GD support into this version, which makes it tolerably usable with a lot of web applications, but there are some contradictions across these published methods and none of them are really step-by-step methods.
Another problem that seems to have cropped up on a number of systems (including mine) is that PHP occasionally stops playing nicely with MySQL. This is a real pain if you are trying to run a PHP/MySQL based CMS, for example. Just as with PHP there are a few published workarounds but nothing that sets it all down in one clear description.
Of course, you could just install MAMP, but that didn't appeal to me, as I wanted to ensure control over all parts of my installations.
I have written this article as a short guide pulling together all the things you are likely to need to do in order to get a nicely working PHP/MySQL environment on your Leopard-powered Mac.
This guide covers the following topics:
This is not intended to be a universal guide. It is just what worked here for me. The usual disclaimers about backing up your system, not blaming me if your computer dies and so on, all apply.
I used the following versions of the various different pieces of software:
PHP 5.2.6
MySQL 5.0.45
Mac OS X 10.5.6
I'm not that clever... I did spend a lot of time googling and collating the hard work that a lot of other people have done. This article pulls that good stuff together, but I should include links to that good stuff:
ttp://osx.topicdesk.com/content/view/133/62/ Provides a PDF on how to install GD for PHP on Mac OS X Leopard Server.
http://www.kenior.com/macintosh/adding-gd-library-for-mac-os-x-leopard Another step-by-step for Mac OS X Leopard on GD and PHP.
You will see other links where relevant throughout the document.
One of the most frustrating aspects of many of the guides I have read while working through this was the assumption that you knew whether you were running Mac OS X as a 32-bit or 64-bit OS. It is important in the installation of GD, and not knowing what you are running is the single most common cause of failure in people running this installation. Perhaps it is just that I was raised by wolves, but I have never had this knowledge instilled in me. If you are similarly ignorant, fear not... I have discovered the secret flame of knowledge, and I am about to share it with you. Pop up to the top left of your Mac's screen, pull down the "About this Mac" menu and check what it says:

If you are running Leopard (which you presumably are, or you wouldn't be reading this, right?) and your processor says "Core 2 Duo" (as in the screen shot above) then you are running in 64-bit. If it says "Core Duo" then you are running 32-bit. Simple as that (http://forums.macrumors.com/archive/index.php/t-372907.html).
Actually, for the geekily inclined there is another way. Open up Terminal and type this command:
sysctl hw.optional 2> /dev/null | awk -F ': ' '/64/ {print $2}'
This command will return a 1 for 64-bit or a 0 for 32-bit (http://yourmacguy.wordpress.com/2008/10/08/archbits/).
Adding GD support to PHP
To check whether your installation already supports GD, you need first of all to make sure apache is running. Go to http://localhost and you should see a page like this:

This just shows you have apache running. If it isn't, you can do one of two things:
Go to the Sharing Preferences panel and turn on web sharing (this is the preferred approach)
Go to the Terminal and use the apachectl command to start the web server:
sudo apachectl start
Now open a text editor and create a file called test.php with this content:
<?php phpinfo(); ?>
Put this file in your web document root (usually /Library/WebServer/Documents). Now load this url: http://localhost/test.php:

This shows you the current version of PHP that is running, and also all of the supported configurations. The chances are if you are using the built-in PHP that you will not see GD in the list. So let's put that right.
But what if I have the entropy.ch PHP installed?
If you have the old entropy version installed you will see something like this instead of the screenshot above:

If so, then you either aren't having any problems (since this distribution supports GD, as you can see from the screenshot above) or you want to get rid of the entropy installation. If the latter, then see the section "Getting a clean slate for sorting out PHP" at the end of this article.
What we are going to do does not involve having to re-compile PHP, you will be pleased to know, but you will need to do some software compiling. So you will need to make sure that you have installed the Mac OS X Developers tools. These are on your Mac OS X install disk, or are available as a free download from the Apple Developers website. These tools provide you with some of the UNIX utilities (like gcc) that you will need here.
Important: make sure you are running as root or can sudo these commands. To be safe I started the whole thing with sudo -s to give myself a root-level shell. Assume you need at least sudo for all of these commands.
The first package to download is the jpeg library. I put all of the files I needed into a new root-level folder called SourceCache.
mkdir -p /SourceCache
cd /Sourcecache
curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar xzpf jpegsrc.v6b.tar.gz
cd jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
So I have downloaded and untarred the library and copied in a couple of files from elsewhere on my machine. I honestly don't know why: the clever folks probably do (http://osx.topicdesk.com/content/view/133/62/).
Next, I configured and compiled the library. This is where you need to know whether your machine is 64-bit or not. If it is 64-bit, use this command (all on one line):
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure --enable-shared
Important note (because this failed for me the first half-dozen times or so): in the line above, there are no spaces between "-" and "arch" anywhere. Basically any departures from the exact syntax above will probably cause the configure to fail. If you copy and paste the text from above it should be OK. But check.
If it is 32-bit, use this command:
./configure --enable-shared
Assuming there are no alarming messages the configure step will complete and put you back at the prompt. Now you just do the usual make and make install stuff:
make
mkdir -p /usr/local/include
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib
mkdir -p /usr/local/man/man1
make install
cd /SourceCache
curl -O http://www.opensource.apple.com/darwinsource/10.5.6/apache_mod_php-44.1/php-5.2.6.tar.bz2
tar xjf php-5.2.6.tar.bz2
cd /SourceCache/php-5.2.6/ext/gd
phpize
Important note: the precise location of the source code that I am downloading here is determined by the version of PHP that I am installing for. In my case it is 5.2.6. You should check the Darwin source site against your version of PHP to make sure you download the appropriate code. In practice it's fairly easy to track through the site to find the correct string to put into the curl command.
Next, we need to do the configure command, and again the actual command is dependent on whether we are in 64-bit or 32-bit land. For 64-bit:
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure --with-zlib-dir=/usr --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/X11R6 --with-freetype-dir=/usr/X11R6 --with-xpm-dir=/usr/X11R6
For 32-bit:
./configure --with-zlib-dir=/usr --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/X11R6 --with-freetypedir=/usr/X11R6 --with-xpm-dir=/usr/X11R6
Then assuming all is well (it went fine for me) then it is just a question of make and make install:
make
make install
That completes the compilation and installation stages. Next, I had to make sure that PHP was correctly configured for apache.
Configuring PHP and apache
First, assuming that you could see something when you ran test.php earlier, you have already activated PHP in your apache configuration (/etc/apache2/httpd.conf). If not, you need to do this. Find the section that contains the LoadModule statements: it will look something like this:
[...]
LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule bonjour_module libexec/apache2/mod_bonjour.so
LoadModule php5_module libexec/apache2/libphp5.so
#LoadModule fastcgi_module libexec/apache2/mod_fastcgi.so
[...]
In the case above the the PHP 5 module is enabled (though you can see the fastcgi module is disabled). Just edit the file to remove the "#" from in front of the PHP 5 line if it is there, then save the file.
Next, we have to make sure that php.ini is correctly configured. If you have not made any configuration changes to PHP then you may actually not have a php.ini at all (PHP assumes various defaults in the absence of a php.ini file). The simplest thing to do in this case is to copy the file /etc/php.ini.default to /etc/php.ini. Now open up php.ini in a text editor and look for this line:
extension_dir = "./"
Put a comment at the beginning of the line, like so:
;extension_dir = "./"
Now look in the Dynamic Extensions section for the line
extension=gd.so
If it doesn't exist, add it.
That completes the work for PHP. Just restart apache using either apachectl restart or apachectl graceful, and load the test.php url again. This time you should see something like this:

Well, you may find that everything is just fine at this stage. I didn't. When trying to load a new SilverStripe CMS installation, for example, I found that the application couldn't find MySQL. It returned error messages along the lines of "I can't find a MySQL installation" and "The root user does not have rights to create databases". A sure sign of this problem is that you can actually get into MySQL at the command line and all works well. However from PHP it works if you define the host as "127.0.0.1" but not if you define the host as "localhost". It seems that if you use the ip address 127.0.0.1 then the connection uses TCP/IP, but if you use the hostname localhost then the connection uses sockets
(http://blogs.sun.com/jbolter/entry/php_mysql_leopard_mysql_sock). That difference is the key to the problem: MySQL expects the socket location to be /var/mysql/mysql.sock, but in Mac OS X Leopard the location is /tmp/mysql.sock.
There are a number of ways to fix this, but the easiest (well, it worked for me) was to edit the /etc/php.ini file again. Look for the line:
mysql.default_socket =
and edit it to look like this:
mysql.default_socket = /tmp/mysql.sock
Similarly edit this line
mysqli.default_socket =
to look like this:
mysqli.default_socket = /tmp/mysql.sock
Restart your MySQL and apache servers and you should have a properly working Mac OS X Leopard, PHP 5 and MySQL 5 setup.
If you have arrived here it is probably because you have an existing installation of the entropy version of PHP, or have otherwise hosed your installation. Here are some things to look at and the appropriate settings to add.
First of all, visit your apache /etc/apache2/httpd.conf file. Look near the end of the LoadModule section and you will probably see something like this:
#LoadModule php5_module libexec/apache2/libphp5.so
#LoadModule fastcgi_module libexec/apache2/mod_fastcgi.so
LoadModule php5_module /usr/local/php5/libphp5.so
This tells you that the native PHP has been turned off and the entropy version is enabled. The first thing to try is simply to uncomment the line pointing to libexec/apache2/libphp.so and comment the line pointing to /usr/local/php5/libphp.so. Then do
sudo apachectl graceful
and check your test.php file again. When I did this I saw the result change from this:

to this:

Ian Piper is director and owner of Tellura Information Services, a UK-based information services consultancy. When not wrangling with the innards of Mac system software he develops content-rich web applications and information system architectures, specialising in the design of organisational taxonomies and metadata vocabularies.