Tuesday, November 17, 2009

Installing Memcached on RedHat or CentOS

1) What is memcached?
Memcached is a service that allows entire database tables to be stored in memory, drastically speeding up queries to those tables and alleviating database load. E.g. In Drupal, the Memcached module allows you to store all cache tables in memory.

2) Install memcached through RPM
From a shell prompt, get the CentOS/RedHat version number:

#cat /etc/redhat-release
CentOS release 5.3 (Final)

Then get the server architecture information. This is a typical response for a 32-bit machine:

#uname -a
Linux server1.example.com 2.6.18-92.1.13.el5 #1 SMP Wed Sep 24 19:33:52 EDT 2008 i686 i686 i386 GNU/Linux

Or if you have a 64-bit machine you will probably get something like this:

#uname -a
Linux server.example.com 2.6.18-53.1.21.el5 #1 SMP Tue May 20 09:35:07 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux

Now install the RPM server that matches your architecture and CentOS version from http://dag.wieers.com/rpm/FAQ.php#B2

#rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

Now we can simply use yum (or up2date) to install Memcached:
#yum install memcached

Afterwards you can confirm memcached is up and running by calling it.
#memcached -h
memcached 1.2.6

3) Install the Memcache PECL Extension
Even though memcached is happily running on the server, it's not accessible from PHP without the PECL extension. Fortunately this is a very easy process, just use the pecl command.

#pecl install memcache

Then add the memcache extension to your php.ini file, usually at /etc/php.ini.
extension=memcache.so

And finally restart Apache so that it will pick up the new extension:
#/etc/init.d/httpd restart

Running phpinfo() on your webserver should now confirm that memcache is installed.

4) Set up Memcached as a service

#chkconfig --add memcached

Now you can start up memcached as a service.
#service memcached start

And you can confirm that memcached has fired up several instances by checking ps.

#ps -e | grep memcached
22805 ? 00:00:59 memcached
22807 ? 00:00:58 memcached
22809 ? 00:01:16 memcached
22811 ? 00:00:55 memcached
22813 ? 00:00:01 memcached
22815 ? 00:01:02 memcached
22817 ? 00:00:27 memcached
22819 ? 00:00:35 memcached
22821 ? 00:00:01 memcached
22823 ? 00:00:01 memcached
22825 ? 00:00:01 memcached

that's it! :)

No comments: