Please note: The article below refers to installing mcrypt on Mac OS X 10.5. If you have 10.6 Snow Leopard, please see my updated guide: Install mcrypt PHP extension on OS X Snow Leopard
Adding additional functionality to the standard Apple-supplied PHP on Mac OS X 10.5 is a little tricky if you are running a 64 bit processor such as the Intel Core 2 Duo. The reason is that any dynamic extensions that you add will need to be 64 bit, and many shared libraries by default will compile as 32 bit binaries. Trying to use a 32 bit extension with a 64 bit PHP results in the following unfriendly error message:
PHP Warning: PHP Startup: Unable to load dynamic library './mcrypt.so' - (null) in Unknown on line 0
mcrypt is a good example of a useful extension that can be added to PHP with a little bit of effort:
- Open up your Terminal.app
- To explicitly build for 64 bit architecture
export CFLAGS="-arch x86_64"
- Download libmcrypt from sourceforge http://sourceforge.net/projects/mcrypt
- Unpack the archive
cd libmcrypt
./configure --disable-shared
make
sudo make install
- download PHP 5.2.6 source from http://www.php.net/get/php-5.2.6.tar.bz2/from/a/mirror
- unpack the archive and go into the
php-5.2.6/ext/mcrypt/
dir phpize
./configure
make
sudo make install
- verify the extension is 64 bit:
file /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so
/usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so: Mach-O 64-bit bundle x86_64
To actually use the extension, you can simply create a symbolic link to it. For example:
cd ~/Sites
ln -s /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so
Example code: mcrypt.php
Drop the following code into your ~/Sites
directory to verify everything is working:
<?php if ( ! extension_loaded('mcrypt') ) { dl('mcrypt.so'); } $key = "this is a secret key"; $input = "Let us meet at 9 o'clock at the secret place."; $td = mcrypt_module_open('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, $key, $iv); $encrypted_data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); print_r($encrypted_data); ?>