Drupal 8 Redis Configuration

As of today (February 14, 2017) the Redis plugin on D8 is on beta 1 and appears to be pretty stable, however the documentation appears to be lacking a bit. (I know, I should contribute and maybe I will later this week). There’s a lot of different advice/instructions for setting up Redis caching with Drupal 8, some with parts right, some with parts wrong and some with webhost-specific parts. Here’s what we’ve found to get working. This all assumes an Ubuntu 14.04 or 16.04 OS, Drupal 8.2.x, PHP7 and some basic Linux knowledge.

Install Redis

add-apt-repository ppa:chris-lea/redis-server
apt-get update
apt-get install redis-server
redis-benchmark -q -n 1000 -c 10 -P 5

Install PHP Redis

apt-get install php7.0-dev git
git clone https://github.com/phpredis/phpredis.git
cd phpredis
git checkout php7
phpize
./configure
make && make install
cd ..
rm -rf phpredis
echo "extension=redis.so" > /etc/php/7.0/mods-available/redis.ini
ln -sf /etc/php/7.0/mods-available/redis.ini /etc/php/7.0/fpm/conf.d/20-redis.ini
ln -sf /etc/php/7.0/mods-available/redis.ini /etc/php/7.0/cli/conf.d/20-redis.ini
service php7.0-fpm restart

Install the Redis module in Drupal

Download from Drupal

Configure Drupal

In your sites/default/settings.php (or preferably settings.local.php (or even more preferably (however you need to manually include this file) settings.local.redis.php) ) use the following:

$settings['redis.connection']['interface'] = 'PhpRedis';                  // Can be "Predis" in the future
$settings['redis.connection']['host']      = '127.0.0.1';                 // Your Redis instance hostname
$settings['cache_prefix']                  = 'any-text-you-want';         // Optional prefix for cache entries

$settings['cache']['default']              = 'cache.backend.redis';       // The default cache engine for the site
// Always set the fast backend for bootstrap, discover and config, otherwise this gets lost when redis is enabled.
$settings['cache']['bins']['bootstrap']    = 'cache.backend.chainedfast';
$settings['cache']['bins']['discovery']    = 'cache.backend.chainedfast';
$settings['cache']['bins']['config']       = 'cache.backend.chainedfast';

$settings['container_yamls'][] = 'modules/redis/example.services.yml';
$settings['container_yamls'][] = 'modules/redis/redis.services.yml';

//Register our namespace
$class_loader->addPsr4('Drupal\\redis\\', 'modules/redis/src');

Test

In a CLI window into the server run:

redis-cli monitor

Browse your Drupal site in your web browser and you should now see a bunch of stuff appearing in your CLI window which tells you that everything’s working correctly. If you don’t see text flashing by, you’ve made a mistake or your configuration is different than mine. If you do see text, hit Ctrl + C to exit redis.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.