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

December 12, 2008

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 and and PDO support.

This document assumes that the OS X developer tools (XCode 3.0) are installed and you are running BASH as your default shell. It also assumes you are installing on the client version of OS X 10.5.5 Leopard with the latest security upgrades.

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.2.6 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 your 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.2.6.tar.bz2

Install the JPEG, PNG and FREETYPE 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 already have MacPorts installed, you can obtain it from http://www.macports.org

# Get the JPEG Library
sudo port install jpeg

# Get the PNG Library
sudo port install libpng

# Get the FREETYPE Library
sudo port install freetype

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.2.6-apple.so

Run Apache HTTPD Server 32-Bit

The HTTPD server the ships with 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. The trouble is that even though PHP can be compiled 64-bit, a number of external libraries have not been converted to 64-bit, especially on MacPorts. At best, you will have to download and manually compile each library you need. In a development environment it's really best to run 32-bit, and even in a production environment it's debatable whether compiling in 64-bit support is worth the trouble. In many applications there is not going to be a significant difference in performance and running 32-bit might be a little faster.

# Backup Apache HTTPD Server Daemon
cd /usr/sbin
sudo cp httpd httpd.uni

# List httpd's architecture types
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 # Intel

# Copy to httpd
sudo cp httpd.ppc httpd # PowerPC
sudo cp httpd.i386 httpd # Intel

Keep in mind that your newly liposuctioned httpd may be overwritten by OS X updates.

Build and Install PHP (32-bit)

Use the ./configure --help command to get a complete list of build time options. These directions assume you have installed MySQL in the default MySQL directory /usr/local/mysql. They also assume that the mysql libraries are located at /usr/local/mysql/lib/mysql. If you have installed the packaged version of MySQL you will have to symlink the libraries.

Execute the following commands in Terminal if using the packaged version of MySQL:

# 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

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.2.8

# 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 the configure command to build the make file
./configure \
--prefix=/usr/local \
--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 \
--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

# 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.

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

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

Keep in mind that some Apple OS updates will overwrite this installation. So you may want copy or rename your newly compiled php module. (/usr/libexec/apache2/libphp5.so)

# Rename the module and edit httpd.conf
cd /usr/libexec/apache2
sudo mv libphp5.so libphp5.2.8-pdogd.so # Rename
sudo nano /etc/apache2/httpd.conf # Change the php module name in httpd.conf

The newly compiled php command line interface (CLI) and utilities will be installed in /usr/local/bin. Consider adding /usr/local/bin to your PATH in .profile. The original preinstalled php command line interface and utilites are located in /usr/bin. The original headers are located in /usr/include/php and original libraries are in /usr/lib/php.

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 its already running
sudo apachectl graceful-stop

# Start the HTTPD server
sudo apachectl start

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

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

Useful Links

Feedback

Please let me know if you found this page useful or have any other comments.

Comments:

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

Type the following letters in alphabetical order (Z D A B):