Enabling, Compiling and Installing PHP 5 on Mac OS X (Snow) Leopard

October 11, 2011

Contents

Introduction

These installation instructions will compile PHP with a configuration that closely matches the PHP configuration shipped with OS X, but with the addition of enabling GD, PDO and MCRYPT support. These instructions will build either a 1-way 32-bit (Leopard) or 64-bit (Snow Leopard) binary for the architecture you are compiling on, or a 32/64-bit 4-way universal binary for both PowerPC and x86 architectures. It has been reported that these instructions will also function correctly on OS X 10.7 Lion.

This document assumes that the OS X developer tools (XCode 3.13 - Leopard) or (XCode 3.2.1 - Snow Leopard) are installed, you are logged in as an administrator and using the BASH shell. It also assumes you are installing on the client version of OS X 10.5.8 Leopard or OS X 10.6 Snow Leopard with the latest security upgrades.

I have tried to make this document as concise and direct to the point as possible. It is helpful to have some knowledge of the basic UNIX commands and permissions, the GNU build system, and the infamous httpd configuration file (httpd.conf).

Where it is

The directory layout for the Apple installed version PHP on OS X is somewhat different than on UNIX systems. You will find php in the following directories:

/usr/bin
The PHP command line interface and utilities (CLI)
/usr/include/php
The headers
/usr/lib/php
The libraries
/usr/share/man/man1
The man pages
/etc/apache2 -> /private/etc/apache2
Apache httpd configuration files
/etc -> /private/etc
PHP configuration file - php.ini
/usr/libexec/apache2
Apache httpd module - libphp5.so

If you use this document to compile PHP; the command line interface, headers, libraries and man pages will be installed in /usr/local instead of /usr. Everything else remains the same.

Enable PHP

If you just need to enable the preinstalled version of PHP, execute the following in terminal and then skip down to the Start HTTPD instructions. If you compile PHP, the build scripts will enable PHP for you.

Launch Terminal and execute the following commands.

# Change to the apache configuration directory
cd /etc/apache2

# Edit httpd.conf with your favorite editor
sudo nano httpd.conf

# Find the line .....
#LoadModule php5_module libexec/apache2/libphp5.so
# and uncomment it by removing the pound sign

# Save and exit the editor

# Create php.ini (For Development)
cd ..
sudo cp php.ini.default php.ini

Download the source code and unpack into source directory

Download the PHP source code

Download the Complete Source Code (tar.bz2) from http://www.php.net/downloads.php At the time of this writing PHP version 5.3.10 is the latest release.

Create the source code directory and unpack

Launch Terminal and execute the following commands where <adminID> is your administrator ID. If you don't know your administrator id, just use the console command whoami to find out. That's assuming you are logged in as an administrator.

# Create a /usr/local/src directory if it doesn't already exist
cd /usr/local
sudo mkdir src
sudo chown <adminID> src
sudo chgrp admin src
chmod 750 src

# Change to the source directory and unpack
cd src
tar -xvjf ~/Downloads/php-5.3.10.tar.bz2

# If Safari has already attempted to open the archive after downloading it, you may have to do this instead
tar -xvf ~/Downloads/php-5.3.10.tar

# Consider changing the owner or group of the /usr/local directory to your admin id or the admin group. It will make administration easier as you won't have to do so many sudo's or run as root to install stuff in /usr/local.

# Some of the commands used in this document require the BASH shell. All versions of OS X from version 10.3 on up default to using BASH. However, if you upgraded OS X from an earlier version and imported your home directory, you may still be running TCSH. To find out what default login shell you are running and change it, go to the System Preferences Accounts pane. If the pane is locked, unlock it. Control Click on your account name and a contextual menu will appear. Click on Advanced Options. You will then be presented with a dialog where you can change the default login shell to whatever you want. You can also use the dscl utility to read and change the default login shell. Execute the following to read the directory service properties of your id:
dscl . read /Users/YourID.

# To find out what YourID is, execute the whoami command.
whoami

Install the JPEG, PNG, FREETYPE and MCRYPT Libraries

To enable GD support you must install the JPEG and PNG support libraries. Optionally, you may want to install the FREETYPE library. The easiest way to compile and install these libraries is to use MacPorts. If you don't need these libraries just skip these instructions and then modify the configure command in the build section of this document and delete the PHP options you don't need. If you don't already have MacPorts installed, you can obtain it from http://www.macports.org

On OS X 10.5 Lepard, unless you manually configure macports to compile 64-bit universal binaries it will by default compile 1-way 32-bit binaries. If you are running on OS X 10.6 Snow Leopard, macports will generate 64-bit binaries by default. You must edit macports.conf to compile 4-way universal binaries. If you want to shorten compile times, you may want to leave out the architectures you don't need. Also, on Snow Leopard you can leave out the +universal option if compiling a 64-bit one-way binary.

# Edit macports.conf
cd /opt/local/etc/macports
sudo nano macports.conf
# Change the line ....
universal_archs ppc i386
# to ...
universal_archs ppc ppc64 i386 x86_64

# Get the JPEG Library
sudo port install jpeg +universal

# Get the PNG Library
sudo port install libpng +universal

# Get the FREETYPE Library
sudo port install freetype +universal

# Get the MCRYPT Library
sudo port install mcrypt +universal

# If you are forced to use macports through an institutional http proxy,
# execute the following commands.

sudo bash
export http_proxy=proxyhostname:portnumber
# (e.g. export http_proxy=wwwcache.uni.ac.uk:8080)
export HTTP_PROXY="$http_proxy"
# (you might want to echo $HTTP_PROXY now to check this gives wwwcache...)
port install jpeg +universal
# (etc.)

Install the ICONV Library

The ICONV library can be downloaded from http://www.gnu.org/software/libiconv. Alternatively, you can now install this library with macports.

# Unpack
cd /usr/local/src/
tar -xvzf ~/Downloads/libiconv-1.14.tar.gz

# Configure
cd /usr/local/src/libiconv-1.14
CFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' CCFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' CXXFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' ./configure

# Make
make

# Install
sudo make install

Backup the existing PHP installation

These directions will overwrite the PHP apache module included with Leopard, so you will first need to back up the existing module.

# Backup the main PHP Apache HTTPD module
cd /usr/libexec/apache2
sudo cp libphp5.so libphp5-apple.so

Run Apache HTTPD Server 32-Bit (Leopard Only)

Skip this section if:

Use the arch command to find out what architecture you are running on.

The HTTPD server that ships with OS X 10.5 Leopard is compiled as a 4-way universal binary and will run 64-bit on 64-bit boxes (G5, Core 2 Duo, Xeon). If you run HTTPD 64-bit, it's modules must also be 64-bit. To run a 32-bit HTTPD module on a 64-bit box, you must first strip out the 64-bit support from HTTPD.

# Create a /usr/local/sbin directory if it doesn't exist
sudo mkdir /usr/local/sbin

# List httpd's architecture types
cd /usr/sbin
file httpd

# Strip out the 64-bit support
sudo lipo httpd -thin ppc7400 -output httpd.ppc # PowerPC
sudo lipo httpd -thin i386 -output httpd.i386 # x86

# Stitch them together into a single 2-way 32-bit binary
sudo lipo httpd.ppc httpd.i386 -create -output /usr/local/sbin/httpd

# Edit org.apache.httpd startup plist
cd /System/Library/LaunchDaemons
sudo nano org.apache.httpd.plist

# Change the line:
<string>/usr/sbin/httpd</string>
to ..
<string>/usr/local/sbin/httpd</string>

Build and Install PHP

Use the ./configure --help command to get a complete list of build time options.

