Over the years I have become a fan of Memcache/Memcached environments for both production and development. This caching mechanism is super stable, reliable and greatly helps reduce the expense of the PHP/MySQL driven applications that I work on daily. In production it greatly reduces server load and in a development environment, it certainly increases performance.
For this tutorial, I am just going to explain how to equip MAMP 2.0 with Memcache. There seems to be good and plentiful documentation out there for production implimentations including clustered Memcache servers but the MAMP instructions I found seemed to be a little dated. As I am sure this document will be in six months or less.
There are several prerequisite steps that must be followed before we can start working with PECL to install the PHP PECL Memache extension.
First, install Xcode Development Tools from Apple https://developer.apple.com/xcode/. It is available as a free download using the App Store program that is shipped with your Mac. We are really after the GCC program that comes along with Xcode. During the Xcode install, please be sure to select the Developer Tools Software option that includes GCC and other goodies.
Once we have GCC installed we need to make PECL active. By default you will receive the following error messages from MAMP's PECL binary:
sudo /Applications/MAMP/bin/php/php5.3.6/bin/pecl install memcache Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050 ERROR: The default config file is not a valid config file or is corrupted.
To solve this error, we need to move the pear.conf file or configure it properly. I am opting to move it to the home directory temporarily for safe keeping.
sudo mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf ~/
If you try to run the pecl again, you will find out that we are missing the autoconf dependency. Let's go ahead and solve this by downloading the source code and compiling it using GCC. I downloaded autoconf version 2.69.
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar -xf autoconf-2.69.tar.gz
sudo ./make install
Now that we have autoconf installed we might as well download and install the m4 dependency. It is also missing and PHP/PEAR/PECL will be in need of it on occasion. I chose the 1.4.16 version of m4.
curl -O http://ftp.gnu.org/gnu/m4/m4-1.4.16.tar.gz
tar -xf m4-1.4.16.tar.gz
sudo ./make install
m4 should be installed now but if you have some trouble don't worry. It's not actually a dependency for this memcache tutorial.
Now that we have the autoconf and m4 installed, we need to download the source files for PHP 5.3.6 and include it as an extension for MAMP's PHP. It is important that the version of the PHP source files match the MAMP installed PHP version.
sudo mkdir /Applications/MAMP/bin/php/php5.3.6/include
sudo curl -O http://museum.php.net/php5/php-5.3.6.tar.gz
sudo tar -xf php-5.3.6.tar.gz
sudo mv php-5.3.6 php
The end result should be in this directory: /Applications/MAMP/bin/php/php5.3.6/include/php that contains the PHP 5.3.6 source code.We need to run (configure) so that PHP recognizes our new included source files.
sudo /Applications/MAMP/bin/php/php5.3.6/include/php/configureLet's verify that MAMP's PHP has found our included source files by running:
You will receive php-config information printed out in the terminal. If you see these two lines, MAMP's PHP has found our included directory of source code:
Alright, now we should be able to finally run pecl and install the memcache extension.
sudo /Applications/MAMP/bin/php/php5.3.6/bin/pecl install memcache
After PECL is done building memcache, add the extension extension=memcache.so to your php.ini file using your favorite text editor or using a fancy shell command.
sudo emacs /Applications/MAMP/bin/php/php5.3.6/conf/php.ini
You can paste it at the top of the file just under [PHP] or the preferred place is just under the Dynamic Extensions section header:
;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;;
Restart Apache using the MAMP status window by clicking on the Stop Servers button and then clicking on the Start Servers buttons.
Let's verify that our extension has been added by clicking the Open start page button within the MAMP Status window and clicking on the PHP Info link in the top header of the MAMP start page. Memcache should now be listed on this info page.
We need to start memcached which is the daemon that the PHP memcache extension uses. This can be accomplished just by running the following command as your regular user and not sudo.
Then run this command to verify that memcached is running:
Information about Memcached should be printed in the terminal window. Memcached was installed on my OSX Lion by default but in-case it is not, we can surely download the source and compile it to get the latest and greatest.
curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -xf memcached-1.4.13.tar.gz
sudo make install
This version should compile just fine. Restart your terminal program and run:
Then check to make sure the daemon is running by using:
Your Memcached/PHP memcache MAMP environment should be complete. To use this environment with Drupal, please follow the Drupal installation instructions found at: http://drupal.org/node/1131468.
We have already taken care of steps 1 through 4 of the Drupal instructions for your MAMP environment.
Enjoy the performance of your Memcache powered MAMP Drupal development environment!
- Nate Haug's Lullabot tutorial found at http://www.lullabot.com/articles/setup-memcached-mamp-sandbox-environment that contains other techniques and installation options.
- Drupal memcache module project
- More Drupal Memcache documentation.
- Home of Memcached on the web
- PHP Community manual page for memcache: http://php.net/manual/en/book.memcache.php (I actually found this very helpful.)
These instructions are only relative to PHP version 5.3.6 because that is the PHP version currently bundled with MAMP 2.0. PHP 5.2 is also bundled with MAMP 2.0 but has been officially unsupported by the PHP Community for more than two years and Drupal versions 6.x and 7.x officially support PHP 5.3x so this tutorial will only focus on PHP 5.3.6 for now. Further these instructions will not easily transfer to WAMP the Microsoft Windows environment.