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:
- Installing the system service on the VPS
- Installing the correct PHP extension
- 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:
yum install redis
Once the installation is complete, enable and start the redis service:
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:
Scroll down till you see the following line:
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:
Uncomment it, and set it to the maximum memory that you want Redis to use. For my installation, I used:
For me, 256 MB is more than enough for Redis. Your limit might be different.
Once you’ve made the changes, restart redis:
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:
yum search redis | php74
I get an output like this:
Over here, we see that the latest PHP 7.4 extension for communicating with the Redis server is:
So we use yum to install this package:
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:
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:
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!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply