How To Setup And Use Memcached With WordPress
By on May 8th, 2011

A few days ago, I spoke about the architecture that powers Techie Buzz. In that post, I had mentioned that memcached was one of our key ingredients.

Memcache is a powerful tool to share objects across servers and it is used by several big sites including , Yahoo and many more. The best part about using memcached on your server is that you can cut down on your database queries significantly and store commonly used objects in memory and share it across multiple servers.

If you use WordPress on a multi-server setup, you can easily cut down on DB queries by up-to 50% by implementing memcached. If you are interested in knowing how to implement memcached on your server, here is a tutorial. The assumption is that you are using Ubuntu, if not, the installation steps might change.

How To Install Memcached And Configure It

Step 1: Install memcached using the command given below

sudo apt-get install memcached

Step 2: After you have installed memcached you will have to configure it through a .conf file, to do that use the command given below

vi /etc/memcached.conf

Step 3: In the configuration file you will find few parameters, you can ignore the -d, -u and logfile parameters and leave it as default. However, change the -m, -p and -i parameters to suit your servers. The -m parameter specifies the memory that will be allocated to memcache. The -p parameter specifies the port that memcached will run on and the -l (ell) parameter specifies the IP address the memcached server will run on. You will need to change the -l (ell) parameter value from 127.0.0.1 to a local or global IP address of your server. This is necessary because your memcached server will be accessed by outside servers and using 127.0.0.1 will not work then.

Once you have done that run the following command.

service memcached restart or /etc/init.d/memcached restart

Using Memcached With WordPress

Once you have setup your memcached server, it is time to now setup WordPress to use memcached to store your objects. To do that, head over to http://wordpress.org/extend/plugins/memcached/ and install the memcached plugin for WordPress. Unlike other plugins, you will have to drop this file into the wp-content directory and not the plugins directory.

You will have to install additional plugins for your server to make use of this plugin, namely PECL memcache extension which can be downloaded from http://pecl.php.net/package/memcache.

Now comes the part where you will actually tell WordPress to use memcached as an object storing mechanism. To do that, open the wp-config file and add the line below to it.

global $memcached_servers;
$memcached_servers = array(‘default’ => array(‘memcache-server-ip-or-name:11211′));

The array is used to define multiple memcached servers which can be used for your site. Please make sure to edit the name/ip of the server and the port before you save the file.

That’s it. Now just refresh your cache and WordPress should start storing the objects in memcache and will access your database very sparsely.  Do let me know if you have any problems or difficulties in setting this up through your comments.

Bonus Note: You can browse and manage your memcache server through a WordPress plugin called WP Memcached Manager which can be downloaded at http://wordpress.org/extend/plugins/wp-memcached-manager/.

Tags: , , , , , ,
Author: Keith Dsouza Google Profile for Keith Dsouza
I am the editor-in-chief and owner of Techie Buzz. I love coding and have contributed to several open source projects in the past. You can know more about me and my projects by visiting my Personal Website. I am also a social networking enthusiast and can be found active on twitter, you can follow Keith on twitter @keithdsouza. You can click on my name to visit my Google+ profile.

Keith Dsouza has written and can be contacted at keith@techie-buzz.com.

Leave a Reply

Name (required)

Website (optional)

 
    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'advanced_comment' not found or invalid function name in /home/keith/techie-buzz.com/htdocs/wp-includes/comment-template.php on line 1694
 
Copyright 2006-2012 Techie Buzz. All Rights Reserved. Our content may not be reproduced on other websites. Content Delivery by MaxCDN