Object caching is one of the best things for WordPress as it makes WordPress faster and reduces load on your database. However, WordPress’s built-in object cache feature isn’t persistent, which means the cache is stored only until the page cache expires. As a result, when you clear the page cache, everything is cleared. To make this system work persistently, we need something like Redis. In this tutorial, I will guide you on how to install and configure Redis object cache for WordPress.
The best part of this tutorial is that you can use it on both LEMP and LAMP setups.
How does Redis Cache work?
The most important question is how Redis cache works and how it makes our website faster. The answer is, whenever a visitor visits your blog or website, many database quries are executed. Redis stores these queries, and next time when a user visits the same page, the cached query is executed. As a result, the user gets the output very quickly, which drastically reduces query execution time.
Benefits to Configure Redis cache
Two biggest advantages to use Redis cache is that it speeds up the database queries, resulting in faster page loading speed. Secondly, it reduces the load on the database because instead of querying the database again and again, Redis provides the required data from it’s own cache.
Prerequisites
- A user with sudo privileges
- Installed and configured WordPress. If you haven’t installed WordPress yet, you can follow my tutorial to install WordPress with Nginx on Ubuntu
Step 1 – Install Redis Server on Ubuntu
The first step before installing Redis is to update the repository. Let’s do that by executing the command given below
# sudo apt update && sudo apt upgrade -y
As it’s done, let’s begin installing Redis on our Ubuntu server
# sudo apt install redis-server -y
As the installation completes, you can verify the installation by executing the given below command
# redis-cli ping
You should the output as
PONG
Now, let’s enable the Redis server
# sudo systemctl enable redis-server
Step 2 – Configure Redis Server on Ubuntu
As our Redis server is installed, let’s move ahead and configure it, and for that, we will edit the Redis conf file.
# sudo nano /etc/redis/redis.conf
Find out “maxmemory” and change it to
maxmemory 256mb
Find “maxmemory-policy” and change it to
maxmemory-policy allkeys-lru
Find “bind 127.0.0.1” and remove the “#” from the front. If “#” is not mentioned, then leave it as it is.
bind 127.0.0.1
Now save the file by pressing “ctrl+x”,”y”, and hit the “Enter” key. Restart the Redis service for the changes to take effect
# sudo systemctl restart redis-server
Step 3 – Install Redis PHP extension
The third step is to install the PHP Redis extension so that it will work with our existing PHP environment
# sudo apt install php-redis -y
Now, restart the php-fpm service so that it will adapt to the changes
# sudo systemctl restart php8.4-fpm
Note:- Replace version of php as per the php version installed on your server
Step 4 – Configure the wp-config.php file for Redis
The next step is to configure the wp-config.php file. Just copy and paste the directives given below in the wp-config.php file
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', '6379');
define('WP_REDIS_TIMEOUT', 1);
define('WP_REDIS_READ_TIMEOUT', 1);
Now save the file by pressing “ctrl+x”, “y”, hit “Enter” key
Step 5 – Install the Redis plugin from the WordPress dashboard
Now, log in to your WordPress and install the Redis Object Cache plugin and enable it
Additional Recommendations
If WordPress and the database are configured on the same server, then make sure to bind Redis to 127.0.0.1 for security reasons. We also recommend that you configure a password for Redis by editing the /etc/redis/redis.conf file. To enable Redis password, make the following changes
Find
# requirepass foobared
Change it to
requirepass your_password
Note:- Change your_password to whatever password you want
If you set a password, then make sure to define its directive in the wp-config.php file by adding the code given below.
define('WP_REDIS_PASSWORD', 'your_password');
Note: Change your_password to the password you have entered in front of "requirepass" above
Now save the file by pressing “ctrl+x”, “y”, and hit the “Enter” key
Restart the Redis service for the changes to take effect
# sudo systemctl restart redis-server
Conclusion
With this, we have installed and configured Redis for WordPress on our Ubuntu server. I managed to reduce the number of queries from 136 to 42, which is very good when it comes to performance and load on the server.