Any external libraries must be of the same architecture type as the architecture type you are building PHP for. For example, don't try to build a 32-bit PHP with a 64-bit JPEG library. You may want to modify the configure command below to compile PHP for only the architectures you need.

This document assumes you are using either a compiled version of MySQL or MySQL that is included with Leopard Server. If you are using the packaged version (dmg) of MySQL, you may want to set --with-mysql-sock=/tmp/mysql.sock in the configure command below.

Execute the following commands in Terminal to build PHP:

The OS X developer tools must be installed.

# Change to the PHP source directory
cd /usr/local/src/php-5.3.10

# You may need to create or symlink gawk
# On my machine, gawk mysteriously disappeared and configure depends on it

sudo ln -s /usr/bin/awk gawk

# Issue this configure command to build the make file to produce a 32-bit (Leopard) or 64-bit (Snow Leopard) one way binary for the architecture you are compiling on
./configure \
--prefix=/usr/local \
--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 \
--with-iodbc=/usr \
--with-curl=/usr \
--with-config-file-path=/etc \
--sysconfdir=/private/etc \
--with-mysql-sock=/var/mysql/mysql.sock \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl=/usr \
--with-xmlrpc \
--with-xsl=/usr \
--without-pear \
--with-libxml-dir=/usr \
--with-iconv-dir=/usr/local \
--with-gd \
--with-jpeg-dir=/opt/local \
--with-png-dir=/opt/local \
--with-freetype-dir=/opt/local \
--with-mcrypt=/opt/local

# Issue this configure command to build the make file to produce a 32/64-bit 4-way Universal binary
# The 4-Way Universal Binary build takes a long time.
# You may want edit this command and delete the architectures you don't need.
CFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' CCFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' CXXFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64' ./configure \
--prefix=/usr/local \
--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 \
--with-iodbc=/usr \
--with-curl=/usr \
--with-config-file-path=/etc \
--sysconfdir=/private/etc \
--with-mysql-sock=/var/mysql/mysql.sock \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl=/usr \
--with-xmlrpc \
--with-xsl=/usr \
--without-pear \
--with-libxml-dir=/usr \
--with-iconv-dir=/usr/local \
--with-gd \
--with-jpeg-dir=/opt/local \
--with-png-dir=/opt/local \
--with-freetype-dir=/opt/local \
--with-mcrypt=/opt/local

# Patch the make file per http://bugs.php.net/bug.php?id=48195
# Need to do this only on 64-bit builds
nano Makefile
# Change the line ....
$(CC) $(MH_BUNDLE_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) $(PHP_GLOBAL_OBJS:.lo=.o) $(PHP_SAPI_OBJS:.lo=.o) $(PHP_FRAMEWORKS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@ && cp $@ libs/libphp$(PHP_MAJOR_VERSION).so
# to ...
$(CC) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) $(PHP_GLOBAL_OBJS:.lo=.o) $(PHP_SAPI_OBJS:.lo=.o) $(PHP_FRAMEWORKS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) $(MH_BUNDLE_FLAGS) -o $@ && cp $@ libs/libphp$(PHP_MAJOR_VERSION).so

# Build the executables
make

If you receive the error:
Undefined symbols:
"_EVP_CIPHER_CTX_block_size", referenced from:
_zif_openssl_seal in openssl.o
ld: symbol(s) not found
after compiling and linking PHP, it indicates a conflict with other MacPorts libraries. When you link php with a MacPorts library, it starts searching for all libraries in MacPorts, some of which may conflict with other libraries on your system. You may have to isolate the MacPorts libraries that you need to compile php from any other MacPorts libraries or applications that you may have already installed with MacPorts. One way to do this is to move your old MacPorts directory (usually /opt) to a new directory, reinstall macports and rerun the port installs. Some have also found using --with-openssl=shared,/opt/local option in the configure command will also fix this problem. You will have to download and compile openssl with macports for this option to work.

# Copy the installation to the OS X PHP directories
sudo make install

