Install Memcache on your ubuntu server
2011 29 Oct

Install Memcache on your ubuntu server & make your drupal faster

By Mukesh Agarwal

NOTE: This post explains setting up memcache on Drupal 6. We will very soon follow up for Drupal 7. Why do I need memcache for my drupal site?

Drupal's default cache mechanism stores a lot of cached content in the database, so every time an http request needs this cached content, they have to make a request to the cache tables of the drupal database. This means the web server has to load your database server into the memory ...NOTE: This post explains setting up memcache on Drupal 6. We will very soon follow up for Drupal 7.

Why do I need memcache for my drupal site?

Drupal's default cache mechanism stores a lot of cached content in the database, so every time an http request needs this cached content, they have to make a request to the cache tables of the drupal database. This means the web server has to load your database server into the memory. So lets say we had to load the entire page (from cache_page) there is no way of skipping calls to the database. Memcache allows you to take memory from parts of your system where you have more than you need and make it accessible to areas where you have less than you need. In our case, memcache stores the cached entries in the system RAM and updates itself whenever the corresponding cache tables are updated. To sum up, you save a lot of computing resources when you start using memcache. And yes, the big guns are using it too :-) Youtube, Facebook, Twitter, Wikipedia, Flickr et al use this service. Read the google's faq to understand how memcache works - http://code.google.com/p/memcached/wiki/FAQ#How_does_memcached_work?

Now that you want to setup memcache on your drupal site, lets jump into the installation. [Note: the below mentioned installation is for ubuntu server and not any other operating system.]

1.Install memcached on your server: sudo aptitude install memcached (avoid sudo when already logged in as root)

2.Install the php5-dev package if not already done: sudo aptitude install php5-dev (you'll need the dev package to phpize the pecl memcache library)

3.Install the PECL memcache library:

            1. cd /usr/local/src

            2. sudo wget http://pecl.php.net/get/memcache- 3.0.6.tgz (look at http://pecl.php.net/package/memcache to find the latest version available)

            3.sudo tar -xzvf memcache-3.0.6.tgz

            4.sudo cd memcache-3.0.6    

           5.sudo phpize   

           6.sudo ./configure

           7.sudo make

            8.sudo make install

4.Make sure your php loads the new memcache library: append the following lines in your php.ini (the one that is used by your web server, e.g. if you are using apache, your configuration file is likely to be at /etc/php5/apache2/php.ini) extension = memcache.so memcache.hash_strategy="consistent" read more about consistent hashing technique at http://code.google.com/p/memcached/wiki/FAQ#What_is_a_"consistent_hashing"_client?

5.Restart your web server: sudo /etc/init.d/apache2 restart 6.Okay thats a lot of commands, lets now see if we are moving in the right track. Create a info.php file under your root document and just have the following php code in it Now, call this file from your browser, e.g. [servername]/info.php and if you see something like the following then you are good: memcache memcache support enabled Version 3.0.

6 Revision $Revision: 310129 $ Else, post your issue as a comment and I'll try to help

7.Initiate Memcache instances: What cache tables do you want to use with memcache? You understand that only when you take a look at your database. What cache tables have decent number of entries? Those are the ones you would want to store in your RAM. Lets just assume you want to go for all the cache tables and lets also assume that there are 8 cache tables namely cache, cache_block, cache_content, cache_filter, cache_form, cache_menu, cache_page, cache_views. So here is what you do:

 sudo memcached -u www-data -p 11211 -m 2 -d sudo memcached -u www-data -p 11212 -m 2 -d sudo memcached -u www-data -p 11213 -m 2 -d sudo memcached -u www-data -p 11214 -m 2 -d sudo memcached -u www-data -p 11215 -m 2 -d sudo memcached -u www-data -p 11216 -m 2 -d sudo memcached -u www-data -p 11217 -m 2 -d sudo memcached -u www-data -p 11218 -m 2 -d 

Now that would create 8 daemons of memcache instances and you can see the processes with the command ps -A | grep memcached But thats not it, you need to make sure these instances are running everytime you restart your server. Lets just add the above 8 lines in the init command of memcache located at /etc/init.d/memcached -- add it in the "start" if clause. And now try to restart memcache sudo /etc/init.d/memcached restart and then make sure you test whether the instances are initiated using the same ps command that we tried a short while ago.

8.Install memcache module in your drupal code base. [Advice: Start using drush, it makes life easier, you just need to do a drush dl memcache] [Note: Do not enable the module yet]

9.PUT YOUR SITE TO OFFLINE MODE before continuing else you'll run into the risk of white screen of death 10.Modify your settings.php (located in sites/default) so that it knows the relation between the cache tables and the memcache instances. Append the following line in settings.php:

 $conf = array( 'cache_inc' => './sites/all/modules/contrib/memcache/memcache.db.inc', 'memcache_servers' => array( 'localhost:11211' => 'default', 'localhost:11212' => 'filter', 'localhost:11213' => 'menu', 'localhost:11214' => 'page', 'localhost:11215' => 'form', 'localhost:11216' => 'content', 'localhost:11217' => 'views', ), 'memcache_bins' => array( 'cache' => 'default', 'cache_filter' => 'filter', 'cache_menu' => 'menu', 'cache_page' => 'page', 'cache_form' => 'form', 'cache_content' => 'content', 'cache_views' => 'views', ), ); 

11.Enable the memcache module

12.You can now put your site to ONLINE mode now

13.Besides an improved performance, you can also check whether your memcache is responding via the urls admin/settings/memcache and admin/reports/memcache

Do post your comment, even if this post was helpful :-)

Latest Blogs

Chrome

Chrome to call out websites that are ‘too slow’

There’s little that can test your patience like a slow-loading website. Google gets that, and they’ve now got your back.

Read More

Significant UI

6 Significant Reasons to Invest in UX Design

In the digital world, we live in today building a digital presence has become important for organizations of all kinds.

Read More

Drupal 9

Getting ready for Drupal 9: what should website owners do?

Drupal 9 will release on 3rd June 2020, which means it will be the end of announcements for Drupal 7 and Drupal 8. Drupal 9 will not be much of a change from Drupal 8.

Read More

Security

Tips to Secure your Drupal Website with Ease: Basic Security checklist

If someone were to break in your room, they would probably learn less about you than if they hacked you on the internet.

Read More