Memcached with PHP on Mac OS X
October 18th, 2008 by ErikNate Haug provides a great script for installing memcached along with some very detailed instructions on setting up a sandbox environment. I’m not using his MAMP sandbox, instead opting for the built in PHP / Apache install, so I needed to change a few things from his tutorial. My system is a fully updated Intel MacBook Pro running OS X 10.5.5 with the Xcode tools installed - YMMV. PHP is currently reporting version 5.2.6.
- I added my revised start script for memcached.
- The PHP version that ships with OS X doesn’t have PECL, so I downloaded the source and compiled manually.
phpize; configure; make; sudo make install - Edit to /etc/php.ini: Changed: extension_dir = /usr/lib/php/extensions/no-debug-non-zts-20060613/
- Edit to /etc/php.ini: Added: extension=memcache.so
You can skip his Apache scripts. Restart apache by restarting Web Sharing in the System Preferences.
The major changes I made from Nate’s memcached startup script were the singular instance and binding the service to localhost (127.0.0.1) only. This keeps memcached slightly more secure by only having it listen on the loopback adapter. If you need more space, just change the -m attribute to be higher; it’s measured in MB.
# /bin/sh memcached -m 1 -l 127.0.0.1 -p 11211 -d |
NOTE: As with any other service running on your system, opening a web server exposes your system to potential attack and worse. Be sure to keep production data away from your test environment. Someone at Starbucks, sharing your WiFi connection, may be surfing your development site too. Consider yourself warned!
Tags: development, guide, howto, memcached, os x, php, what works
November 21st, 2008 at 1:43 pm
Hi Erik,
Thanks very much for the post, I was trying to do exactly the same thing to avoid switching to MAMP, but wasn’t aware of the phpize command, so I ran into a wall.
I now have the problem that I followed your process exactly, got the .so generated in the same place, but PHP doesn’t like it:
PHP Startup: Unable to load dynamic library ‘/usr/lib/php/extensions/no-debug-non-zts-20060613/memcache.so’ - (null) in Unknown on line 0
Is there any chance you could email me your .so file to see if I can use it?
Thanks,
Giles
November 21st, 2008 at 1:50 pm
Giles,
Glad the post helped, sorry it didn’t outright work for you. I’ve posted mine should you, or anyone else want to download it.
http://af-design.com/downloads/memcache.so.tar.gz
Erik
November 24th, 2008 at 1:03 pm
Got the same error copying yours in. Gah! This is absolutely maddening.
Amazingly if I remove a working extension (gd.so) from that same directory, I get exactly the same error, ‘(null) in unknown on line 0′ - indicating that PHP apparently gives this error when “file not found” would be more appropriate. Now I have no idea if it’s having a problem finding / reading the file or whether there’s an error loading it up.
December 22nd, 2008 at 6:41 pm
Hi Giles,
I originally ran into the same problem as you, but have fortunately found a solution. Are you working on a 64bit machine? If so, try:
“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”
instead of just “./configure”. Chances are you are going to have to “sudo su” to root before running that command. Then, follow the rest of Erik’s guide, and it should work for you!
I found the above code at: “http://www.kenior.com/macintosh/adding-gettext-extension-for-mac-os-x-leopard”, fyi.
Matt
December 22nd, 2008 at 7:17 pm
as an addendum, make sure to change the magic double quotes to normal double quotes, as I seem to have pasted them incorrectly
December 22nd, 2008 at 9:03 pm
Great tip! I’m first-gen MBP still, so I’m only 32bit.