Using memcached With Your vBulletin Forum To Reduce Server Load (Debian Etch)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2008-12-18 12:39. :: PHP

Using memcached With Your vBulletin Forum To Reduce Server Load (Debian Etch)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 12/01/2008

In this article I will explain how you can reduce server load by using memcached together with your vBulletin forum software (I'm assuming you are running vBulletin on Apache2 with mod_php5). memcached is a daemon that can store objects in the system's memory (e.g. results of database queries) which can speed up your web site tremendously. You can use memcached over a network (i.e., install your web application on one server and memcached on another server), but usually you install both on one server to avoid the networking overhead.

It should be noted the memcached is no out-of-the-box solution for speeding up your web applications. Typically you have to adjust your scripts (PHP, Perl, etc.) to work with memcached, so this requires a little bit of work. Fortunately, vBulletin has built-in support for memcached - all you need to do is modify vBulletin's config.php file.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm assuming that you have vBulletin installed in the directory /var/www/www.example.com/web/forums, so config.php is located in /var/www/www.example.com/web/forums/includes.

 

2 Installing memcached And The PHP5 memcache Module

memcached and the PHP5 memcache module are available as packages for Debian Etch, so we can install them as follows:

apt-get install memcached php5-memcache

After the installation, memcached should already be running. You can check that by typing

netstat -tap | grep memcached

server1:~# netstat -tap | grep memcached
tcp        0      0 *:11211                 *:*                     LISTEN     3053/memcached
server1:~#

As you see, memcached is running on port 11211 (the default memcached port), and it's listening on all interfaces on the system. As memcached has no built-in authentication mechanisms (in order to not give up on speed), this means that anyone can connect to it from outside and use it. To avoid this, you can either close port 11211 in your firewall, or you configure memcached to listen on localhost only. I will use the latter method here.

To do this, open the memcached configuration which is stored in /etc/memcached.conf:

vi /etc/memcached.conf

Add -l 127.0.0.1 to the configuration (you can also adjust the other settings if you like - the file contains explanations for each setting):

[...]
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
# -l 12.34.56.78
-l 127.0.0.1
[...]

Restart memcached...

/etc/init.d/memcached restart

... and run

netstat -tap | grep memcached

again. As you see, memcached is now listening on localhost only:

server1:~# netstat -tap | grep memcached
tcp        0      0 localhost.localdo:11211 *:*                     LISTEN     3092/memcached
server1:~#

Then open /etc/php5/apache2/php.ini...

vi /etc/php5/apache2/php.ini

... and make sure that you have something like this in it (probably at the end):

[...]
extension=memcache.so
[memcache]
memcache.dbpath=/var/lib/memcache
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0

Afterwards, we restart Apache so that our new PHP configuration takes effect:

/etc/init.d/apache2 restart

 

3 Configure vBulletin

Next open vBulletin's config.php file:

cd /var/www/www.example.com/web/forums/includes/
vi config.php

You should find something like this in it:

[...]
/*
$config['Datastore']['class'] = 'vB_Datastore_Memcached';
$i = 0;
// First Server
$i++;
$config['Misc']['memcacheserver'][$i]                = '127.0.0.1';
$config['Misc']['memcacheport'][$i]                        = 11211;
$config['Misc']['memcachepersistent'][$i]        = true;
$config['Misc']['memcacheweight'][$i]                = 1;
$config['Misc']['memcachetimeout'][$i]                = 1;
$config['Misc']['memcacheretry_interval'][$i] = 15;
*/
[...]

The /* ... */ means the section is commented out. Remove /* and */ from that section and save the file:

[...]

$config['Datastore']['class'] = 'vB_Datastore_Memcached';
$i = 0;
// First Server
$i++;
$config['Misc']['memcacheserver'][$i]                = '127.0.0.1';
$config['Misc']['memcacheport'][$i]                        = 11211;
$config['Misc']['memcachepersistent'][$i]        = true;
$config['Misc']['memcacheweight'][$i]                = 1;
$config['Misc']['memcachetimeout'][$i]                = 1;
$config['Misc']['memcacheretry_interval'][$i] = 15;

[...]

(If your memcached is running on another server or port, you must adjust these settings!)

That's it. Your vBulletin is now using memcached!

 

4 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Mon, 2009-05-04 08:18.

The benefit of running memcache on the same machine as the Apache/MySQL used by vBulletin is debatable. Linux and MySQL already do a pretty good job of caching things; by using memcache you're just taking memory away from them.

Running memcache on another box (preferably one with a low latency to the web server) makes sense though.

On a side note, there's a useful vbulletin-specific caching system at  http://linuxbox.co.uk/vbulletin_caching_with_lbcache.php