Howto: Integrate memcached to speed up your drupal site

Enabling drupal cache, drupal will save cached data to database and send data from database whenever the data is required. This save up time to query database and build pages / blocks from php code, hence the performance of your drupal site is significantly boost. But there is still a way to speed up your site to be even faster -- to save the cached data directly into server's memory and fetch from it. Memcached, a high-performance memory caching system is the tool for the solution. This post is an answer to the question of how to integrate memcached and drupal to speed up your site.

environment: ubuntu hardy (8.04), drupal 6.13

memcached

To install memcached, you need to install libevent first:

sudo apt-get install libevent-dev

Install memcached:

mkdir src
cd src
wget http://memcached.googlecode.com/files/memcached-1.4.0.tar.gz
tar xzvf memcached-1.4.0.tar.gz
cd memcached-1.4.0
./configure
make
sudo make install
cd ..

Create control script:

sudo nano /usr/local/bin/memcache.sh

Add the following code:

#!/bin/sh
case "$1" in
start) /usr/local/bin/memcached -d -u root -m 240  -p 11211
;;
stop)  killall memcached
;;
esac

240 is the memory limit for the instance of memcached, the unit is MB.
11211 is the port number.

make it executable :

sudo chmod +x /usr/local/bin/memcache.sh	

start a memcached instance when the server startup:

sudo nano /etc/rc.local

add:

/usr/local/bin/memcache.sh start

start a memcached instance by running:

/usr/local/bin/memcache.sh start

PECL memcache extension for PHP

install php-pear if you have not installed it yet

apt-get install php-pear

install PECL memcache :

pecl install memcache	

edit php.ini file:
add "extension=memcache.so" to it.

restart apcache:

sudo apachectl restart

now, on your phpinfo page ( admin/reports/status/php ), you should see a memcache session like this:
phpinfo memcache status

Memcache API and Integration module

open settings.php of your drupal site ( /sites/default/settings.php ), and add the following to the end of the file :

 $conf = array(
   'cache_inc' => './sites/all/modules/memcache/memcache.inc',
 );

note: you may place './sites/all/modules/memcache/memcache.inc' with 'cache_inc' => './sites/all/modules/memcache/memcache.db.inc' to cache data both to memory and database if your memcache's memory limit is small or the memcached instance go offline often. (See the README.txt of Memcache API and Integration for more details. )

download Memcache API and Integration module from http://drupal.org/project/memcache, install and enable it.

Now, the integration of memcached and your drupal site is done. You can view memcache status from /admin/reports/memcache .

03 Nov15:11

"/usr/local/bin/memcached:

By xain

"/usr/local/bin/memcached: not found."
Is there a memcached file under the directory? Have you had the memcached installed?

25 Sep01:18

When I execute

By Jerry (not verified)

When I execute /usr/local/bin/memcache.sh start I am returned
/usr/local/bin/memcache.sh: 7: /usr/local/bin/memcached: not found.
However an ls of /usr/local/bin clearly lists memcache.sh and an edit of that file shows the contents as listed in your post.

phpinfo page shows memcache enabled. pecl extension is installed and php.ini has extension=memcache.so
Here is a copy of /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/local/bin/memcache.sh start

exit 0

Does the hash in front of rc.local require removing?

Any help is appreciated.

Post new comment

The content of this field is kept private and will not be shown publicly.