How To Install Memcached For WordPress On A VPS

If you're using a VPS, it won't come with memcached pre-installed. Here's how to install it on a VPS so your WordPress site runs faster!

On this page
  1. Two Components to Memcached
  2. Step 1: Installing the memcached Service on a VPS
  3. Step 2: Secure memcached by Restricting Access to the Local Machine
  4. Step 3: Install the PHP Component of Memcached
  5. Using memcached on WordPress

If you’re running WordPress on a VPS – managed or unmanaged – your web hosting provider won’t include memcached as part of the installation. A managed VPS takes care of OS updates, setting up the control panel, e-mail, etc., but only goes so far. Unlike shared hosting, where your web host might have a checkmark in the PHP extensions interface where you can enable or disable various modules, with a VPS, you’ll have to install memcached for WordPress manually.

It’s not very hard to install memcached on a VPS, but you’ll have to take into consideration things like PHP versions for integration into WordPress.

#Two Components to Memcached

For your PHP script in WordPress to be able to access the memcached system, two components must be installed:

  1. The memcached server component
  2. The memcached PHP component

The first is the memcached service that should be installed on your VPS. It creates and enables the daemon that runs in the background. The second is what allows your PHP scripts to access memcached and use its features. So let’s see how to install them one by one.

#Step 1: Installing the memcached Service on a VPS

For the following commands, I’m assuming a CentOS distribution. The commands will be different for Ubuntu, which uses apt-get instead of yum. So make the changes as necessary! Note that all these commands will require at least sudo privileges. In general, I don’t recommend that you log in as root, so create a user with sudo privileges, and dive in!

First, we update the existing repositories:

Code
yum update

Then we install memcached and the libmemcached library:

Code
yum install memcached libmemcached

Once this is done, you’ve installed the first component of memcached on your VPS!

#Step 2: Secure memcached by Restricting Access to the Local Machine

You don’t want anyone outside your VPS to access memcached and potentially cause a DDoS attack. So we change the configuration file to only allow requests from the local machine to access it. The configuration file is located at:

Code
/etc/sysconfig/memcached

Open it and paste the following code in the OPTIONS line like this:

OPTIONS=”-l 127.0.0.1 -U 0″

This also blocks the UDP protocol that has been used previously for DDoS attacks.

Start and enable the memcached daemon like this:

Code
systemctl restart memcached
systemctl enable memcached

And you’re done!

#Step 3: Install the PHP Component of Memcached

It can get tricky here. I happen to have multiple versions of PHP on my machine, and it was hard for me to figure out how to install the right version of memcached.

To do this, search for the right module for your PHP version. In my case, my site runs on PHP 7.4. So I first do this:

Code
yum search memcached | grep php74

This brings up the following package in the list:

Get the Right Package for Memcached

Get the Right Package for Memcached

As you can see, the correct name for the memcached package I want is:

Code
php74-php-pecl-memcached

So now I install this using the command:

Code
yum install php74-php-pecl-memcached

Let the installation proceed. When it’s done, restart apache and PHP. The process of restarting PHP will be different depending on what kind of PHP set up you have. My installation uses PHP-FPM, so I restart it like this:

Code
systemctl reload php-fpm

Your way might be different. So make sure you read the documentation to see how to do it!

#Using memcached on WordPress

To fully utilize memcached, you should have applications that call it. One great example is the caching plugin W3 Total Cache, which you can configure to use memcached. If you have the LiteSpeed web server, then you shouldn’t use a 3rd party caching plugin. But if your server is Apache, then caching is a must! Memcached will speed up the process of caching, so make sure you use it as much as possible!

Photo of Bhagwad Park
Written by

Bhagwad Park

Bhagwad Park writes for the NameHero blog.

More from Bhagwad →