Memcache Installation for CentOS and cPanel

Are you taking advantage of the super fast caching capability of memcache? now available on all BUYHTTP shared, VPS and dedicated servers.

Dynamic websites like Joomla and WordPress sometimes feels sluggish after installing a number of plugins or modules due to the number of database SQL queries generated by these plugins, so how do we fix that?

The answer is simple, by using some sort of caching system that utilize memcache or redis and since memcached seems to work well, widely supported and very easy to work with we’re going with memcache. Joomla comes with a built-in module to enable memcache for your Joomla website. For WordPress, you can install a caching plugin that support memcache. W3 total cache comes to mind.

What is memcached?

“Memcached (pronunciation: mem-cash-dee) is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read” – source: wikipedia

memcache consist of 2 main components, memcached deamon and memcache{d} PHP modules:

How to install memcache on cPanel server with EasyApache 3?

First of all we’re installing memcached daemon as root user and since we’re mainly a CentOS shop i will use yum (this instruction was tested on CentOS 5 & 6 & now 7):

1- Install EPEL repo by following instructions at https://fedoraproject.org/wiki/EPEL

# yum install memcached

2- Start the daemon:

CentOS5/6

# /etc/init.d/memcached start

CentOS 7

# systemctl start memcached

Make sure the daemon starts automatically after reboot:

CentOS 5/6

# chkconfig memcached on

CentOS 7

# systemctl enable memcached

You can change memcached configuration like maximum connections and memory size by editing this file:

# vi /etc/sysconfig/memcached

Service daemon is now installed and we need to install the PHP module that will talk to memcached daemon, there are 2 PHP modules you can install memcache & memcached (yes, the second module was named as the daemon name)

To install memcache php module using PECL:

# pecl install memcache

Or from WHM: Go to Software -> Module Installers -> PHP Pecl and search for memcache and install memcache. This should be enough to start using memcache in your PHP script but what if your plugin uses memcached PHP module? here’s how to install memcached PHP module:

Install libmemcached & libmemcached-devel and cyrus-sasl-devel packages:

# yum install libmemcached
# yum install libmemcached-devel
# yum install cyrus-sasl-devel

You can also install memcached module by compiling from source:

# cd
# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
# tar -zxvf libmemcached-1.0.4.tar.gz
# cd libmemcached-1.0.4
# ./configure
# make && make install
# pecl install memcached

How to install memcache module on cPanel server with EasyApache 4?

If you upgraded your server to EasyApache 4 with multiple PHP versions then you will have to install memcahce module for each version, to do so use the following commands:

PHP 5.4

#/opt/cpanel/ea-php54/root/usr/bin/pecl install memcache

To verify the module was installed and loading properly
#/opt/cpanel/ea-php54/root/usr/bin/php -m | grep memcache

PHP 5.5

#/opt/cpanel/ea-php55/root/usr/bin/pecl install memcache

To verify:
#/opt/cpanel/ea-php55/root/usr/bin/php -m | grep memcache

PHP 5.6

#/opt/cpanel/ea-php56/root/usr/bin/pecl install memcache

To verify:
#/opt/cpanel/ea-php56/root/usr/bin/php -m | grep memcache

php 7 & 7.1 is a little different, we have to compile the module from source:

PHP 7.0

#cd /usr/local/src/
#wget https://github.com/websupport-sk/pecl-memcache/archive/NON_BLOCKING_IO_php7.zip
#unzip NON_BLOCKING_IO_php7.zip
#cd pecl-memcache-NON_BLOCKING_IO_php7
#/opt/cpanel/ea-php70/root/usr/bin/phpize
#./configure –enable-memcache –with-php-config=/opt/cpanel/ea-php70/root/usr/bin/php-config
#make
#cp modules/memcache.so /opt/cpanel/ea-php70/root/usr/lib64/php/modules/
#echo ‘extension=memcache.so’ > /opt/cpanel/ea-php70/root/etc/php.d/memcached.ini

To verify:
#/opt/cpanel/ea-php70/root/usr/bin/php -m | grep memcache

PHP 7.1

#cd /usr/local/src/
#wget https://github.com/websupport-sk/pecl-memcache/archive/NON_BLOCKING_IO_php7.zip
#unzip NON_BLOCKING_IO_php7.zip
#cd pecl-memcache-NON_BLOCKING_IO_php7
#/opt/cpanel/ea-php71/root/usr/bin/phpize && ./configure –enable-memcache –with-php-config=/opt/cpanel/ea-php71/root/usr/bin/php-config
#make
#cp modules/memcache.so /opt/cpanel/ea-php71/root/usr/lib64/php/modules/
#echo ‘extension=memcache.so’ > /opt/cpanel/ea-php71/root/etc/php.d/memcached.ini

To verify:
#/opt/cpanel/ea-php71/root/usr/bin/php -m | grep memcache

Finally to test memcache and see live stats use memcached-tool:

# memcached-tool localhost:11211 display

Get started with Premium VPS hosting for WordPress today

Leave a Reply

Your email address will not be published. Required fields are marked *

Top