Oban’s techster Mark has written us up a nice long walkthrough for installing PHP 4 on the Mac – useful for those annoying pieces of software that always seem demand it! The article assumes some knowledge with the terminal…
I have a new(-ish!) toy – a shiny Mac Mini! Its a lovely piece of kit and thats all i’m going to say about the hardware as I’m 3 years late to the party and a million other bloggers have expounded upon the virtues of the Mac Mini and Apple in general.
Unfortunately until I can convince our Finance Director that a home theatre in the office is absolutely imperative, the Mac mini is going to be put to work.One of the tasks I want to give the Mac is internal development server. This means we can move away from our rather unstable, slow, inflexible hosting provider. This sounds easy enough – Leopard comes with FTP, Apache and PHP5. Unfortunately the project I am working on requires PHP4 compatibilty, and uses extensions like MySQL, GD and zlib. This means setup is made that little bit harder.
I could use XAMMP or MAMP but we already have part of the puzzle (Apache, PHP5) so it feels a little bit strange to reinstall these. In addition I don’t want to have to VNC into the box and restart Apache every time I want to switch from PHP4 to PHP5 and vice versa.
One of the plus points of our external hosting is the ease which with we can switch from PHP4 to PHP5. A simple addition of:
AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php
to the .htaccess file will switch PHP4 off and PHP5 on. (The sharp ones amongst you may recognise which provider that’s from). Uploading a .htaccess file to choose PHP version on a case by case basis seems like a much more flexible way of doing it.
So to recap what we are looking for is a setup that:
- Uses PHP4 by default
- Can switch to PHP5 using an .htaccess rule
- Has all neccessary extensions built in
We are going to have to build a CGI version of PHP4 methinks and since no-one seems to have written a definitive guide listing all the steps, the existence of this post is justified (well until a reader points me in the direction of another article doing just that!).
So without further ado onto the geek stuff:
Step 1. Download and install Xcode from Apple (or install from the OSX disc). This includes the tools that allow us to build from sources
http://developer.apple.com/tools/xcode/
Step 2. Download PHP4
curl -L http://www.php.net/get/php-4.4.9.tar.gz/from/this/mirror > php-4.4.9.tar.gz
Step 3. Decide on and download extensions. The ones I wanted for the moment were:
bzip2 – Bzip2 compression library: http://www.bzip.org/
crack – CrackLib password checking library http://sourceforge.net/projects/cracklib
expat – Expat XML parser http://expat.sourceforge.net/
freetype2 – Freetype2 software font engine http://www.freetype.org/freetype2/index.html
gettext – GetText translation tool http://www.gnu.org/software/gettext/
libjpeg – JPEG encoder and decoder http://freshmeat.net/projects/libjpeg/
libpng – PNG encoder and decoder http://www.libpng.org/pub/png/libpng.html
mcrypt – MCrypt encryption functions http://mcrypt.sourceforge.net/
mhash – MHash hash functions http://mhash.sourceforge.net/
mysql4 – MySQL4 database server http://www.mysql.com/
zlib – Zlib DELFATE (gz) compression library http://www.zlib.net/
zziplib – ZzipLib zip compression library http://zziplib.sourceforge.net/
So downloading these using curl
curl -L {Web address} > {Filename}
So in my case I did
curl -L http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz > bzip2-1.0.5.tar.gz
curl -L http://ovh.dl.sourceforge.net/sourceforge/cracklib/cracklib-2.8.13.tar.gz > cracklib-2.8.13.tar.gz
curl -L http://dfn.dl.sourceforge.net/sourceforge/expat/expat-2.0.1.tar.gz > expat-2.0.1.tar.gz
curl -L http://dfn.dl.sourceforge.net/sourceforge/freetype/freetype-2.3.7.tar.gz > freetype-2.3.7.tar.gz
curl -L http://ftp.heanet.ie/mirrors/ftp.gnu.org/gnu/gettext/gettext-0.17.tar.gz > gettext-0.17.tar.gz
curl -L http://freshmeat.net/redir/libjpeg/5665/url_tgz/jpegsrc.v6b.tar.gz > jpegsrc.v6b.tar.gz
curl -L http://kent.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.32.tar.gz > libpng-1.2.32.tar.gz
curl -L http://heanet.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz > libmcrypt-2.5.8.tar.gz
curl -L http://kent.dl.sourceforge.net/sourceforge/mhash/mhash-0.9.9.tar.gz > mhash-0.9.9.tar.gz
curl -L http://dev.mysql.com/get/Downloads/MySQL-4.1/mysql-4.1.22.tar.gz/from/http://mirrors.ukfast.co.uk/sites/ftp.mysql.com/ > mysql-4.1.22.tar.gz
curl -L http://garr.dl.sourceforge.net/sourceforge/libpng/zlib-1.2.3.tar.gz > zlib-1.2.3.tar.gz
curl -L http://heanet.dl.sourceforge.net/sourceforge/zziplib/zziplib-0.13.49.tar.bz2 > zziplib-0.13.49.tar.bz2
Step 4. Extract archives
Firstly extracting the gzipped tars:
for i in *.tar.gz; do tar -zxf “$i”; done
Then the single bzipped tar
tar -jxf zziplib-0.13.49.tar.bz2
That should leave you with a directory for each archive which we can use for the build and install processes.
Step 5. Build and install extensions.
We use the tool ‘make’ to build, this uses a file Makefile with all the compiler calls already setup. The setup of the makefile will determine what commands are available but usually the following useful ones exist:
make (builds)
make all (completely rebuilds)
make clean (removes compiled software)
make install (installs software to preconfigured destination)
If there are a lot of install settings there will usually be a script to produce the Makefile, called configure. Calling
./configure –help
will list the options available.
For bzip2:
cd bzip2-1.0.5
make
sudo make install PREFIX=/usr/local/bzip2-1.0.5
For expat:
cd expat-2.0.1
./configure –prefix=/usr/local/expat-2.0.1
Make
sudo make install
The default location for an install to is usually /usr/local however I prefer to give each one a separate directory underneath this so that I can maintain several versions of the same software:
/usr/local/<software-version>
This is done using the prefix switch. If in doubt on how to build or install each piece of software there are usually instructions in a README file:
cat README | more
Alternatively look on the software’s website.
Step 6 (Optional): Create soft links
Creating soft links allows us to upgrade software easily without having to change paths each time we rebuild dependent software. All we need to do is delete the old link and create the new one. For example creating a link for bzip2:
sudo ln -s /usr/local/bzip2-1.0.5 /usr/localtmp/bzip2
We can then use the path /usr/localtmp/bzip2 whenever we need bzip2-1.0.5. If we ever upgrade to bzip2-1.0.6 we can just relink:
sudo ln -s /usr/local/bzip2-1.0.6 /usr/localtmp/bzip2
Step 7: Build and install PHP
Once all the extensions are installed we can build and install PHP4. PHP4 builds a CGI without any options needed. To enable FastCGI use the –enable-fastcgi option. To disallow calling the binary directly for security reasons use –enable-force-cgi-redirect. The commands I used were:
cd php-4.4.9
./configure \
–prefix=/usr/local/php-4.4.9 \
–enable-force-cgi-redirect \
–enable-fastcgi \
–enable-calendar \
–enable-ftp \
–enable-magic-quotes \
–enable-sockets \
–enable-track-vars \
–enable-mbstring \
–enable-memory-limit \
–enable-exif \
–enable-mbstring \
–with-kerberos \
–with-ttf \
–with-freetype \
–with-freetype-dir=/usr/local/freetype2 \
–with-freetype2 \
–with-freetype2-dir=/usr/local/freetype2 \
–with-zlib \
–with-zlib-dir=/usr/local/zlib \
–with-zip=/usr/local/zziplib \
–with-jpeg-dir=/usr/local/libjpeg \
–with-png-dir=/usr/local/libpng \
–with-gd \
–with-curl \
–with-gettext=/usr/local/gettext \
–with-mcrypt=/usr/local/mcrypt \
–with-mhash=/usr/local/mhash \
–with-pear=/usr/local/lib/pear \
–with-crack=/usr/local/crack \
–with-bz2=/usr/local/bzip2 \
–with-mysql=/usr/local/mysql \
–with-mysql-sock=/tmp/mysql.sock
The last line is required since Leopard changed the location of the mysql socket file from /var/run/mysql/mysql.sock to /tmp/mysql.sock. We then build and install:
make
sudo make install
Step 8. Create a hard link for PHP
Once you have built PHP you have to make it accessible to Apache, by placing it into the CGI-Executables directory. Unfortunately a soft link here won’t do, so each time PHP is rebuilt the link will have to be remade.
sudo ln /usr/local/php4/bin/php /Library/WebServer/CGI-Executables/php
Step 9. Set php file handler
If you want PHP4 to be the default php handler then edit etc/apache2/other/php.conf and replace with the following:
#AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php4 .php
AddType application/x-httpd-php-source .phps
Action application/x-httpd-php4 /cgi-bin/php
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
If you want PHP5 to be the default handler then comment the second line:
AddHandler application/x-httpd-php .php
#AddHandler application/x-httpd-php4 .php
AddType application/x-httpd-php-source .phps
Action application/x-httpd-php4 /cgi-bin/php
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
Step 10. .htaccess File
This will let you change PHP version very quickly. Firstly make sure the commands in the .htaccess file will be respected by Apache by using:
AllowOverride All
In the appropriate directory section within /etc/apache2/httpd.conf:
<Directory “/Library/WebServer/Documents”>
…
AllowOverride All
…
</Directory>
Then for PHP4 add the following to your .htaccess file:
AddHandler application/x-httpd-php4 .php
For PHP 5 add the
AddHandler application/x-httpd-php .php
For the default leave the command out.
And that’s all to it! You should now have easy switching of PHP enabled on the Mac. Enjoy!
Final note:
Be careful with $_SERVER variables when you are using a CGI version of PHP. SCRIPT_FILENAME points to the CGI binary not the PHP script being run. Use PATH_TRANSLATED instead.
Cheers! Mark C.











