How to build mongodb pecl extension in 32bit for PHP 5.2 on MacOSX Snow Leaopard

Unfortunately i have to support PHP 5.2 and i also need to run my Mac in 32 bit mode as my VPN client does not support 64 bit :(

This causes quite a lot of hassle as PHP 5.2 is less and less supported by frameworks, extensions etc. It is also waste of time as i have to manually build stuff instead of using regular packages, ports etc.

Here is a quick step by step guide on how to get mongodb and PHP5.2 mongo pecl extension going on your MacOSX in 32bit mode!

NOTE: 32 bit mongodb binaries have 2GB address space limit so you wont be able to process too much on your laptop. You will still be able to code and connect to remote instances just fine.

Steps to compile mongo extension for PHP 5.2 on Mac Snow Leopard for 32 bit Snow Leopard and use it in MAMP

STEP 1 Download sources of mongo extension from PECL site: http://pecl.php.net/package/mongo

STEP 2 Extract it into your MAMP folder

tar -xvzf mongo-1.2.7.tgz
mv mongo-1.2.7 /Applications/MAMP/bin/php5.2/include/php/ext/mongo

STEP 3 Set compiler flags

MACOSX_DEPLOYMENT_TARGET=10.6
CFLAGS="-arch i386 -g -Os -pipe -no-cpp-precomp"
CCFLAGS="-arch i386 -g -Os -pipe"
CXXFLAGS="-arch i386 -g -Os -pipe"
LDFLAGS="-arch i386 -bind_at_load"
export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET

STEP 4 Build extension using the MAMP binaries and config

cd /Applications/MAMP/bin/php5.2/include/php/ext/mongo

/Applications/MAMP/bin/php5.2/bin/phpize --with-php-config=/Applications/MAMP/bin/php5.2/bin/php-config

./configure  --with-php-config=/Applications/MAMP/bin/php5.2/bin/php-config

make

STEP 5 Build extension using the MAMP binaries and config. You have to add extension=mongo.so into php.ini and then execute commands below

cp modules/mongo.so /Applications/MAMP/bin/php5.2/lib/php/extensions/no-debug-non-zts-20060613/
/Applications/MAMP/Library/bin/httpd -k stop
/Applications/MAMP/Library/bin/httpd -k start

That should be it!

Installing and running MongoDB on 32bit MacOSX

NOTE: 32 bit mongodb binaries have 2GB address space limit so you wont be able to process too much on your laptop. You will still be able to code and connect to remote instances just fine.

STEP 6 Download macosx binaries for 32bit machineshttp://www.mongodb.org/downloads

STEP 7 Create default data folder

sudo mkdir -p /data/db/
sudo chown `id -u` /data/db

STEP 8 Run server in terminal to be able to stop it at any time

cd bin
./mongod

If server is not there or php mongo extension can not connect to it you may get an exception like:

"Fatal error: Uncaught exception 'MongoConnectionException' with message 'Invalid argument' in ..."

It is a bit strange as i would expect exception to explicitly tell you what is the problem but apparently this is what happens.

Hope that helps

Comments

Post new comment

Image CAPTCHA

About the author

Artur Ejsmont

Hi, my name is Artur Ejsmont,
welcome to my blog.

I am a passionate software engineer living in Sydney and working for Yahoo! Drop me a line or leave a comment.

Follow my RSS