Memcache For Drupal in Ubuntu 13.04

Installation and Configuration of Memcached Daemon and PECL Memcache in Ubuntu for Drupal 7

Steps:

1. Install memcached in the server

sudo apt-get install memcached libmemcached-tools
sudo apt-get install php5-dev php-pear make
sudo pecl install memcache

Latest dev Version of Memcache can be installed via.

# If you haven't already installed PECL memcache
pecl install memcache-beta
# If you wish to "upgrade" to a beta release
pecl upgrade memcache-beta

2. Add memcache.ini in PHP conf.d directory

sudo nano /etc/php5/conf.d/memcache.ini
Add: extension=memcache.so
Add: memcache.hash_strategy="consistent"

This should normally install Memcache in Ubuntu.

3. Create individual memcached sockets for each Site where Memcached is required.
Edit /etc/init.d/memcached and place the following line at roughly line 44

# Make /var/run/memcached directory
if [ ! -d "/var/run/memcached" ]; then
  mkdir /var/run/memcached
  chown memcache /var/run/memcached
fi

4. Make a copy of the memcached.conf for your site

# The naming convention is memcached_myserver.conf, this will
# create the config for a Memcached instance called myserver
# You can name what you please
cp /etc/memcached.conf /etc/memcached_myserver.conf

5. Edit /etc/memcached_myserver.conf to our liking:

# The memory limit is on line 23, it is measured in MB, it is probably safe to leave it at 64MB.
# You can monitor it and increase it later if necessary.
# Worth noting is that the daemon will not start out using this
# much memory for storage, but will only grow to use this much space.
-m 64
# Comment out the default port on line 26 like so:
# -p 11211
#Comment out the IP address option on line 35 like so:
# -l 127.0.0.1
# At the bottom of the file add the location of your socket by specifying the -s switch:
-s /var/run/memcached/memcached_myserver.sock
# The name of the socket isn't important as long as you remember it for later.
# I just stick to the config file convention, replacing .conf with .sock as I then always
# know which socket belongs to which instance.

# Finally add the -a switch to specify the octal file mode of the socket when it is created:
-a 0766
# You may not want the socket to be world writable, I have not set it up like that just yet.

6. Check if instance is running

# Start all Memcached instances with:
service memcached start
echo "stats" | nc -U /var/run/memcached/memcached_myserver.sock

7. Download “Memcache API and Integration” module. Enable memcache and memcache_admin. Download this patch for sockets to work with memcache_admin statistics. Currently the module does not support

# We need to change to the memcache_admin folder for patch no. 2 as it doesnt apply otherwise
cd /path/to/memcache/memcache_admin
"wget http://drupal.org/files/patch-memcache-memcache_admin-socket-support.patch" 
git apply -v patch-memcache-memcache_admin-socket-support.patch

8. Edit the settings.php file

# Adds memcache as a cache backend
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
# Makes it so that memcache is the default caching backend
$conf['cache_default_class'] = 'MemCacheDrupal';
# Keep forms in persistent storage, as per discussed at the beginning
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

# Specify the memcache servers you wish to use and assign them to a cluster
# Cluster = group of memcache servers, in our case, it's probably just one server per cluster.
$conf['memcache_servers'] = array('unix:///var/run/memcached/memcached_myserver.sock' => 'default');
# This assigns all cache bins to the 'default' cluster from above
$conf['memcache_bins'] = array('cache' => 'default');

#Alternatively, if you were using more Memcached server instances it would look like this:
$conf['memcache_servers'] = array(
  'unix:///var/run/memcached/memcached_myserver.sock' => 'default',
  'unix:///var/run/memcached/memcached_myserver2.sock' => 'mycluster',
);
# And then you could assign different bins to the second cluster
$conf['memcache_bins'] = array(
  'cache' => 'default',
  'cache_bootstrap' => 'mycluster',
  'cache_block' => 'mycluster',
);

# If you wanted multiple Drupal installations to share one Memcache instance use the prefix like so:
$conf['memcache_key_prefix'] = 'site_specific_prefix';

--
Source:
http://www.thefanclub.co.za/how-to/how-install-memcached-on-ubuntu-for-d...
http://www.failover.co/blog/drupal-7-memcache-pecl-memcache-memcached-an...
http://www.ibm.com/developerworks/xml/library/os-memcached/index.html