How to install memcached into CPanel running CentOS With Examples

Recently I was trying to configure a VBulletin forum to use Memcached for caching. I was looking for some guidance for easy setup but didn’t find any useful and easy guide. I had installed memcached server on Mac, so I had some idea about it and when I got succeeded, I thought of writing this post to help others also.

To install and use Memcached caching in any software, we need to do the following three things.

  1. Install libevent library that is used by memcached server
  2. Install memcached server and start it
  3. Install PHP extension for memcache

Before installing memcached server, just create a directory at any location that you want to use. We will download all the binaries there and use it for our installation purpose.

Installing libevent on CentOS

Run below commands to install libevent on CentOS. Use the latest libevent stable version.

tar xvfz libevent-2.0.20-stable.tar.gz
cd libevent-2.0.20-stable
make install

Above commands will install libevent on your CentOS server.

Install Memcached server on CentOS

Run below commands to download latest Memcached server library and install it. Check their website to get the latest version.

tar xvfz memcached-1.4.15.tar.gz
cd memcached-1.4.15
make install

Above steps will install Memcached server, now you can start it will following command.


Troubleshoot Tips

If you get following error “error while loading shared libraries: cannot open shared object file: No such file or directory” then you need to register libevent by following command.

export LD_LIBRARY_PATH= /usr/local/lib

You should keep above export command in your user profile (.bash_profile) so that you don’t need to export it always.

You might get an error message as “[memcached] can’t run as root without the -u switch” if you are using root user. You can’t run Memcached using root user. Use below command to start Memcached server.

Memcached Security Tip

Make sure to use -l option while starting Memcached server, so that only specific IPs can connect to the Memcached instance. If you won’t use this option, anybody who knows the IP and port of memcached can connect it using telnet, which is a security threat.

memcached -d -u nobody -p 11211

Now try to connect to memcached server using telnet and run some commands from memcached telnet commands to make sure its working fine.

Once you are satisfied that memcached server is running fine, go to the next step to install PHP Memcache extension.

Install PHP Memcached Extension on CentOS

Run below commands to install PHP Memcached extension and configure it. Make sure to use the latest stable version.

tar xvfz memcache-2.2.7.tgz
cd memcache-2.2.7
make install

Above commands will install Memcached extension but to use it with PHP, we need to configure it in php.ini file and restart apache web server.

vi /usr/local/lib/php.ini

Add below line to the php.ini file.

Check for extension_dir variable, if it’s not set in php.ini (most probably if you are installing any extension for the first time) then add following lines to provide directory location also. Make sure you change directory location as in your server.

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"

Now your PHP configuration is done to load Memcached extension also, run below command to restart apache web server.

service httpd restart

Run below command to confirm that the Memcache module is loaded.

php -m

Test PHP Memcache Extension

To test whether everything is fine or not, just create a PHP file with following content.

  <title>PHP Memcache Extension Test</title>
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
echo "Server's version: {$memcache->getVersion()}";
$tmp = new stdClass;
$tmp->string_attribute = 123;
$memcache->set('key', $tmp, false, 10) or die ("Failed to save temporary object at memcache server");
echo "Data from the cache:n";

Now access this PHP file from browser and if you get output like this:

Server's version: 1.4.15Data from the cache: stdClass Object ( [string_attribute] => 123 )

It means that everything is fine. Memcached server is running fine and PHP Memcache module is able to connect and save/retrieve data into caching server.

By admin

Leave a Reply

%d bloggers like this: