Thursday 8 August 2019

How to install memcached into CPanel running CentOS

Installing libevent on CentOS

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

wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
tar xvfz libevent-2.0.20-stable.tar.gz
cd libevent-2.0.20-stable
./configure
make
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.

wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar xvfz memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure
make
make install
Above steps will install Memcached server, now you can start it will following command.

memcached

Troubleshoot Tips

If you get following error “error while loading shared libraries: libevent-1.4.so.2: 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.

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xvfz memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure
make
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.

extension=memcache.so
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"
extension=memcache.so
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.

<html>
<head>
  <title>PHP Memcache Extension Test</title>
</head>
<body>
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
echo "Server's version: {$memcache->getVersion()}";
$tmp = new stdClass;
$tmp->string_attribute = 'JournalDev';
$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";
print_r($memcache->get('key'));
?> 
</body>
</html>
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.

0 comments:

Post a Comment