Installing/Recompiling PHP on Mac OS X Snow Leopard with GD, JSON + ZIP support

GUIDE TO COMPILING A USEFUL DEVELOPMENT VERSION PHP FOR LEOPARD - INCLUDING GD GRAPHICS AND ZIP SUPPORT
After some substantial frustration, I have compiled (mainly for myself) a guide to improving on the default PHP installation that comes with Leopard. If you've landed here from Google you've no doubt already found that the standard Leopard PHP is basic, lacking some (IMHO) essential features such as GD for generating graphics and Zip support. You'll have also cursed that there is no readily available PHP package to update with, so re-compiling yourself is the only way.

// NOTE: Dec 09 - This ONLY WORKS FOR LEOPARD, NOT SNOW LEOPARD. SEE MORE RECENT POST FOR HOW I SOLVED THIS ON MY NEW MACBOOK PRO

This propect needn't be too daunting though - with this handy guide!

# Note: All commands below are meant to be run in Terminal

BEFORE YOU START
# You'll need some (free) software:
- TextWrangler
- XCode OSX Developer Tools (to give you a working C compiler - alternatively it's on the Mac OS X install CD you got with your Mac)
- MacPorts
- MySQL if you want it install it before PHP
# While you're installing all of these, get the latest version of the PHP source code (get the .tar.bz2 version)

# Stop the Apache server in System Preferences / Sharing (simply clear the checkbox next to web-sharing)


1. PREPARE THE SOURCE CODE AND CONFIGURE APACHE & MYSQL
# Double-click the PHP source code file to unpack it into your Downloads directory (assuming you saved it here). Next copy the code to /usr/local/src - if this directory doesn't already exist, create it (you may also need to create /usr/local ) - it's hidden and owned by the root user, so you'll have to do this from terminal:
cd /usr/local
sudo mkdir src

cd src
sudo mv ~/Downloads/php-5.3.0 /usr/local/src


# Once you've installed MacPorts (make sure you install OSX Developer Tools first) use it to get various useful image support libraries, JPG, PNG and FreeType (to use half-decent fonts in PHP generated images):
sudo port install jpeg
sudo port install libpng
sudo port install freetype

Note: if you get a port: command not found you'll need to create a new file in textwrangler and save it as .profile in your user's home directory, with these lines in it (then restart terminal):
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH
Note @ Dec 2009: MacPorts install now creates / adds a .profile, so all you need to do is restart Terminal

# Switch Apache into 32bit mode (by default OSX's Apache is compiled as a 3-way universal binary (effectively a package of 3 different compilations of Apache rolled together) so that it runs either on a PowerPC (PPC), 32bit i386 or 64bit architecture. So - your wizzy Mac runs Apache (and thus PHP) in 64bit mode by default. However, GD et al do not like this, as they are only intended (so far) to run as 32bit. Fortunately, sorting this out does not require a recompile of Apache - as the 32bit version is already in there :) - just do this:
# List httpd's architecture types (for your own information)
cd /usr/sbin
file httpd
or lipo httpd -info
# Suction out the 32bit i386 support into it's own binary file
sudo lipo httpd -thin i386 -output httpd.i386
# Overwrite httpd
sudo cp httpd.i386 httpd

# These directions assume you have installed the default packaged version of MySQL in the default MySQL directory /usr/local/mysql. They also assume that the mysql libraries are located at /usr/local/mysql/lib/mysql. You now have to symlink the libraries:
# Change to the mysql libraries directory
cd /usr/local/mysql/lib
# Create /usr/mysql/lib/mysql subdirectory
sudo mkdir mysql
# Symlink the libraries
sudo ln -s lib* mysql

3. RE-COMPILE PHP
# Change to the PHP source directory
cd /usr/local/src/php-5.3.0

# You may need to create or symlink gawk
sudo ln -s /usr/bin/awk gawk

# Issue the configure command to build the make file. The one below will give you GD and Zip support, but you can add other options to this. Run ./configure --help for a full set of possible features
./configure \
--prefix=/usr \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--with-apxs2=/usr/sbin/apxs \
--with-ldap=/usr \
--with-kerberos=/usr \
--enable-cli \
--with-zlib-dir=/usr \
--enable-exif \
--enable-ftp \
--enable-mbstring \
--enable-mbregex \
--enable-sockets \
--enable-zip \
--with-iodbc=/usr \
--with-curl=/usr \
--with-config-file-path=/etc \
--sysconfdir=/private/etc \
--with-mysql-sock=/var/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql=/usr/local/mysql \
--with-openssl=/usr \
--with-xmlrpc \
--with-xsl=/usr \
--without-pear \
--with-libxml-dir=/usr \
--with-iconv=/usr \
--with-pdo-mysql=/usr/local/mysql/bin/mysql_config \
--with-gd \
--with-jpeg-dir=/opt/local \
--with-png-dir=/opt/local \
--with-freetype-dir=/opt/local

# Get the C compiler you installed with the OSX Developer Tools to build the executables
make

# Install your PHP executables into their correct directories
sudo make install

# Create a php.ini file (this does not exist by default on Leopard)
# For a development environment
sudo cp php.ini-development /etc/php.ini
# For a production environment
sudo cp php.ini-production /etc/php.ini

# Use TextWrangler to open the file /etc/apache2/httpd.conf (use "Open Hidden..." from the file menu). Un-comment (by removing the hash sign) the line:
#LoadModule php5_module libexec/apache2/libphp5.so

# Reboot

# Apple OS Updates tend break things! So far this has been easy to fix however - just run through the "Switch Apache into 32bit mode" stage above and all is well ;)


Thanks go to http://www.malisphoto.com/tips/php-on-os-x.html
and http://zhuaxia.spaces.live.com/blog/cns!71787D3A37FFC48A!743.entry which offered most helpful guidance on the way to clarity

Comments

Unknown said…
make install ended with an ominous "hmmm", but it appears to be working. Thank you!
PeterW said…
When I run make I get this error:

make: *** [sapi/cli/php] Error 1

This is with php 5.2.9 and OS X 10.5.7.
Anonymous said…
I have bookmarked your page as it's been my reference for a little while now.

I am trying to upgrade to 5.3 and am running into issues. I have been goggling but can't find what it is.

The make command stops at this point and I really don't know what's wrong. I have Leopard but not the snow one :) Any clue? Thanks a lot.

libs/libphp5.bundle && cp libs/libphp5.bundle libs/libphp5.so
Undefined symbols:
"_EVP_MD_size", referenced from:
_zif_openssl_digest in openssl.o
"_EVP_CIPHER_CTX_block_size", referenced from:
_zif_openssl_seal in openssl.o
"_EVP_CIPHER_block_size", referenced from:
_zif_openssl_encrypt in openssl.o
_zif_openssl_decrypt in openssl.o
"_EVP_CIPHER_key_length", referenced from:
_zif_openssl_encrypt in openssl.o
_zif_openssl_decrypt in openssl.o
_zif_openssl_decrypt in openssl.o
"_EVP_CIPHER_iv_length", referenced from:
_zif_openssl_encrypt in openssl.o
_zif_openssl_decrypt in openssl.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [libs/libphp5.bundle] Error 1
@norwichtech said…
Olivier,
Looks like you don't have openssl installed. Try running:
sudo port install openssl

or take the --with-openssl=/usr \ line out of the configure. (if you do this, remove config.cache:
rm config.cache
make clean
)

Popular posts from this blog

Installing LAMP Apache/MySQL/PHP on Chromebook/Chromebox

Installing Ubuntu Linux on your ChromeBox/ChromeBook

Changing your Mac OSX Terminal's default text editor