Monday, June 14, 2010

Using memcached with Drupal or Pressflow on CentOS 5

SkyHi @ Monday, June 14, 2010
Installing memcached

1. Packages for memcached are available from the popular EPEL repository. Enable this repository if you haven't already.
2. Install memcached:

yum install --enable=epel memcached

3. Start the single default instance:

service memcached start

4. Test your connectivity to it:

telnet localhost 11211

5. Press Ctrl + ] and type quit to end the test.
6. Stop the default instance:

service memcached stop

Installing PHP's memcache extension

Use one of the following methods.
Using Remi's repository (preferred method)

1. Enable Remi's repository, if you haven't already.
2. Install the extension:

yum install --enable=remi php-pecl-memcache

3. Restart Apache:

service httpd restart

Using the PEAR package manager (alternative method)

1. Install PHP's PEAR package manager:

yum install php-pear httpd-devel zlib-devel

2. Install the extension:

pear install pecl/memcache

3. Enable the extension:

echo "extension=memcache.so" > /etc/php.d/memcache.ini

4. Restart Apache:

service httpd restart

Testing PHP's memcache extension

1. Place a PHP file in a directory served by Apache:
phpinfo.php


2. Load the file in a browser.
3. Search for "memcache.default_port".

Installing the Memcache module in Drupal or Pressflow

1. Download the Memcache module for your core version.
2. Use your project's procedure to copy the memcache module files into sites/all/modules/memcache, just as you would for any module. Do not "install" the module from Drupal's module administration page.
3. Apply the patch attached to this wiki page to fix the statistics output of the Memcache Admin module. (There is currently only a patch for the 5.x-1.8 version of the Memcache module.)

Configuring memcached instances

1. Examine the database for your project.
2. List all tables in your database and note the ones with "cache" in the name.
3. Create mappings from each database table to the instance name you'll use for memcached. Use the instance name "default" for the table named (or with the name ending in) "cache". Name the others using the text following "cache_".
* Example mappings for a database without table prefixes:
Table Instance
cache default
cache_filter filter
cache_page page
* Example mappings for a database with table prefixes:
Table Instance
prefix_cache default
prefix_cache_filter filter
prefix_cache_page page
4. Configure each instance in the memcached init script. Use a size of 8-256 for each instance; these sizes can be tuned later. The linked replacement init script has sensible sizes and pools for core version 6. The sizes for core 5 are generally similar. See below for some example mappings of tables to memcached instances. See Replacement init script for memcached on CentOS 5.
5. Start up the new instances:

service memcached start

6. Set them to run by default on system start:

chkconfig memcached on

Configuring Drupal to use the instances (single-host version)

1. Edit the site's settings.php file.
2. If you already have a $conf array, add these keys to the exiting one. If not, create a $conf array.
3. Add the following:

$memcached_port = 11211;
$conf = array(
'cache_inc' => './sites/all/modules/memcache/memcache.inc',
'memcache_servers' => array(
'localhost:' . $memcached_port++ => 'default',
// Servers/instances configured here
),
'memcache_bins' => array(
'cache' => 'default',
// Bins assigned here
),
);

4. For the next steps, it's critical that the instances be configured in the same order as in your init script because the ports are sequentially determined.
5. For each non-default memcached instance, add one line beneath "// Servers/instances configured here":

'localhost:' . $memcached_port++ => 'instance-name',

6. For each non-default memcached instance, add one line beneath "// Bins assigned here":

'cache_instance-name' => 'instance-name',

7. Within Drupal, enable the Memcache Admin module and visit Administration > Memcache status.
8. Add "access memcache statistics" permissions to a role associated with your user.
9. Check that your cache interface is working for each instance, each of which shows as a top-level tab. Some of the sub-tabs ("default", etc.) may return errors because memcached's statistics interface varies quite a bit for each version. If any tab gives you results, the instance is configured correctly.

Configuring Drupal to use the instances (multiple-host version)

1. Determine the non-localhost IP addresses for your servers. These are the IPs you would use to ping from one server to another within the cluster or behind your firewall.
2. Follow the single-host instructions for one server in the cluster, but replace "localhost" with that server's external IP.
3. Replace the occurrences of "$memcached_port++" with the actual port numbers.
4. Duplicate the list of instances from "memcache_servers" once for each server in your cluster, replacing the IP address in each duplicate with the corresponding IP address of the memcached instance host.
5. Use the exact same instance list on every host. This ordering is critical to memcached performing proper hashing.
6. Expect to get a standard memcached configuration like this, which you should apply to the settings.php file on each Apache host:
settings.php (excerpt)

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

'memcache_servers' => array(
'10.0.0.1:11211' => 'default',
'10.0.0.1:11212' => 'block',
'10.0.0.1:11213' => 'content',
'10.0.0.1:11214' => 'filter',
'10.0.0.1:11215' => 'form',
'10.0.0.1:11216' => 'menu',
'10.0.0.1:11217' => 'page',
'10.0.0.1:11218' => 'update',
'10.0.0.1:11219' => 'views',

'10.0.0.2:11211' => 'default',
'10.0.0.2:11212' => 'block',
'10.0.0.2:11213' => 'content',
'10.0.0.2:11214' => 'filter',
'10.0.0.2:11215' => 'form',
'10.0.0.2:11216' => 'menu',
'10.0.0.2:11217' => 'page',
'10.0.0.2:11218' => 'update',
'10.0.0.2:11219' => 'views',
),

'memcache_bins' => array(
'cache' => 'default',
'cache_block' => 'block',
'cache_content' => 'content',
'cache_filter' => 'filter',
'cache_form' => 'form',
'cache_menu' => 'menu',
'cache_page' => 'page',
'cache_update' => 'update',
'cache_views' => 'views',
),
);

There are currently no attachments on this page.

REFERENCES
https://wiki.fourkitchens.com/display/PF/Using+memcached+with+Drupal+or+Pressflow+on+CentOS+5