# Copy the configuration file php.ini to /etc
sudo cp php.ini-development /etc/php.ini # For a development environment
sudo cp php.ini-production /etc/php.ini # For a production environment

# Rename the module and edit httpd.conf
cd /usr/libexec/apache2
sudo mv libphp5.so libphp5.3.10.so # Rename
cd /etc/apache2
sudo nano httpd.conf

# change the line:
LoadModule php5_module libexec/apache2/libphp5.so
# to ..
LoadModule php5_module libexec/apache2/libphp5.3.10.so

# The newly compiled php Command Line Interface (CLI), utilities, includes, as well as the man pages are installed in /usr/local.

# Add /usr/local/bin to your PATH in .profile.
nano ~/.profile

# Your PATH should look something like:
export PATH=/usr/local/bin:$PATH:/Developer/Tools:
/usr/local/mysql/bin:/opt/local/bin:
/opt/local/sbin

Start HTTPD and Display the PHP Configuration

Use the System Preferences Sharing panel or the apachectl utility to stop and restart the HTTPD server.

Execute the following commands in Terminal:

# Stop the HTTPD server if it's already running
sudo apachectl graceful-stop

# Start the HTTPD server
sudo apachectl start

# Create Display PHP Configuration Script
sudo echo "<?php phpinfo(); ?>" > /Library/WebServer/Documents/PHPInfo.php

Go to http://localhost/PHPInfo.php in your browser.

Troubleshooting

If things are not working as expected, first use the console utility to check the console messages and httpd error log at /var/log/apache2/error_log.

Also, check /etc/apache2/httpd.conf for any spelling errors. Look closely at the line that loads the php module. It should look something like this:
LoadModule php5_module libexec/apache2/libphp5.3.10.so.
Make sure httpd.conf is not attempting to load the PHP module twice, this can happen when compiling/installing a new version of PHP and the old version PHP has been renamed something different than the default.

Some have reported problems connecting with MySQL. Depending on whether you are running Leopard Server, a packaged version of MySQL, or a user compiled version of MySQL, mysql.sock can get created in either /var/mysql or /tmp. You may have to make the following changes in /etc/php.ini:
mysql.default_socket = /var/mysql/mysql.sock (or /tmp/mysql.sock)
mysqli.default_socket = /var/mysql/mysql.sock (or /tmp/mysql.sock)
pdo_mysql.default_socket = /var/mysql/mysql.sock (or /tmp/mysql.sock)
Another way of handling this issue is create a symlink to the appropriate socket location.

Stop and then start the httpd server to force httpd to read httpd.conf. If you use the apachectl restart command, httpd.conf will not be read.

Useful Links

User Comments

A couple of additional steps I had to take to get it to work correctly. My build machine is running pretty stock 10.6.5, compiling php v 5.3.5.
I had to update MySQL from 5.5.0 to 5.5.8. There is a configuration error looking for 'mysql_close' that would fail. The 10.6 binary from Oracle worked fine.
Before you build;
Add -lresolv to EXTRA_LIBS in the Makefile to avoid unresolved symbol issues with dns.o. See http://bugs.php.net/49332 and http://bugs.php.net/bug.php?id=49267.
This problem appears to be fixed in PHP 5.3.10.

Confirmed steps above still need to be taken with PHP 5.3.6 / OS X 10.6.7 / xcode 4.02.

Sponsors

Did you find this page useful? Did it save you some time? Please visit our sponsor Cyrilla's Artistic Crochet and purchase a fine handcrafted product. You can find beautiful products for babies and children, bag holders to recycle your plastic bags, cell phone pouches, finger puppets, faux food, kitchen towels, scarves and wine gift bags. All products are handmade in the U.S.A.

Feedback

Please let me know if you found this page useful or have any other comments. Your feedback will help make this document more accurate and complete.

Comments:

Your Email (Optional):
Include an email address if you would like a reply.