How to Install Memcached on CentOS 5.6May 7, 2011

Memcached is a general-purpose distributed memory caching system that is used to speed up dynamic database-driven websites by caching data in RAM to reduce the number of times an external data source must be read. Memcached provide a giant hash table (which can be distributed across multiple machines), once table is full new inserts will purge old data in least recently used order.

Memcached uses a client-server architecture. The Servers maintain a key-value (keys can be up to 250 bytes and values can be at most 1 MB in size) associative array and the clients populate and query to this array.

Memcached can be installed on Unix, Windows and MacOS and currently is in use by few of the very heavy traffic sites such as YouTube, Facebook, Twitter and reddit etc.

In this post I will show you how to install memcached on CentOS 5.6 with apache 2.2.1 and PHP 5.3.

Download and Unpack required RPMs

Login as root and go to directory /usr/src and download the following RPMs:

$ cd /usr/src
$ wget http://www.monkey.org/~provos/libevent-2.0.10-stable.tar.gz
$ tar –zxvf libevent-2.0.10-stable.tar.gz
$ wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
$ tar -zxvf memcached-1.4.5.tar.gz
$ wget http://pecl.php.net/get/memcache-2.2.6.tgz
$ tar -zxvf memcache-2.2.6.tgz

Install Libevent

$ cd /usr/src/libevent-2.0.10-stable
$ ./configure
$ make
$ make install

Install Memcached Server

$ cd /usr/src/memcached-1.4.5
$ ./configure
$ make
$ make install

Check if installed correctly (For 64 bit machine)

To check if memcached is installed successfully type the following command (If following command produce an error then run the second command as well)

$ memcached –h
$ ln –s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

Start Memcached Server

$ memcached -d -u nobody -m 1024 -l 127.0.0.1 -p 11000

Install Memcache Client

$ cd /usr/src/memcache-2.2.6
$ phpize
$ ./configure
$ make
$ make install

Restart Apache

$ service httpd restart

Check if everything is fine

<?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11000);
    $user_object = $memcache->get('user');
    if (! $user_object) {
        $user_object = get_userinfo();
        $memcache->set('user', $user_object, false, 30);
        echo 'Stored data in the cache (data will expire in 30 seconds)';
    }
    else {
        echo 'Data retrieved from cache';
    }
?>


blog comments powered by Disqus
Me Hi! My name is Zeeshan Muhammad Khan (nick name Shan) and I am a software engineer, database developer, web developer, programming geek, statistics geek, mathematics geek, system analyst and maintainer of this site. read more

Web Shelf