How to install memcached on Ubuntu for use with Drupal

This guide is based on various community forum posts. Special thanks to Jonathan Wagener for the Ubuntu specific instructions

This guide is intended as a relatively easy step by step guide to:

  • Install and configure memcached and memcache PHP extension on Ubuntu 11.04 and higher for use with Drupal and the Drupal Memcache module.
  • This is a very basic single memcached server setup but a good starting point for getting memcached working. 

Requirements:

  • Ubuntu 11.04 or later installed on your machine. 
  • PHP 5.2.0 or higher installed

 


1. Install memcached on your server.

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

2. Install memcache PHP extension using PECL.

  • PECL is great for installing PHP extensions.
sudo apt-get install php5-dev php-pear make
  • After you have installed PECL on your system, open the Terminal Window and enter :
sudo 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 :
sudo nano /etc/php5/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/default/memcached
  • 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 :
sudo nano /etc/default/memcached
  • Change the following line FROM-
MEMCACHED_PARAMS="-d -l 127.0.0.1"
  • TO the following by adding the -m 4096 bit 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.
MEMCACHED_PARAMS="-m 4096 -d -l 127.0.0.1"

6. Start the memcached service.

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

7. Restart Apache.

  • Open the Terminal Window and enter :
sudo service apache2 restart
  • OR on older systems :
sudo /etc/init.d/apache2 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. 

http://www.thefanclub.co.za/how-to/how-install-memcached-on-ubuntu-for-d...

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
To