CentosControl PanelCWPLinuxUbuntuVesta CPWordpress

Configure and Speed Up WordPress with Memcached on PHP 7

In this tutorials we’ll configure WordPress with Memcached on PHP 7.xx. Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

For this to work we need PHP MEMCACHED and Memcached package installed on your server. Memcached supports wide variety of OSs including Centos and Ubuntu, in this tut we’ll only cover the installation of memcached and pecl php memcached on Centos and on Ubuntu. Let’s get Started :

CentOS :

Install remi repo :-

Centos 8 :

cd /usr/local/src
dnf -y install epel-release
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf -y --enablerepo=remi,remi install memcached
systemctl start memcached
systemctl enable memcached

Centos 7 :

cd /usr/local/src
yum -y install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y --enablerepo=remi,remi install memcached
systemctl start memcached
systemctl enable memcached

Centos 6 :

cd /usr/local/src
yum -y install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum -y --enablerepo=remi,remi install memcached
service memcached start
chkconfig memcached on

Ubuntu

20.04 and above (old version also supported) :

apt-get install memcached libmemcached-tools
systemctl start memcached
systemctl enable memcached

PHP MEMCACHED :

Now We’ll Build and install PHP MEMCACHED on php 7.xx (official) :

cd /usr/local/src
rm -rf memcached*
curl https://pecl.php.net/get/memcached -o memcached.tgz
tar -xf memcached.tgz
cd memcached-*/
phpize
./configure
make
make install

If you get SASL error then we need disable sasl flag added to the config:

cd /usr/local/src
rm -rf memcached*
curl https://pecl.php.net/get/memcached -o memcached.tgz
tar -xf memcached.tgz
cd memcached-*
phpize
./configure --disable-memcached-sasl
make
make install

Now you need to add this line to your php.ini  to enable this php extension  :

extension=memcached.so

Then at last restart apache/php-fpm service and done, you can then check phpinfo and search “memcached” if it is there then you’ve successfully installed php-memcached and memcached.

WordPress Configuration with Memcached :

Now we need to go with WP configuration with memcached it is the simple process before proceeding make sure you’ve installed memcached and php memcached on php 7.xx.

Download this plugin on your PC : Memcached Redux

Extract and Find “object-cache.php” copy it to your WordPress “wp-content dir  using FTP/SFTP/File-Manager

Before enabling object-cache in WordPress, you have to add the following lines in your wp-config.php file.

define( 'WP_CACHE_KEY_SALT', 'example.com:' );

replace example.com with your site

Done you’ve successfully Configured Memcached with WordPress, keep visiting more coming.

How to monitor Memcached stats :

Ensure “nmap-ncat/nc” package is installed before running this command :

watch -td '(echo stats ; echo quit) | nc 127.0.0.1 11211 | grep get*'

It will show stats in realtime, if “get_hits” is increasing then memcached is working fine :

eg :

STAT rusage_user 0.058720
STAT rusage_system 0.039618
STAT cmd_get 0
STAT get_hits 588669
STAT get_misses 200
STAT get_expired 560
STAT get_flushed 962
STAT slab_global_page_pool 0

Sandeep B.

I'm a system admin and php developer and currently working as System Admin In CWP Control panel, expertise in Linux and Windows administration RHEL certified admin.

Related Articles

6 Comments

  1. Hi and thanks for your nice tutorial.

    On my website plugin is not working after installng memcached, i.e

    Fatal error: Cannot redeclare wp_cache_add() (previously declared in /home/[…]/wp-includes/cache.php:27) in /home/[…]/wp-content/plugins/memcached-redux/object-cache.php on line 18

    Any idea solving this issue?

Leave a Reply

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

Back to top button