A few weeks back, we wrote about how reducing MySQL queries 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.
Redis 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.
Why caching? In a normal WordPress installation, when a user browses to the index page (
index.php), 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
index.php file, which will boost page loads and dramatically cut back on database queries.
- A VPS running Ubuntu 16.04
- A working WordPress installation.
Step 1. Installing Redis
Redis is available in the default package repositories for all our OS options, so installation is a breeze.
$ sudo add-apt-repository ppa:chris-lea/redis-server $ sudo apt-get update $ sudo apt-get install redis-server php-redis
Now, we’ll quickly verify that Redis was installed correctly by running
redis-cli. If you see the prompt change to
127.0.0.1:6379>, you’re off to a good start, and if you run the
ping command, you should see
PONG in response.
$ redis-cli 127.0.0.1:6379> ping PONG
Step 2. Configuring Redis
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
/etc/redis/redis.conf file in the editor of your choosing, and add the following lines to the end of the file.
maxmemory 256mb maxmemory-policy allkeys-lru
The RAM allotted in the
maxmemory 256mb line can be increased according to your needs.
Finally, we’ll restart the Redis server.
$ sudo service redis-server restart
Step 3. Connecting WordPress to Redis
The easiest way to connect WordPress to Redis is by using a popular plugin called Redis Object Cache. Simply install the plugin via the WordPress dashboard and then navigate to the settings. You should see a screen like the following.
The plugin immediately recognizes our Redis installation, and all you have to do is click the Enable Object Cache button to start using Redis as a caching layer for WordPress.
Step 4. Double-check that the cache is working
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.
$ redis-cli monitor
If the cache is working properly, you’ll start to see output like the following:
1501199997.504554 [0 127.0.0.1:58530] "PING" 1501199997.505818 [0 127.0.0.1:58530] "GET" "wp_:default:is_blog_installed" 1501199997.509033 [0 127.0.0.1:58530] "GET" "wp_:options:notoptions" 1501199997.509179 [0 127.0.0.1:58530] "GET" "wp_:options:alloptions"
c to end output.
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—there’s a good reason why so many people love and use Redis on their VPS.