How To Install Redis For WordPress On A VPS

Here's a step by step tutorial on how to install Redis and use it with WordPress on a VPS or dedicated server.

On this page
  1. Step 1: Install the Redis System Service
  2. Step 2: Configure Redis maxmemory and Ensure it’s Bound to the Local Port
  3. Step 3: Install the Redis PHP Agent
  4. Step 4: Install a Plugin that Can use Redis

Earlier, I’d written a tutorial on how to install memcached on a VPS that integrates with PHP. In this tutorial, I’ll write about how to do the same with Redis. Like memcached, Redis is another caching mechanism that can speed up reads and writes by using your (much faster) RAM instead of accessing the database or the disk. Here’s how to install and configure Redis with your VPS.

Note: As before, for these examples, I’m using CentOS 7 on Linux. If you have a different distribution, the installation commands will differ, but hopefully nothing will be too different. Also, most of these commands will require root privileges, so make sure you’re using an account that can run sudo commands.

To use Redis for WordPress requires at least 3 steps:

  1. Installing the system service on the VPS
  2. Installing the correct PHP extension
  3. Installing a plugin that can use Redis

Here are the steps one by one.

#Step 1: Install the Redis System Service

First, we update the yum packages to make sure we’re getting the latest information:

Code
yum update

Then:

Code
yum install redis

Once the installation is complete, enable and start the redis service:

Code
systemctl start redis
systemctl enable redis

#Step 2: Configure Redis maxmemory and Ensure it’s Bound to the Local Port

By default, Redis listens to all interfaces on all ports. This makes it susceptible to DDoS attacks. So first we want to bind Redis to only the local IP address: 127.0.0.1. It should already be enabled by default, but no harm in checking.

Navigate to the Redis config file and open it in a text editor:

Code
vi /etc/redis.conf

Scroll down till you see the following line:

Code
bind 127.0.0.1

Ensure that there’s no hash (#) in front of it, so the line is uncommented. If there is a hash, remove it.

Next, go down till you see the following line:

Code
maxmemory

Uncomment it, and set it to the maximum memory that you want Redis to use. For my installation, I used:

Code
maxmemory 256M

For me, 256 MB is more than enough for Redis. Your limit might be different.

Once you’ve made the changes, restart redis:

Code
systemctl restart redis

#Step 3: Install the Redis PHP Agent

The next step is to integrate Redis with PHP. Search the repo for the version of Redis that integrates with your PHP version. Mine is PHP 7.4, so I use the following command:

Code
yum search redis | php74

I get an output like this:

Get the Correct Version of Redis for your PHP Version

Get the Correct Version of Redis for your PHP Version

Over here, we see that the latest PHP 7.4 extension for communicating with the Redis server is:

Code
php74-php-pecl-redis5

So we use yum to install this package:

Code
yum install php74-php-pecl-redis5

Once this is done, restart your server and PHP. Now you’re set up with Redis! You can verify this with a phpinfo(); command and check that the Redis module exists.

#Step 4: Install a Plugin that Can use Redis

The final step is to install a plugin that actually allows WordPress to access the Redis cache for its operations. There are two well-known plugins that do this:

  1. The Redis Object Cache Plugin
  2. W3 Total Cache

For the latter, you have to specifically go into the “General” tab and select Redis for each of the Page, database, and Fragment caches like this:

Using Redis for Caching with W3 Total Cache

Using Redis for Caching with W3 Total Cache

If you’ve performed all the previous steps correctly, then the above option should be available to you and not greyed out.

Note: The jury is out on database caching – especially when the W3 Total Cache Object caching is already enabled. The documentation suggests leaving it out, so I suggest that’s what you do. You can always try enabling it to see how it works for you.

And that’s it! You’ve successfully installed Redis to speed up your WordPress site!

Photo of Bhagwad Park
Written by

Bhagwad Park

Bhagwad Park writes for the NameHero blog.

More from Bhagwad →