Saturday 23 June 2012

memcached on Debian for use with Drupal


1. Install memcached on your server.

  • Open the Terminal Window and enter :
apt-get install memcached libmemcached-tools

2. Install memcache PHP extension using PECL.

  • PECL is great for installing PHP extensions.
apt-get install php5-dev php-pear make
  • After you have installed PECL on your system, open the Terminal Window and enter :
pecl install memcache

3. Add memcache.so to php.ini

  • We must instruct PHP to load the extension.
  • You can do this by adding a file named memcache.ini to the configuration directory /etc/php5/conf.d
  • Open the Terminal Window and enter :
nano /etc/php5/fpm/conf.d/memcache.ini
  • Add the following line to the file and save :
extension=memcache.so
  • If you intend to use memcached with Drupal also add the following line to your php.ini or memcache.ini file and save :
memcache.hash_strategy="consistent"

4. Open firewall port 11211.

  • The default port for the memcached server is TCP port 11211.
  • Configure your firewall to open port 11211 for TCP traffic.

5. Configure the memcached allowed memory.

  • All memcached configuration settings can be found in /etc/memcached.conf
  • The default memory setting for memcached is 64 MB. 
  • Depending on the amount of RAM available on the server allocate a block of memory to memcached.
  • Open the Terminal Window and enter :
nano /etc/memcached.conf
  • Change the following line FROM-
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64
  • TO the following by changing the -m 64 to -m 4096 to allow memcached 4 GB of RAM. Adjust the size in MB according to the memory that you have available. Save the file when done.
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 4096

6. Start the memcached service.

  • Open the Terminal Window and enter :
service memcached start
  • OR on older systems :
/etc/init.d/memcached start

7. Restart nginx.

  • Open the Terminal Window and enter :
service nginx restart
  • OR on older systems :
sudo /etc/init.d/nginx restart

8. Check to see if memcached server is active and listening on port 11211.

  • Open the Terminal Window and enter :
netstat -tap | grep memcached

9. Check the status and stats with memstat tool

  • Part of the memcached package is a handy tool called memstat.
  • You need to specify the host IP and port. In this case the host IP is 127.0.0.1 and the port 1211.
  • Open the Terminal Window and enter :
memstat 127.0.0.1:11211

10. Activate the Drupal memcached module.

  • Install the Drupal Memcache module and activate. For more complete instructions visit the Drupal Memcache Documentation
  • Edit settings.php in your Drupal installation to include memcache.inc
  • For Drupal 6, edit the settings.php file and add the following :
$conf['cache_inc'] ='sites/all/modules/memcache/memcache.inc';
  • For Drupal 7, edit the settings.php file and add the following :
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_key_prefix'] = 'something_unique';
* note : Replace the "something_unique" in the last line with your own unique memcache key prefix. The memcache_key_prefix is also needed for both Drupal 6 & 7 in a multi-site environment if you would like to use memcached for more than one Drupal installation on the same server. 

Source  (with slight modifications to accout for Debian , nginx & php5-fpm)