{"id":2908,"date":"2017-07-28T00:00:00","date_gmt":"2017-07-28T00:00:00","guid":{"rendered":"http:\/\/ssdnodes.billabailey.com\/2017\/07\/28\/tutorial-using-redis-as-cache-for-wordpress\/"},"modified":"2025-05-18T19:36:04","modified_gmt":"2025-05-18T19:36:04","slug":"redis-cache-wordpress","status":"publish","type":"post","link":"https:\/\/www.ssdnodes.com\/blog\/redis-cache-wordpress\/","title":{"rendered":"Redis Cache For Speeding Up Your WordPress Website"},"content":{"rendered":"<p>A few weeks back, we wrote about how <a href=\"https:\/\/www.ssdnodes.com\/blog\/tutorial-speeding-up-mysql-without-using-the-cpu\/\">reducing MySQL queries<\/a>, and suggested that Redis was one available option for those who wanted to take their caching to the next level. Now, a quick tutorial on installing Redis and configuring WordPress to communicate with Redis as a caching layer.<\/p>\n<p><a href=\"https:\/\/redis.io\/\" target=\"_blank\" rel=\"noopener\">Redis<\/a> is an open source data structure store, and it's popular among WordPress users for its simplicity and highly optimized approach to caching, which can help pages load significantly faster.<\/p>\n<p>Why caching? In a normal WordPress installation, when a user browses to the index page (<code>index.php<\/code>), the MySQL database is queried for the posts within the loop. This is usually pretty quick, but if you have multiple loops or many concurrent users, the sheer volume of database queries can become overwhelming for your VPS. A caching layer like Redis will query the database once and create a plain HTML file that it will serve instead of normal <code>index.php<\/code> file, which will boost page loads and dramatically cut back on database queries.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>A VPS running Ubuntu 16.04<\/li>\n<li>A working WordPress installation\u2014check out our previous guides on a <a href=\"https:\/\/www.ssdnodes.com\/blog\/tutorial-wordpress-lemp-ubuntu\/\">bare WordPress installation<\/a>, using <a href=\"https:\/\/www.ssdnodes.com\/blog\/install-easyengine-wordpress\/\">EasyEngine<\/a> or\u00a0<a href=\"https:\/\/www.ssdnodes.com\/blog\/getting-started-docker-vps\/\">Docker<\/a>.<\/li>\n<\/ul>\n<h2>Step 1. Installing Redis<\/h2>\n<p>Redis is available in the default package repositories for all our OS options, so installation is a breeze.<\/p>\n<pre><code class=\"language-shell hljs\"><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> sudo add-apt-repository ppa:chris-lea\/redis-server<\/span>\n<span class=\"hljs-meta\">$<\/span><span class=\"bash\"> sudo apt-get update<\/span>\n<span class=\"hljs-meta\">$<\/span><span class=\"bash\"> sudo apt-get install redis-server php-redis<\/span>\n<\/code><\/pre>\n<p>Now, we'll quickly verify that Redis was installed correctly by running <code>redis-cli<\/code>. If you see the prompt change to <code>127.0.0.1:6379&gt;<\/code>, you're off to a good start, and if you run the <code>ping<\/code> command, you should see <code>PONG<\/code> in response.<\/p>\n<pre><code class=\"language-shell hljs\"><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> redis-cli<\/span>\n127.0.0.1:6379&gt; ping\nPONG\n<\/code><\/pre>\n<h2>Step 2. Configuring Redis<\/h2>\n<p>Next, we need to set up a few configurations to make PHP and Redis work together, and for Redis to work as a cache. Open the <code>\/etc\/redis\/redis.conf<\/code> file in the editor of your choosing, and add the following lines to the end of the file.<\/p>\n<pre><code class=\"hljs nginx\"><span class=\"hljs-attribute\">maxmemory<\/span> 256mb\nmaxmemory-policy allkeys-lru\n<\/code><\/pre>\n<p>The RAM allotted in the <code>maxmemory 256mb<\/code> line can be increased according to your needs.<\/p>\n<p>Finally, we'll restart the Redis server.<\/p>\n<pre><code class=\"language-shell hljs\"><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> sudo service redis-server restart<\/span>\n<\/code><\/pre>\n<h2>Step 3. Connecting WordPress to Redis<\/h2>\n<p>The easiest way to connect WordPress to Redis is by using a popular plugin called <a href=\"https:\/\/wordpress.org\/plugins\/redis-cache\/\" target=\"_blank\" rel=\"noopener\">Redis Object Cache<\/a>. Simply install the plugin via the WordPress dashboard and then navigate to the settings. You should see a screen like the following.<\/p>\n<p><img decoding=\"async\" title=\"Enabling the cache\" src=\"https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2017\/07\/20170727_redis-plugin.png\" alt=\"Enabling the cache\" \/><\/p>\n<p>The plugin immediately recognizes our Redis installation, and all you have to do is click the <strong>Enable Object Cache<\/strong> button to start using Redis as a caching layer for WordPress.<\/p>\n<h2>Step 4. Double-check that the cache is working<\/h2>\n<p>Before we move on, let's take a moment to ensure that Redis is actually being used as a caching layer for WordPress. Type in the following command, which will enable a real-time log of Redis activity.<\/p>\n<pre><code class=\"language-shell hljs\"><span class=\"hljs-meta\">$<\/span><span class=\"bash\"> redis-cli monitor<\/span>\n<\/code><\/pre>\n<p>If the cache is working properly, you'll start to see output like the following:<\/p>\n<pre><code class=\"hljs json\"><span class=\"hljs-number\">1501199997.504554<\/span> [<span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>:<span class=\"hljs-number\">58530<\/span>] <span class=\"hljs-string\">\"PING\"<\/span>\n<span class=\"hljs-number\">1501199997.505818<\/span> [<span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>:<span class=\"hljs-number\">58530<\/span>] <span class=\"hljs-string\">\"GET\"<\/span> <span class=\"hljs-string\">\"wp_:default:is_blog_installed\"<\/span>\n<span class=\"hljs-number\">1501199997.509033<\/span> [<span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>:<span class=\"hljs-number\">58530<\/span>] <span class=\"hljs-string\">\"GET\"<\/span> <span class=\"hljs-string\">\"wp_:options:notoptions\"<\/span>\n<span class=\"hljs-number\">1501199997.509179<\/span> [<span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">127.0<\/span><span class=\"hljs-number\">.0<\/span><span class=\"hljs-number\">.1<\/span>:<span class=\"hljs-number\">58530<\/span>] <span class=\"hljs-string\">\"GET\"<\/span> <span class=\"hljs-string\">\"wp_:options:alloptions\"<\/span>\n<\/code><\/pre>\n<p>Hit <code>Ctrl<\/code> + <code>c<\/code> to end output.<\/p>\n<p>And with that, your WordPress implementation should be cached and ready to serve pages faster than ever before! Proof that not every speed boost needs to come with incredibly complex administration\u2014there's a good reason why so many people love and use Redis on their VPS.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick tutorial on installing Redis and configuring Wordpress to communicate with Redis as a caching layer.<\/p>\n","protected":false},"author":20,"featured_media":2914,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18,30],"tags":[271,196],"class_list":["post-2908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-tutorials","tag-redis","tag-wordpress"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/2908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/comments?post=2908"}],"version-history":[{"count":3,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/2908\/revisions"}],"predecessor-version":[{"id":13058,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/2908\/revisions\/13058"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media\/2914"}],"wp:attachment":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media?parent=2908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/categories?post=2908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/tags?post=2908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}