14 January 2009

Enabling PHP extensions on a Mac

So I hit my first attempt at using something not bundled in Apple's 'safe' PHP build for Mac OS X. I decided to implement memcached on my big project. I found out that memcached is pretty easy to set up and had that running in no time.

The problems start when trying to get PHP to talk 'memcached'. Windows binaries come bundled with the latest stable release of the PECL Memcache library, but Apple has decided not to bundle it into the PHP Apache module.

Then I got thinking: "Well what about other extensions that I might need?". I resolved to get this working so I knew what to do for future extensions!

I'd read so much about how Apple has disabled extensions and there's no way to make it work... blah blah blah... Then I found this brilliant tutorial by Erik Giberti.

Stupidly though I didn't follow the guidelines to the letter (I was probably distracted), but Erik provided continuing support. I Twitter'd him and he seemed more than happy to oblige. One of the good guys!

Upshot... I now have my first working self-compiled extension to PHP loaded and empowering my local development platform. That wasn't so bad, was it?

For future reference:

  1. Download latest stable extension from PECL
  2. cd Downloads/{library_name-x.x.x}/
  3. phpize
  4. ./configure (may need extra compiler options!)*
  5. make
  6. sudo make install
  7. Update PHP ini with extension=(whatever the installed file is)
You may need to find out what your extension_dir ini variable is (HINT: phpinfo!) as this is where the .so file will need to be. In point 7 just put the file name not the path.

I will no doubt be adding more libraries in the coming weeks. If I get any trouble, I will let you know. Sound off in the comments your issues with PHP extensions in Mac OS X

UPDATE 04/09:
*If you're running 64-bit architecture (and more than likely have a 64-bit build of PHP) you should run the followingat point 4 instead:
MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure

No comments: