Install and Configure Latest Redis Cache for WordPress

Install and Configure Latest Redis Cache for WordPress

Redis-server is a caching system that speeds up WordPress using object cache. Benchmarks show ridiculous speed increases with the PECL Redis extension for PHP. I use Redis caching and Varnish caching for this site and have found it to be the best combination on my 512 MB Digital Ocean droplet for $5 a month.

This guide is for Ubuntu and Debian based systems (Wheezy 7, Jessie 8, Trusty 14.x and Vivid 15.x and later) but should work on CentOS, Fedora and others by making small adjustments to package managers and paths. It provides a secure Redis installation for WordPress in light of recent telnet exploits – no easy root access can be gained from hackers if you follow this configuration tutorial.

Note: If you are on Ubuntu 16.04 please follow my new Redis guide.

Install and Configure Latest Redis Cache for WordPress

Using Redis object cache with WordPress requires two components speed boost your site

  • Redis Server which stores the WordPress object cache
  • Redis PHP extension (phpredis or predis) for php to communicate with the redis-server for storing and retrieving cache

phpredis is the the native c extension so is the fastest and obvious choice, the instructions for compiling from source follow the Redis server installation section.

If you prefer to use predis (redis extension written in php – (read slower)) then you do not need to install phpredis as the WordPress Redis cache plugin will default to predis.

Install Redis Server

This block of code installs the compilation tools, grabs the latest stable redis, builds and installs it. The latest version number can be found here

Create a non-privileged Redis user for maximum security which has no login, valid shell and limited home directory to its PID

Ensure the redis user is there by checking the shadow file

If it isn’t there use the add user command again

Make some changes to the redis configuration

Set daemonize to yes for init.d and systemd services

Change bind to only listen on localhost: to prevent serious security issues

Set maxmemory to 50M so Redis doesn’t use more than 50 MB of RAM. Even with 40 plugins I have never seen Redis use more than 20-30 MB for WordPress object caching.

Change the policy to allkeys-lru which forces redis-server to delete old cache (least recently used objects) when the Redis server starts to run out of memory

Ctrl+X, Y and Enter to save

Create a folder for the pid file

Change permissions so the redis user owns it

Time to start Redis server on boot, only use the init.d script or the systemd script

If you are in doubt which to choose run this command, if it doesn’t say systemd use the init.d script

Redis Server Systemd Script

Create the redis-server systemd script

Paste the Redis server systemd script

Enable the redis-server systemd script

Start the redis-server systemd script

Redis-server init.d Script

Create the Redis server init.d script which may already exist

Paste the redis server init.d script unless it already exists

Make sure the redis-server init.d script is executable

Update the system to use Redis server init.d script

Start the Redis server service

Confirm Redis-server is Secure

These are a few checks to make sure Redis is running securely, first use netstat

Make sure it listening on

You can also make sure the redis process is running as the redis user we specified

If redis says root instead that would be bad, even though it’s listening on the loopback interface

Install phpredis

Install php5-development tools and git, clone the latest phpredis, build and install it.

If you are using php7 you will need the php7.0-dev package for the development tools. You will also need to add -b php7 at the end of the git clone line (not on its own new line) to use the php7 branch of the github repository.

Add the redis extension to php5-fpm for nginx or apache2

For Apache2 add Redis to its php file

For nginx with php5-fpm enable Redis like this

For nginx with php7.0-fpm

Install the WordPress Redis plugin and enable it and enjoy the speed!

Добавить комментарий

Войти с помощью: 

Ваш e-mail не будет опубликован. Обязательные поля помечены *