Install memcache for drupal on centos

Posted by: 
Dominique De Cooman

In terminal:

First install libevent:

cd /usr/local/src/
wget <a href="http://monkey.org/~provos/libevent-1.3e.tar.gz
tar">http://monkey.org/~provos/libevent-1.3e.tar.gz
tar</a> zxpfv libevent*
cd libevent*
./configure
make install

Download and install memcached:

cd /usr/local/src/
wget <a href="http://www.danga.com/memcached/dist/memcached-1.2.4.tar.gz
tar">http://www.danga.com/memcached/dist/memcached-1.2.4.tar.gz
tar</a> zxpfv memcached*
cd memcached*
./configure
make install

Now we'll start a daemon to test

./memcached -u root -d -m 48 -l 10.10.102.6 -p 11211
 
//change the ip in your ip
//change the user root in an appropriate user for memcache
//To test if it is running type memcached -u root -vv
//Now kill it
killall memcached

If you get the error:./memcached: error while loading shared libraries: libevent-1.3e.so.1: cannot open shared object file: No such file or directory
Try:

ln -s /usr/local/lib/libevent-1.3e.so.1 /lib64/

If you still have the same error:
Copy all files in /usr/local/lib to /usr/lib

Now you should have a memcached server up and running.

We still need to make it work with php.
Install libmemcached:

cd /etc/yum.repos.d/
wget <a href="http://rpms.famillecollet.com/remi-enterprise.repo
wget">http://rpms.famillecollet.com/remi-enterprise.repo
wget</a> <a href="http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
wget">http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarc...</a> <a href="http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm">http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm</a> -Uvh remi-release-5*.rpm epel-release-5*.rpm
 
yum --enablerepo=remi install libmemcached\*

Install pecl memcache:

pecl install memcache 

//or you could build it yourself
pecl download memcached
tar zxvf memcached-1.0.0.tgz (or whatever version downloads)
cd memcached-1.0.0
phpize
./configure --with-libmemcached-dir=/opt/local
make
make install

Add these lines to the php.ini file

extension=memcache.so
memcache.hash_strategy="consistent"

Restart the server.

/etc/init.d/httpd restart

To test if the php module is enabled do a php_info() and check if memcache settings table is present.

In drupal enable the module and put following in the settings file:

$conf = array(
   // The path to wherever memcache.inc is. The easiest is to simply point it
   // to the copy in your module's directory.
   'cache_inc' => './sites/all/modules/memcache/memcache.inc',
   // or
   // 'cache_inc' => './sites/all/modules/memcache/memcache.db.inc',
);

Edit :
More appropriate caching configuration for a drupal site. I defined six bins.

$conf['cache_inc'] = 'sites/all/modules/contrib/memcache/memcache.inc';
$conf['memcache_servers'] = array(
    'localhost:11211' => 'default',
    'localhost:11212' => 'page',
    'localhost:11213' => 'filter',
    'localhost:11214' => 'content',
    'localhost:11215' => 'block',
    'localhost:11216' => 'views',
    'localhost:11217' => 'form',//read update below why the form cache needs its own bin
   );
$conf['memcache_bins'] = array(
     'cache'        => 'default',
 
     'cache_menu'   => 'default',
     'cache_apachesolr'   => 'default',
     'cache_form'   => 'form',//read update below why the form cache needs its own bin
     'cache_glue'   => 'default',
     'cache_rules'   => 'default',
     'cache_mollom'   => 'default',
     'cache_update'   => 'default',
     'cache_usermenu'   => 'default',
     'cache_views'   => 'views',
     'cache_views_data'   => 'views',
     'cache_page'   => 'page',
     'cache_filter' => 'filter',
     'cache_content' => 'content',
     'cache_block'   => 'block',
);

This needs another initiating of the bins. Also a script is needed to start and stop the bins.
To start the memcached daemons we use this script. (Would be done instead of initiating the daemon in the command line above)
So created a file in /etc/init.d

cd /etc/init.d/
vi memcached
//type 'i' to edit
//paste this
#!/bin/bash
#source is the path to your memchached installation (if you have another version adapt)
source="/usr/local/src/memcached-1.2.4/memcached"
 
RETVAL=0
prog="memcached"
desc="Distributed memory caching"
 
start() {
        echo -n $"Starting $desc ($prog): "
 
 
        $source -d -u root -m 64  -p 11211
        $source -d -u root -m 320 -p 11212
        $source -d -u root -m 128 -p 11213
        $source -d -u root -m 320 -p 11214
        $source -d -u root -m 128 -p 11215
        $source -d -u root -m 64 -p 11216
        $source -d -u root -m 64 -p 11217
 
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
 
stop() {
        echo -n $"Shutting down $desc ($prog): "
        killall $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        return $RETVAL
}
 
restart() {
        stop
        start
}
 
reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
 condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac
 
exit $RETVAL
 
 
//press esc type :wq to save and exit
//make the file executable
chmod u+x memcached
 
//to start
service memcache start
 
//to stop
service memcache stop

Go to admin/settings/memcache and enable show statistics to monitor the process.
Go to reports memcached to see statistics of your bins.
You should monitor the process and adjust the bins to your needs. You could give it more or less memory. A balanced size is the most performant.

UPDATE:
When using memcached you need a seperate bin for your forms. (http://drupal.org/node/500646)
I corrected the above code so it works.

Yet another issue I ran into with memcached, hierarchical select failed. It can be solved using this patch http://drupal.org/node/538022#comment-3116204

Add new comment