Speed Up WordPress with Memcached on PHP 7

Speed Up WordPress with Memcached on PHP 7


In this guide, we will walk through the process of configuring WordPress with Memcached on PHP 7.xx. Memcached is a high-performance, distributed memory object caching system designed to speed up dynamic web applications by reducing database load. It works by storing small chunks of arbitrary data, such as strings or objects, which can include results from database queries, API calls, or page rendering.

Memcached stores data in memory, rather than fetching it from the database each time, making it especially useful for improving the speed of frequently accessed data. To use Memcached with WordPress, you must install both the Memcached server and the PHP Memcached extension on your server. This tutorial will cover how to install these components on CentOS and Ubuntu systems.

Installing Memcached and PHP Memcached on CentOS

1. Install the Remi Repository

To begin, you need to install the Remi repository, which provides the required PHP packages for CentOS. The installation steps vary depending on the version of CentOS you’re using:

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

2. Install Memcached

Once the Remi repository is installed, you can proceed with installing Memcached. This software acts as the caching server.

Installing Memcached and PHP Memcached on Ubuntu

For Ubuntu users, including versions 20.04 and above, the installation process is similar:

1. Install Memcached

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

Installing PHP Memcached

After setting up Memcached, you need to install the PHP Memcached extension. This allows PHP scripts to communicate with the Memcached server.

1. Install PHP Memcached Extension

Use the PHP package manager (PECL) to install the PHP Memcached extension compatible with PHP 7.xx. If you encounter a SASL error during installation, disable the SASL flag by modifying the configuration file.

2. Enable the Extension in PHP

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 encounter an SASL error then we need to disable the 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

Next, add this line to your php.ini file to enable this php extension:

extension=memcached.so

Finally, restart your Apache or PHP-FPM service to apply the changes.

3. Verify the Installation

To confirm that Memcached is correctly installed and working, you can check your PHP configuration. Open your phpinfo page and search for “memcached.” If the extension is listed, you have successfully installed both PHP Memcached and the Memcached server.

Configuring WordPress to Use Memcached

Now that Memcached and PHP Memcached are installed, it’s time to configure WordPress to use them. This step is straightforward:

1. Download the Memcached Redux Plugin

Download the Memcached Redux plugin, which is available as a zip file. Once downloaded, extract the files to your local computer.

2. Copy the Object-Cache.php File

Locate the object-cache.php file in the extracted plugin folder. Upload it to the wp-content directory of your WordPress installation using FTP, SFTP, or your file manager.

3. Update the wp-config.php File

To ensure proper integration of Memcached with WordPress, add the following line to your wp-config.php file:

define( 'WP_CACHE_KEY_SALT', 'example.com:' );
// Replace example.com with your actual site domain.

Once these steps are completed, you have successfully configured Memcached with WordPress.

Monitoring Memcached Stats

To monitor the performance of Memcached, you can check real-time statistics. First, ensure the nmap-ncat (or nc) package is installed on your system. Then, use the following command to view the stats:

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

If the get_hits count is increasing, Memcached is functioning correctly and caching data as expected.

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

Conclusion

Integrating Memcached with WordPress can significantly improve your site’s performance by reducing the load on your database and speeding up content delivery. By following this guide, you’ve learned how to install and configure Memcached on CentOS and Ubuntu, install the PHP Memcached extension, and configure WordPress to use caching. Regularly monitoring Memcached’s performance can help you ensure that your caching system is working efficiently and contributing to a faster, more responsive website.