How to set up Memcached on CentOS 6 and configure Drupal to use it

Memcached is one of the most popular ways of speeding up websites – it is a high performance, distributed memory caching system that can significantly reduce the number of times a database or API needs to be accessed. It runs and caches objects in the RAM, so it is much faster than the traditional static file cache employed by the majority of CMS.

If you use a server with a relatively large amount of RAM (typically 4+ GB), it would be reasonable to use Memcached to speed up your page load speed and reduce the load on your server. In this article, we’ll take you through a step by step process on how to set up Memcached on CentOS 6.x and configure the popular Drupal CMS to use it for its database and pages. The instructions may also apply for CentOS 5.x, as well as Red Hat Enterprise Linux 6.x and 5.x.

You will need root privileges to run most of the commands in this tutorial – to make it simple, we recommend you elevate to root by using the “su” command and entering your root password. You can copy/paste multiple commands from this article to your SSH console – they should run sequentially.

Installing and configuring memcached and the PHP modules on CentOS 6

Now we’ll need to install and configure memcached and the PHP module that will allow the CMS to use it. The base repository has an outdated version of memcached (1.4.4 at the time of this writing), there have been a lot of performance improvements since version 1.4.10.

An easy solution is to add the Atomic repository to your list – it has the latest version of memcached, as well as the latest stable versions of the required PHP extensions. Run the command and press Enter to agree to the license terms.

[COMMANDS] wget -q -O – | sh [/COMMANDS]

The downside to the Atomic repo is that it has many conflicts with the base repo, which is why I recommend disabling it right away. Use nano to open the repo file and change the “enabled” flag to “0”. You can save and exit nano with CTRL-O and CTRL-X.

[COMMANDS] nano -w /etc/yum.repos.d/atomic.repo [/COMMANDS]

Now you can install memcached and the PHP extension. We’ll install php-pecl-memcache, since it is older and more stable than php-pecl-memcached.

[COMMANDS] yum –enablerepo=atomic install memcached php-pecl-memcache [/COMMANDS]

You may want to configure some memcached options before starting the service:

[COMMANDS] nano -w /etc/sysconfig/memcached [/COMMANDS]

The recommended options are: port left as 11211 (default), user set as “nobody”, maxconn at 1024 (only needs to be increased when you hit the limit) and Cachesize at 512-1024 depending on your amount of RAM.

Now you can set memcached to run at boot, start it and check if it’s actually running:

[COMMANDS] chkconfig memcached on service memcached start ps -eaf | grep memcached [/COMMANDS]

You should see it running in the list.

Configuring Drupal to use Memcached

Now it’s time to set up Drupal to use it, which is a simple matter of installing and configuring the appropriate plugin. First, download the “Memcache API and Integration plugin” from this web page:

Now go to your Drupal Modules page and click on “Install new module”.

Select the plugin file you just downloaded, click install and provide your FTP details. The plugin should install right away. Now go back to the Modules page and enable the Memcache and Memcache Admin plugins.

And finally, go to Configuration->Performance and click on “Clear All Caches”. You can now view your Memcache statistics in Drupal by going to Reports->Memcache statistics:

Congratulations, you’ve got Memcached running on your server and Drupal using it to speed up your website!