How to set up Memcached on CentOS 6 and configure WordPress to use it

Last modified: July 30, 2020
You are here:
Estimated reading time: 2 min

Server performance is always a priority, as website load speed is essential for a good user experience, as well as search engine rankings. One of the most popular ways to increase website performance is to use Memcached – a distributed caching system that runs and can cache objects in-memory, reducing the number of database and API calls.

The increase in performance is primarily due to a decrease in disk I/O, which is a major bottleneck on most servers. Even for SSD-equipped machines, it makes sense to use Memcached in order to reduce the number of disk writes, increasing the lifespan of the drive. CPU usage is also somewhat lower when using Memcached.

Memcached is supported by all major CMS, and if you have a large amount of RAM (4+ GB), it’s generally a good idea to install it. In this tutorial, we will show you how to install and configure Memcached on CentOS 6.x and set up WordPress to use it.

Setting up the Memcached server

First of all, you’ll need root privileges to run most of the commands. To make it simple, you can elevate your status by using the su command and entering your root password.

Now, you’ll need to install memcached – the base CentOS6 repository has an outdated version, which is why we’ll add the RPMForge repo and install the memcached package from RPMForge-Extras. We’ll also need the PHP-PECL-Memcache and PHP-PECL-Memcached packages in order to be able to use Memcached with WordPress (the PHP modules can be installed at the same time – only one is actually used, depending on the plugin).

[CLI COMMANDS]

rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

yum install memcached php-pecl-memcache php-pecl-memcached –enablerepo=rpmforge-extras

[/CLI COMMANDS]

You’ll then need to configure a few Memcached settings:

[CLI COMMANDS]

nano -w /etc/sysconfig/memcached

[/CLI COMMANDS]

The default 1211 port doesn’t need to be changed. You can set any user, usually “nobody” works fine. You only need to set it as “root” if you set a port lower than 1024. The Cachesize depends on how much RAM you have available – generally 512-1024 MB should be enough. You’ll only need to increase the connections limit and cache size if you hit the limits on them. You can save and exit nano using CTRL-O and CTRL-X.

Set Memcached to run on boot and start it:

[CLI COMMANDS]

chkconfig memcached on

/etc/init.d/memcached start

[/CLI COMMANDS]

Check the stats to see if Memcached is working:

[CLI COMMANDS]

echo stats | nc localhost 11211

[/CLI COMMANDS]

If you see the stats, Memcached is running properly.

Setting up WordPress to use Memcached

Now we can proceed to set up WordPress to use Memcached. There are several plugins that can use Memcached – for this example we’ll use WP-FFPC, which is simple and works without issues.

Open your WordPress dashboard, go to “Plugins”, click “Add New” and search for “WP-FFPC”. Click on “Install” and enter your FTP credentials. Note: you can always install it the old way, via manual FTP upload – in that case, you can download the plugin from here: http://wordpress.org/plugins/wp-ffpc/

After activating the plugin, you’ll see a few errors – to complete the activation, you’ll first need to add ” define(‘WP_CACHE’,true); ” to the wp-config.php file in the root directory of your WordPress installation:

You’ll also have to go to the plugin’s settings page and click “Save Changes”:

You can test the caching system by loading your site as a non-logged in user, then running “echo stats | nc localhost 11211” in the command line to see the results. You should see items being added to the cache, as well as their size:

You can also see if Memcached works with PHP by creating a simple PHP file with the following code and opening it in the browser:

<?php $mc = new Memcached(); $mc->addServer(“127.0.0.1”, 11211);

$result = $mc->get(“test_key”);

if($result) {  echo $result; } else {  echo “No data cached. Please refresh the page”;  $mc->set(“test_key”, “Test data found in cache!”) or die (“Failed to save data with Memcached”); } ?>

 

That’s it – your server has Memcached installed and running, and WordPress is configured to use it!

Was this article helpful?
Dislike 0
Views: 12