Recently, i was working on improving my WordPress website’s performance and i found many blogs recommending to install various kinds of plugins. Yes, caching is very important and everyone must use it. But caching plugins only caches the js, css and html of your website. But we have another most important thing on our setup, which is database.
When i digged deeper, i found out that WordPress relies heavily on database and thus by making database queries faster we can squeeze out higher performance from our website. Another benefit of database caching is that your database will consume significantly lesser resources allowing PHP to work freely and won’t create any bottleneck. So i thought to work on database caching. There are two most famous tools for database caching are Memcached and Redis.
Since Redis is built on new technology and it offers persistent caching, i decided to go with Redis caching. Then i began to research more about Redis setup for WordPress. But what i found was really surprising. Most of the tutorials were limited to just installing Redis plugin on WordPress. Although, this thing may work if you are on a managed server because most of the managed hosting provider configure it as default. But if you are managing your server by own, just installing a plugin won’t give you any benefit.
I worked around a bit and successfully configured Redis for WordPress on Ubuntu 24.04.
Table of Contents
How Redis caching works?
Whenever you or someone browse your WordPress website, WordPress generates database queries for each request. Based on the query, your database responds and you or the visitor gets to see the result on the screen. Now, even if the query is repeated, database has to process each and every query.
This is where Redis server comes into picture. Redis server keeps the output of the queries in its cache and when same query is executed, Redis throws the output of the query directly from its cache instead of querying the database again.
This benefits the server in two ways,
- Database server doesn’t have to work on repeated queries which makes database consumes lesser resources
- Since database is consuming lesser resources, your server have abundance of free resource availble, which can be used by PHP, making PHP processing faster.
So configuring Redis is a win win thing.
Here is a complete step by step tutorial that i have crafted for you. Let’s begin
Note:- This tutorial is based on the complete setup running on one single server. So if your complete setup (WordPress + Database) in on one server, you can continue with the tutorial.
Prerequisite
- A VPS/dedicated server with Ubuntu 24.04 installed
- A working WordPress setup with LEMP/LAMP/OpenLitespeed, anything would work
- A sudo user
Step 1 – Updating the server
Always keep in mind that an updated server is very important. So before we begin, lets first update our server.
sudo apt update -y
Step 2 – Installing Redis Server and PHP extension
Redis server doesn’t work alone. Since we are configuring it for WordPress which is based on PHP, we will also have to install its PHP extension. First, lets find the correct version of PHP running on the server. Run the below command
php -v
sudo apt install redis-server php8.5-redis -y
Note:- Since i am using php8.5 i used php8.5-redis extension. Make sure to use the one that is shown by running "php -v" command
Step 3 – Configuring Redis Server
Since Redis stores all the cached data in RAM, we will have to configure it to consume a limited amount of it so that it wont eat up all your server’s RAM.
sudo nano /etc/redis/redis.conf
Now scroll down to the end of the file and copy and paste these
maxmemory 256mb
maxmemory-policy allkeys-lru
Note:- If your server have higher amount of RAM like 16GB or 32GB then you can increase "maxmemory" to 2GB or 4GB or whatever. It's totally upto you.
Now, scroll and find “requirepass“. Uncomment it by removing “#” and make it this way
requirepass your_strong_password
Note:- Make sure to change "your_strong_password" to a stronger password.
Now, scroll again to find “bind“. If it is already uncommented then make sure it looks like below, else uncomment it and make it look like below
bind 127.0.0.1 ::1
Now save the file by pressing ctrl+x, y and hit enter
Now lets restart the Redis server
sudo systemctl restart redis-server
sudo systemctl enable redis-server
Step 4 – Configuring WordPress to use Redis server
Now, open your wp-config.php file and copy and paste the below directives just above the line that says “That’s all, stop editing!“.
define('WP_CACHE', true);
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'your_strong_password');
define('WP_REDIS_MAXTTL', 86400); // 24 hours of cache expiration
Note:- Replace "your_strong_password" to the password you have entered in Step 3
Step 5 – Testing Redis server
Now if you have followed all the steps properly, you should see the following output when you test.
redis-cli -a your_strong_password
127.0.0.1:6379>ping
You should get a reply as
PONG
This reply confirms that your Redis server configuration is working properly.
Step 6 – Installing WordPress plugin
Now let’s install the Redis WordPress plugin
- Login to WordPress admin
- Hover on Plugins and click on Add New
- Search “Redis Object Cache” plugin. Install and Activate it
- Now go to Settings -> Redis
- Click on Enable Object Cache
- You should see the connection as “Connected” in green
Step 7 – Final Testing
Now on your server, Run the following command
redis-cli -a your_strong_password monitor
Now refresh the homepage of your website on the browser. You should see a trial of commands running, which ensures that your setup is working properly.
Conclusion
With this we have successfully configured Redis Server on our Ubuntu 24.04 server. If you have any issues or queries, drop your comment below.
Leave a Reply