How to install Memcache for Drupal

If you host a high traffic website, you probably have already wondered how to improve performances and reduce the load of your web server.

To do this, you can start to optimize Apache and MySQL parameters, but at the end, you will always need powerful caching systems.
In my opinion, the two important ones for a PHP website are APC (to cache PHP opcode) and Memcache (database oriented).
Of course, you can also work with Redis, Varnish, and so on.

This tutorial will help you to set up Memcache on a Apache/MySQL Debian 6 server, to use it with Drupal. It may work without troubles on Ubuntu server too.


On your web server, run the following commands to install everything:

apt-get update
apt-get install memcached libmemcached-tools
apt-get install php5-dev php-pear make
apt-get install php5-memcached
pecl install memcache-3.0.8 (3.0.8 is currently the last version)

Now that everything is installed, we just need to configure some things:

vi /etc/php5/apache2/conf.d/memcached.ini

Add the 2 lines below in the file: (the last one is recommended for Drupal websites)

Change configuration of Memcache:

vi /etc/memcached.conf

Change the line 23 if you want to allow Memcache to use more RAM. Default is 64MB and isn't enough for high database consuming websites.

Restart services:

/etc/init.d/memcached restart
/etc/init.d/apache2 restart

At this point, Memcache is installed and configured (you should see the Memcache daemon in the process list on your server), we just need to connect it to Drupal.

Download the Drupal module.

Install the module in Drupal and activate it.

Finally, add the 3 lines below to your Drupal settings.php file:

$conf['cache_backends'][] = 'sites/all/modules/contrib/memcache/';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_key_prefix'] = 'uniqueKey';

You can change "uniqueKey" for anything you want, and be sure that the path to the "" file is correct.

Then, if you want to display some Memcache stats to see if everything is working fine, please refer to this post.