Memcached is an awesome memory object caching system that allows you to store highly requested data in RAM, across a network of servers, saving you from hitting your Mysql (or SimpleDB) database. This saves time and money for all of us.
This is a rather technical post, but I’ve done this three times already and always forget the steps to install and configure Memcached for PHP in Fedora, so if you don’t know what Memcached (or Fedora) is, you might want to skip this. But for those developers out there who are searching for this in Google, here are, step by step, how to enable Memcached for your box.
I did this for an Amazon EC2 instance running Fedora Core 5, so this should work for pretty much any version of Fedora or Linux that uses ‘yum’ to install packages.
The first thing you need is a library called ‘libevent’. Check if you have it by doing the following (make sure you’re sudo’d first):
updatedb
locate libevent
You should get something like this:
/usr/lib/libevent-1.3b.so.1.0.3
If not, do a yum update libevent
Next, you want to install memcached. With yum, it’s a cinch:
yum install memcached
When yum is finished, you should run it as a daemon. The command has a lot of parameters, but for now let’s keep it simple:
memcached -d -u apache
This makes memcache to run as a daemon under user ‘apache’.
Finally we need to get the Memcached extension for PHP installed. Again using yum:
yum install php-pecl-memcache
That will install the Memcache PHP extension and modify your PHP installation to allow it to talk to the daemon.
Next, we need to modify php.ini to startup with Memcache enabled. Edit /etc/php.ini and add the following line on the section titled “Dynamic Extensions”.
extension=memcache.so
We’re about done. Restart Apache:
/sbin/service httpd restart
And now put the following code on a test PHP page:
$mc = new Memcache;
$mc->addServer('localhost', 11211);
echo "Server's version: " . $mc->getVersion() ;
Running that PHP script should print something like:
Server's version: 1.2.4
If not try to check if memcache is running by doing:
ps -ef | grep memcached
You should get:
apache 9941 1 0 13:36 ? 00:00:00 memcached -d -u apache
root 10113 32255 0 14:19 pts/1 00:00:00 grep memcached
If you don’t see the daemon, check your steps and repeat.
Happy coding!
{ 9 comments }