Installing PHP 5.2.8 on OSX Leopard 10.5.6

Because I travel a lot by train, it’s easy to have a local development environment with me. Because OSX Leopard contains Apache2 and PHP 5.2.6, I installed MySQL, ZF, PEAR, setup the vhost conf for apache and added the path to the ZF and PEAR libraries to /etc/php.ini. This seemed to work fine for a day or two, but after that I needed to use the PDO_MySQL library. This didn’t really work… The fact is that PDO_SQLite and PDO_SQLite2 are compiled along the installed PHP library, but PDO_MySQL isn’t… Big FAIL!.

So, we’ll have to setup a new PHP/Apache2 combo…..

Okay let’s start:

1. Install the Apple Developer tools

If you haven’t done it already, install the Developer Tools(XCode 3) from the OSX DVD

2. Install and update MacPorts

If you haven’t installed MacPorts on your system, do so by downloading it from the MacPorts website and running the installer. Now open a terminal window and update MacPorts:

sudo port selfupdate
sudo port sync

3. Install apache2

Create the following directory and symlink for the correct use of MySQL (the install searches there for libraries):

sudo mkdir /usr/local/mysql/lib/mysql
sudo ln -sf /usr/local/mysql/lib/lib* /usr/local/mysql/lib/mysql/

Install Apache2:

sudo port install apache2

Let the apache2 process be autostarted by the system:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Move the old Apache stuff out of the way:

sudo mv /usr/sbin/apachectl /usr/sbin/apachectl-leopard
sudo ln -s /opt/local/apache2/bin/apachectl /usr/local/bin/apachectl

Copy the sample conf file and make it the default

sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

Now you can modify your apache settings to your liking, but remember to use only the ones in the /opt/local/apache2/conf directory; the old configs(/etc/apache2/conf) are not used anymore.

4. Install the additional stuff needed

Then make sure we have everything installed we want in there; I want: GD libraries, iconv, PDO_MySQL, curl

sudo port install jpeg
sudo port install libpng
sudo port install freetype
sudo port install libmcrypt
sudo port install tidy
sudo port install libiconv

Rename the default iconv.h because it generates errors while compiling PHP

sudo mv /usr/include/iconv.h /usr/include/iconv.h.leo_orig
sudo ln -s /opt/local/include/iconv.h /usr/include/iconv.h

5. Download, configure and install PHP

Download the php5.2.8.tar.gz file from the PHP website. go to your download directory and run

tar xvzf php-5.2.8.tar.gz 

Then move this folder to the /opt/local/php-5.2.8 folder

sudo mv php-5.2.8 /opt/local/

Then create a symlink that you can compile against:

sudo ln -s /opt/local/php-5.2.8 /opt/local/php

Go into the newly created ‘folder’:

cd /opt/local/php/

Now configure PHP:

'./configure' \
'--prefix=/opt/local/php' \
'--with-apxs2=/opt/local/apache2/bin/apxs' \
'--with-xsl=/usr' \
'--with-tidy=/opt/local' \
'--with-ldap=/usr' \
'--with-kerberos=/usr' \
'--enable-mbregex' \
'--enable-ftp' \
'--with-iodbc=/usr' \
'--with-curl=/usr' \
'--enable-mbstring' \
'--with-gd' \
'--with-jpeg-dir=/opt/local' \
'--with-png-dir=/opt/local' \
'--with-zlib-dir' \
'--enable-sockets' \
'--enable-exif' \
'--with-mcrypt=/opt/local' \
'--enable-soap' \
'--with-mysql=/usr/local/mysql' \
'--with-mysqli=/usr/local/mysql/bin/mysql_config' \
'--with-pdo-mysql=/usr/local/mysql/bin/mysql_config' \
'--with-mysql-sock=/tmp/mysql.sock' \
'--with-freetype-dir=/opt/local' \
'--with-openssl=/opt/local' \
'--with-iconv=/usr' \
'--with-libxml-dir=/usr' \
'--with-xmlrpc' \
'--enable-cli'

Then make and install php:

sudo make
sudo make install

And rename the old PHP and create a symlink to the new binary:

sudo mv /usr/bin/php /usr/bin/php-leopard
sudo ln -s /opt/local/php/bin/php /usr/bin/php

copy the new php.ini file and edit it to your liking:

sudo cp php.ini-dist lib/php.ini

Note: The new php.ini file in use is the one located at: /opt/local/php/lib/php.ini

6. Restart the webserver

Up until now the old webserver was running. Shut it down and then start the new apache2:

sudo apachectl-leopard stop
sudo apachectl start

If all went well, Apache is running and PHP scripts get executed. Please check the installed features by calling a page with the following code on it, to ensure everything is installed.

In the next post we will add xDebug to the stack and later on the debugging environment is setup for different IDEs.

Part2: Installing xDebug on OSX