Updated on Jun 17, 2020
Redis and object caching will exponentially speed up your WordPress page load time with every subsequent visit.
In this guide, we will cover how to configure Redis as a cache for WordPress to lessen all redundant and time-consuming database queries that are used for rendering a WordPress page. The result of this will be a WordPress site that is exceptionally faster, uses much fewer database resources, in addition to providing a tunable and persistent cache. Keep in mind that the guide applies to CentOS 6 and cPanel.
What you can find in the post:
Whenever a web page is cached, the elements of that particular page such as images, stylesheets, and other content, are loaded once, and then stored in what we call a “cache.” The cache memorizes everything that was loaded, creates a static version of it, and then can serve that version much faster the next time the page loads. The result is a much faster page load time and fewer server resources being used.
Every time we access a WordPress page, a query is being executed to the WordPress database to fetch the page’s content. By using Redis caching, the results of this query are stored into the Redis-managed block. Upon additional requests of the same page, the data will be retrieved from the Redis cache, eliminating the need for execution of the MySQL query, which could be essential even if we have only a few visitors on our WordPress site. Retrieving the data via Redis directly from the memory can make our page load speed blazing fast.
The solution above is most beneficial for WordPress sites with a large number of simultaneous requests as reducing the number of concurrent queries dramatically improves the performance, provides better utilization to the server, and eliminates the MySQL bottleneck. This way, the site can handle more traffic without the need for more powerful hardware.
Also, caching with Redis eliminates any need for flushing the cache whenever we publish new content manually. In case we edit a page, or we post new content to our WordPress site, the Redis cache for the posted content would be invalidated to avoid “dirty” cache. The content will be updated after the next page load.
Redis is an open source advanced key-value cache store. It can operate as both cache and an in-memory store. Usually, Redis is referred to as a data structure server, and it’s known for having outstanding performance as it works with an in-memory dataset. Although similar results can be witnessed with some other cache software like Memcached, Redis is one of the best choices. It’s not too hard to set up and use. Additionally, we are going to cover how to set up Redis on CentOS 6 server with cPanel, how to set up php-redis
, as well as perform the needed configuration settings on our WordPress installation and boost its performance exponentially.
Websites that are highly dynamic and cannot make good use of page caching are a good target for the persistent object caching* option that is Redis. Generally, Redis would not help the load time of static blogs, news websites, and informational business websites, unless those sites use page caching poorly. However, Redis offers a great deal of support for developing efficient caching mechanisms. It takes just a couple of minutes to implement a cache mechanism and get it working with the chosen application. The outcome we get is a high-performing cache system. With it, you can have huge keys and values of objects.
*Object caching - involves storing database queries. When enabled on a WordPress site, it helps in speeding up PHP execution times, reduces the load on the database, in addition to delivering content to the site's visitors faster.
WordPress also has object caching built-in with the WP_Object_Cache class.
While on a WordPress context, we can use Redis to store the values generated by the native object cache of WordPress. We can do that in a persistent manner so that all cached objects would be reused between page loads.
In order to complete the steps below, we would need root access to the server, or a user with sudo privileges. The steps explain the installation and configuration processes on CentOS 6 with cPanel 11.48, but should also work on other cPanel versions.
Keep in mind that the following installation steps are only for FastComet Cloud VPS and Dedicated CPU Server users.
rpms
:
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm yum install –y redis
chkconfig --level 2345 redis on
Open the Redis configuration file:
vi /etc/redis.conf
and add the following two lines at the end of the file:
maxmemory 256mb maxmemory-policy allkeys-lru
If we want to allow more memory for the Redis cache, we change the maxmemory
value to the desired amount.
After we install Redis, we would need the PHP-REDIS extension. On a cPanel/WHM server, we can easily install PHP-REDIS:
yes | /opt/cpanel/ea-php56/root/usr/bin/pecl install igbinary igbinary-devel redis yes | /opt/cpanel/ea-php70/root/usr/bin/pecl install igbinary igbinary-devel redis yes | /opt/cpanel/ea-php71/root/usr/bin/pecl install igbinary igbinary-devel redis yes | /opt/cpanel/ea-php72/root/usr/bin/pecl install igbinary igbinary-devel redis yes | /opt/cpanel/ea-php73/root/usr/bin/pecl install igbinary igbinary-devel redis yes | /opt/cpanel/ea-php74/root/usr/bin/pecl install igbinary igbinary-devel redis
/scripts/restartsrv_apache_php_fpm service httpd restart service redis restart
/opt/cpanel/ea-php56/root/usr/bin/php -m | grep redis /opt/cpanel/ea-php70/root/usr/bin/php -m | grep redis /opt/cpanel/ea-php71/root/usr/bin/php -m | grep redis /opt/cpanel/ea-php72/root/usr/bin/php -m | grep redis /opt/cpanel/ea-php73/root/usr/bin/php -m | grep redis /opt/cpanel/ea-php74/root/usr/bin/php -m | grep redis
redis
Setting up WordPress to work with Redis requires just a few changes:
wp-content/
directory.object-cache.php.txt
to object-cache.php
and place the file under your wp-content/
directory. We do not need to perform any modifications on the file to make it work, but we may always review the settings.wp-config.php
file and add the following lines at the end of the Authentication Unique Keys and Salts. section:
define(‘WP_CACHE_KEY_SALT’, ‘example.com’); define(‘WP_CACHE’, true);
example.com
with your actual domain name or any randomly generated string./etc/init.d/httpd restart /etc/init.d/redis restart
redis-cli monitor
OK 1428089226.419271 “monitor” 1428089226.419272 “GET” “example.comwp_:options:alloptions” 1428089226.419479 “EXISTS” “example.comwp_:options:notoptions” 1428089226.419548 “GET” “example.comwp_:options:notoptions” 1428089226.419651 “EXISTS” “example.comwp_:options:alloptions” 1428089226.419729 “GET” “example.comwp_:options:alloptions” 1428089226.422514 “EXISTS” “example.comwp_:options:notoptions” 1428089226.422592 “GET” “example.comwp_:options:notoptions” 1428089226.422701 “EXISTS” “example.comwp_:options:alloptions” 1428089226.422770 “GET” “example.comwp_:options:alloptions” 1428089226.427510 “EXISTS” “example.comwp_:options:notoptions”To exit - press CTRL-C.
redis-cli flushall
After you've learned how to install Redis, you are ready to boost the performance of your site with Redis and the FastComet Scalable Cloud VPS Hosting with ultra-fast SSDs. All FastComet WordPress Hosting plans are fully managed with 24/7 Technical Support so you can consult with our experts on how to improve your WordPress performance and get started with Redis. Still, in case there is something that you need help with, always feel free to open a support ticket.
We hope you find this article useful. Discover more about FastCloud - the top-rated Hosting Solutions for personal and small business websites in four consecutive years by the HostAdvice Community!