Installing and Configuring Memcached for PHP in Fedora

by Jorge Escobar on April 15, 2009

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!

2 Tweets

{ 1 trackback }

Simple and Clean Memcached | Poli Bou
February 26, 2010 at 5:54 pm

{ 5 comments… read them below or add one }

jules August 21, 2009 at 7:20 pm

thx so much for placing the cmd line directions here!

Reply

linuxmasterminds September 15, 2009 at 9:36 am

Very nice tutorial. You can also check some more details in the link given below.

http://howtosetup.in/component/content/article/11-how-to-setup-memcached-for-php-in-the-server.html

Reply   More from author

Poli Bou February 26, 2010 at 5:57 pm

Nice tutorial. Helpful and straight forward. I did a writeup on using memcached and linked back to your post in case anyone needed to set it up. http://www.polibou.com/2010/02/26/simple-and-clean-memcached/

Reply   More from author

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: