Installation
These are the broad steps you need to take in order to use this software on Drupal 6.x.Information pertaining to the Drupal 7.x version is also found in the README.txt file from the module distribution.
For 6.x (order is important):
- Install the memcached binaries on your server. See
- Install the PECL memcache extension for PHP.
- In php.ini set memcache.hash_strategy="consistent".
- Put your site into offline mode.
- Download and install the memcache module.
- If you have previously been running the memcache module, run update.php.
- Start at least one instance of memcached on your server.
- Edit settings.php to configure the servers, clusters and bins that memcache is supposed to use. (see code snippet below)
- Edit settings.php to include memcache.inc. For example, $conf['cache_inc'] ='sites/all/modules/memcache/memcache.inc';
- Bring your site back online.
For 7.x (order is important):
-
Install the memcached binaries on your server. See
- How to install Memcache on Debian Etch
-
How to install Memcache on Ubuntu (this link also provides instructions for installing the PHP memcache extension and configuring the Drupal settings.php file).
Alternative instructions for installing & configuring memcached on Ubuntu, including package php5-memcache - How to install Memcache on openSUSE
- How to install Memcache on OS X
- How to install Memcache Fedora / CentOS / RHEL
- Install the PECL memcache extension for PHP. This must be version 2.2.1 or higher or you will experience errors.
- Put your site into offline mode.
- Download and install the memcache module.
- If you have previously been running the memcache module, run update.php.
- Start at least one instance of memcached on your server.
-
Edit settings.php to make memcache the default cache class, for example:
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc'; // The 'cache_form' bin must be assigned no non-volatile storage. $conf['cache_class_cache_form'] = 'DrupalDatabaseCache'; $conf['cache_default_class'] = 'MemCacheDrupal'; $conf['memcache_key_prefix'] = 'something_unique';
- Bring your site back online.
For instructions on 1 and 2 above, please see the INSTALLATION.txt file that comes with the memcache module download.
Servers
NOTE: As of 6.15, it is possible to efficiently run memcache in one daemon, shared by all bins. You can safely ignore advice found elsewhere on the internet that advises one memcached daemon per bin.
In terms of reporting, there are still some advantages to having more daemons. If you want the simple version, you can start one default memcache instance on your web server like this:
memcached -m 24 -p 11211 -d
If that is enough to meet your needs, there is no more configuration needed.
If you want to utilize this module's sophisticated clustering feature and spread your cache over several machines, or if your cache is found on a machine other than your web server, read on.
Security
You should probably lock down the memcache server so that it only listens for connections from the hosts that need to be served.
The default on some installations is that memcache listens to connections from all addresses. To close that hole, modify your memcached configuration as follows:
On RHEL/CentOS, edit /etc/sysconfig/memcached:
OPTIONS="-l 127.0.0.1"
Format is OPTIONS="-l ${HOSTIP}"
On Debian/Ubuntu, edit /etc/memcached.conf:
-l 127.0.0.1
Format is -l ${HOSTIP}
Configuration
The available memcached servers are specified in $conf in settings.php. If you do not specify any servers, memcache.inc assumes that you have a memcached instance running on the local machine on port 11211 (127.0.0.1:11211).
If this is true, and it is the only memcached instance you wish to use, no further configuration is required. If you have more than one memcached instance running, you need to add two arrays to $conf;
memcache_servers and memcache_bins.
The arrays follow this pattern:
$conf['memcache_servers'] = array(
host1:port => cluster,
host2:port => cluster,
hostN:port => cluster
);
$conf['memcache_bins'] = array(
bin1 => cluster,
bin2 => cluster,
binN => cluster
);
WARNING: Avoid the use of "localhost" and instead use real IP addresses or hostnames that will remain consistent across the network.
The bin/cluster/server model can be described as follows:
- Servers are memcached instances identified by host:port.
- Bins are groups of data that get cached together and map 1:1 to the $table param in cache_set(). Examples from Drupal core are cache_filter, cache_menu. The default is 'cache'.
- Clusters are groups of servers that act as a memory pool.
- Many bins can be assigned to a cluster.
- The default cluster is 'default'.
Here is a simple setup that has two memcached instances, both running on 10.1.1.1. The 11212 instance belongs to the 'pages' cluster and the table cache_page is mapped to the 'pages' cluster. Thus everything that gets cached, with the exception of the page cache (cache_page), will be put into 'default', or the 11211 instance. The page cache will be in 11212.
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'pages'
);
$conf['memcache_bins'] = array(
'cache_page' => 'pages',
);
Here is an example configuration that has two clusters, 'default' and 'cluster2'. Five memcached instances are divided up between the two clusters. 'cache_filter' and 'cache_menu' bins go to 'cluster2'. All other bins go to 'default'.
D6:
$conf['cache_inc'] = './sites/all/modules/memcache/memcache.inc';
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'default',
'10.1.1.2:11211' => 'default',
'10.1.1.3:11211' => 'cluster2',
'10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'cache_filter' => 'cluster2',
'cache_menu' => 'cluster2'
);
D7:
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'default',
'10.1.1.2:11211' => 'default',
'10.1.1.3:11211' => 'cluster2',
'10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'cache_filter' => 'cluster2',
'cache_menu' => 'cluster2'
);
Prefixing
If you want to have multiple Drupal installations share memcached instances, you need to include a unique prefix for each Drupal installation in the $conf
array in settings.php:
$conf['memcache_key_prefix'] = 'something_unique';
Sessions
NOTE: Session.inc is not yet stable in Drupal 7 (see #656838: [META] Port sessions to D7 for current status).
You MUST have setup separate memcached instances for both session and users for memcached sessions to work.
Here is a sample config that uses memcache for sessions.
$conf['cache_default_class'] = 'MemCacheDrupal';
// The 'cache_form' bin must be assigned no non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['session_inc'] = './sites/all/modules/memcache/memcache-session.inc';
$conf['memcache_servers'] = array(
'localhost:11211' => 'default',
'localhost:11212' => 'session',
'localhost:11213' => 'users',
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'session' => 'session',
'users' => 'users',
);
Troubleshooting
See the troubleshooting guide.
Memcache Admin
A module offering a UI for memcache is included. It provides aggregated and per-page statistics for memcache.
Memcached PECL Extension Support
We also support the Memcached PECL extension. This extension backends to libmemcached and allows you to use some of the newer advanced features in memcached 1.4.
NOTE: It is important to realize that the memcache php.ini options do not impact the memcached extension, this new extension doesn't read in options that way.
Instead, it takes options directly from Drupal. Because of this, you must
configure memcached in settings.php. Please see the PECL Memcached documentation for possible options.
An example configuration block is below, this block also illustrates the
default options (selected through performance testing). These options will be set unless overridden in settings.php.
$conf['memcache_options'] = array(
Memcached::OPT_COMPRESSION => FALSE,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
);
These are as follows:
- Turn off compression, as this takes more CPU cycles than it's worth for most users
- Turn on consistent distribution, which allows you to add/remove servers easily
Other options you could experiment with:
- Memcached::OPT_BINARY_PROTOCOL => TRUE
- This enables the Memcache binary protocol (only available in Memcached 1.4 and later). Note that some users have reported SLOWER performance with this feature enabled. It should only be enabled on extremely high traffic networks where memcache network traffic is a bottleneck.
Additional reading about the binary protocol:
http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol
- Memcached::OPT_TCP_NODELAY => TRUE
- This enables the no-delay feature for connecting sockets; it's been reported that this can speed up the Binary protocol (see above). This tells the TCP stack to send packets immediately and without waiting for a full payload, reducing per-packet network latency (disabling "Nagling").
Comments
Drupal 6
Note that this page is specific to Drupal 6. The way you hook up memcache in Drupal 7 is a bit different, specifically the way you tie into the cache system. See the README.txt file in the module for the updated installation instructions until this page is updated. (Please delete this comment when this page is updated.)
--
Larry Garfield
http://www.garfieldtech.com/
Thinking Functionally in PHP: https://leanpub.com/thinking-functionally-in-php
One important security hole
One important security hole is present in default memcached daemon configuration. That's it listen to connections from all IPs.
So, to close that hole, edit /etc/sysconfig/memcached with
OPTIONS="-l 127.0.0.1" or change IP to whatever suits your case.
A tip when trying to get this
A tip when trying to get this going on Ubuntu 11.04 is that
sudo apt-get install libmemcached
(needed for building the PECL extension) does not work, as you'd think it would. Usesudo apt-get install libmemcached-dev
instead. This depends on other packages that satisfy the dependency.Support my open-source work: Patreon | Ko-Fi | FillPDF Service
Drupal 7 Prefixing
the above code for Prefixing doesn't work on my D7 setup. it doesn't gave an error message but there's no data passed to memcached. here's what worked for my D7 setup:
$conf['memcache_key_prefix'] = 'something_unique';
How to install memcached on openSUSE for use with Drupal
Here is a tutorial on How to install memcached on openSUSE for use with Drupal
Clarification in docs
Do you mean you need to just need to have a session and user table in Drupal for this to work? Or do you you mean you need to have a separate memcache instance just for sessions and users?
Kim
Mac OS X 10.8 and MAMP (PHP 5.3.20)
After doing a clean install of MAMP/MAMP Pro 2.x with PHP 5.3.20, I followed commonly distributed instructions for installing memcached and then the PHP extension memcache. So in case someone else has a similar configuration, here's what I discovered:
http://www.lullabot.com/blog/article/installing-php-pear-and-pecl-extens...
(http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain...)
Success! So if you encounter the challenge with lazy symbol binding when trying to install memcache extension from pecl, hope these instructions help.
Memcache Install for D7 Windows for XAMP with PHP 5.3 or PHP 5.4
http://zurmo.org/wiki/installing-memcache-on-windows
Note the first option is for win32. If you have win64 (as I do) ref. the fourth bullet point of the install instructions (http://s3.amazonaws.com/downloads.northscale.com/memcached-win64-1.4.4-1...)
Note that the .dll with the win64 version is for PHP 5.3. If you are running PHP 5.4 (as I am) you will need a different .dll (http://windows.php.net/downloads/pecl/releases/memcache/3.0.6/php_memcac...)
It took me 4.5 hours to do this all because I didn't have the correct .dll file. With the correct and current info it should only take 15 minutes if that.
- Cheers
Josh Utley, President & CEO
Intrepid Network Inc. | Multimedia Services & Business Solutions
"Our mission is to provide a more personable multimedia service along with the highest quality business solutions."
php5-memcached
It looks like this simple commands are enough now:
php5-memcache for Drupal 8
In the Drupal 8 README file, php5-memcache is recommended (no "d" at the end), so the commands would be:
How to install Memcache Fedora / CentOS / RHEL link doesn't work
https://tag1consulting.com/blog/memcache-centos-fedora doesn't work any more. Try this https://www.digitalocean.com/community/tutorials/how-to-store-php-sessio...
I want to configure memcache
I want to configure memcache for REDHAT server.
And our server is in VPN environment and for security reason I unable to download the packages through just entering the command.
Also This How to install Memcache Fedora / CentOS / RHEL link is not working.
I need all packages first download from server(repo) also I need all repo links along with process so that I can install as well.
Is there any solutions for the same.
Thanks in advance
Ishwar
install Memcached from sources
Hi Ishwar,
You can download Memcached source from here https://memcached.org/files/memcached-1.4.31.tar.gz
If it doesn't work, I hope this tutorial will help you http://www.ducea.com/2008/01/10/howto-install-memcached-from-sources-on-...
Have a nice day :)
cache_class_cache_form
For Drupal 8, form state information has been moved into a key/value store, but in previous versions,
form state stored in a cache table (though not strictly a cache always keep the cache_form table in MySQL. This override is done for Drupal 7 with the following setting in settings.php:
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
How I installed PECL package - Ubuntu 20.04, Apache2.4, PHP 7.4
The memcache module for drupal says install the memcache pecl package after installing the daemon. The following worked for me to get the most recent version that works with php7.4. For some reason I couldn't find a decent walk-through. Maybe this will help someone.
--- Install the daemon and prerequisites ---
--- install the memcache package ---
Download the version 4.0.5.2 tar
At the time if this post, it's the most recent that will work with PHP7.4.
http://pecl.php.net/package/memcache
I had to compile with root.
Using sudo I would get this error ('/usr/lib/php/20190902/#INST@165437#': Permission denied)
root#phpize && ./configure --enable-memcache && make install
--- Enable the extensions ---
Create this file.
sudo vim /etc/php/7.4/mods-available/memcache.ini
Add the following and save.
Enable the extension by creating a symlink to it here.
sudo ln -s /etc/php/7.4/mods-available/memcache.ini /etc/php/7.4/apache2/conf.d/25-memcache.ini
---- Test ----
restart apache
sudo systemctl reload apache2
Look at your Drupal system php info details page to see if memcache is installed.
Then go ahead and install the modules.
Edit the /etc/memcache.ini file to change the amount of memory etc. as needed.