Redis (Remote Dictionary Server) is commonly used to speed up the performance of web applications. In WordPress, Redis can be used as a caching mechanism to reduce response time and speed up page loading. In this article, we will learn how to configure Redis Cache to speed up your WordPress site.
Step-by-Step: Supercharge Your WordPress Website with Redis Cache Configuration
Redis Cache and WordPress cache plugins (such as the W3 Total Cache plugin, WP Super Cache, or WP Rocket both have a role in improving the performance of a WordPress site by using the cache mechanism. However, Redis Cache has several advantages over traditional cache plugins. Here are some of the strengths of Redis Cache over caching plugins in WordPress:
- High performance : Redis is an in-memory database system, this allows it to cache directly in RAM, which improves page load performance significantly. The RAM cache is much faster than the hard disk cache, as is the case with most traditional caching plugins.
- Custom controls : Redis offers more flexible cache management and configuration options than cache plugins. You can customize how cache works, cache lifetime, type of data stored, and many other options to meet your site’s specific requirements.
- Scalability : Redis supports replication and sharding, allowing you to scale your Redis server cluster to meet your scalability and higher load needs. This makes Redis a good choice when you have a large website and want to expand your cache system.
- Complex processing capabilities : Redis supports many complex data types such as lists, sets, hash maps, sorted sets, etc. This allows you to configure Redis Cache to store complex data structures and perform complex operations on the cached data, which traditional cache plugins are usually unable to do.
- High Reliability : Redis provides secure and reliable mechanisms to ensure your cached data is always protected and not lost. It supports features like replication, snapshots, and data backups, helping to ensure that your data is always available and safe.
- However, it should also be noted that Redis requires a system administrator with knowledge of server configuration and management, while traditional cache plugins can be easily installed and configured through the WordPress management interface. Choosing whether to use Redis Cache or a cache plugin in WordPress depends on your needs and technical knowledge.
Step 1: Install Redis Cache
First see if your VPS server has Redis Cache available, if not, you can run this command on the terminal to install:
sudo apt update sudo apt install redis-server
This will download and install Redis and its dependencies. Accordingly, there is an important configuration change to make in the Redis configuration file, which is automatically generated during the installation process.
Step 2: Config Redis of Server
Configuring and securing Redis on the server is an important step to ensure safety and reliability when using Redis as a key-value database or as a cache mechanism. Now open the redis.conf file and make some configuration changes to it.
sudo nano /etc/redis/redis.conf
Locate this line and make sure it is uncommented (remove the # at the beginning). Currently most of them support IPV6 so you can leave “::1” to allow IPV6 .
bind 127.0.0.1 ::1
Next, look for “maxmemory” to define memory for cache storage purposes. You can set the amount of RAM suitable for your server, usually 256M.
maxmemory 256mb
Keep looking for “maxmemory-policy” and set the value to “allkeys-lru”
maxmemory-policy allkeys-lru
To configure Redis Cache security, find and edit the “requirepass” configuration in the Redis configuration file. Set a strong password for it, like this:
requirepass your_redis_password
Done with redis configuration and security, now restart Redis Cache for the changes to take effect:
sudo service redis-server restart
Step 3: Config Redis for Wordpress
First, open the file “wp-config.php” and add these configurations to the file. This configuration is responsible for declaring and connecting Redis Cache for Wordpress.
define('WP_CACHE', true); define('WP_CACHE_KEY_SALT', 'youdomain.com'); define('WP_REDIS_HOST', 'localhost'); define('WP_REDIS_PORT', '6379'); define('WP_REDIS_PASSWORD', 'your_redis_password'); //Password is set in requirepass (step 2)
Continue to find and install the plugin “ Redis Object Cache ”. Then open “Settings -> Redis -> Enable” if the status: Status, Filesystem, Redis green as the screenshot below is a successful Redis Cache configuration.
Step 4: Config Nginx Redis Caching
The steps to install and configure Redis cache to use for Wordpress are almost done, but now you have to specify where to cache your website as well as tell Nginx not to cache certain pages. The following will ensure that the pages and admin screens for logged in users are not cached, along with a few others. This will go above the first position block.
In the Nginx configuration file for your domain, open and edit the following:
Add the line “fastcgi_cache_path” at the top of the file at the same level as server {}. And please specify your domain name and be directed to the directory containing the cache.
fastcgi_cache_path /var/www/vhosts/arriveddev.com/httpdocs/cache levels=1:2 keys_zone=youdomain.com:100m inactive=60m;
sudo chmod 755 /var/www/vhosts/arriveddev.com/httpdocs/cache
Where “/var/www/vhosts/arriveddev.com/httpdocs/cache” is the path to the directory containing the cache. In this tutorial we have placed the cache in the Wordpress source code directory.
Continue adding the following Redis wordpress configuration inside server {}.
set $skip_cache 0; # POST requests and urls with a query string should always go to PHP if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } # Don’t cache uris containing the following segments if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } # Don’t use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; }
Next, in the PHP location block, add the following commands.
fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache youdomain.com; fastcgi_cache_valid 60m;
Next you need to add the following commands to your nginx.conf file inside http {}.
sudo nano /etc/nginx/nginx.conf
# Cache Redis Settings
# Cache Redis Settings fastcgi_cache_key "$scheme$request_method$host$request_uri"; add_header Fastcgi-Cache $upstream_cache_status;
Finally, restart Nginx for the changes to take effect. (If there is an error on reboot, run the command sudo nginx -t
to show the error).
sudo service nginx restart
If there are no problems and the nginx startup is successful, you can start checking if the Redis cache is really working to cache the website or not.
Step 5: Check if Redis is working?
In step 4, we configured Redis Wordpress with Nginx to not cache users logged in with the “wordpress_logged_in” parameter. You can also omit this parameter to cache all logged in users. However, it is not recommended because it can cause cache errors and violate user information.
So how to check Redis is really working on your Wordpress site or not? There are several ways we will tell you as follows:
- Method 1: Check in the Redis Object Cache plugin by opening the Admin page, clicking Settings -> Redis -> Metrics. Here you will see the display data of Redis Cache.
- Method 2: In step 4 we have configured the cache directory as “/var/www/vhosts/arriveddev.com/httpdocs/cache”. So you can see in this directory, when the URLs are opened (view), redis will create cached versions for each page and will save it here.
- Method 3: Use the command to check the Redis Cache operation status. To do this log in to Redis.
redis-cli monitor
If you have set a Redis password, you can use this command, if it shows values like screenshots it means Redis is caching and speeding up your website.
export REDISCLI_AUTH="your_redis_password" redis-cli monitor
Okay, go ahead and enjoy the perfectly optimized website speed and flexibility from Redis Cache. Using Redis for a Wordpress website can offer many outstanding performance benefits, but at the same time requires in-depth knowledge and management skills for successful implementation. If you have enough experience and knowledge, configure Redis Cache to take advantage of the flexibility and speed that Redis offers, and your website will be more powerful than ever.