How to Setup a Remote Redis 6 Server with Drupal 8

This guide will show you how to deploy a remote Redis 6 server, install the Drupal Plugin for Redis and configure it to use password authentication.

We will also discuss adding an option to provide a failover, in case of a Redis failure which would normally cause Drupal to fail as a result.

We have recently noticed the necessity to investigate options that improve performance for Drupal hosted clients on our Cloud Solutions. One of the options is to include a Redis 6 server into the deployment environment.

Installing Redis node

The first stage is to deploy the Redis node into your existing infrastructure build, through the Cloud Solutions environment builder tool:

At this point, all you need to do is enable the Redis service, select the number of Cloudlets you wish to allow, and deploy it.

With UKHost4u Cloud solutions, Redis is a one click install along with the Load Balancer, Web Server and any other components needed. If you are not using our cloud solution, you would need to deploy your own setup at this stage.

Once the Redis server is deployed, we now need to configure the web server to support this (in this case we’re using Apache).

The first thing we are going to do is modify the php.ini to add support for Redis extension; normally the php.ini would be located under /etc/php.ini but this could vary based on your web server technology:

I am going to at this stage change:

;extension=redis.so

To

extension=redis.so

I will then save this change and restart Apache.

This will give us support for PHPRedis, and will be used for our integration of Drupal and Redis Server.

At this point we have completed the server side configuration, which has included installing Redis and adjusting the web server to support it in the php.ini.

The next stage is going to be mainly focused on the Drupal side of the installation.

Installing Redis Drupal Module

Next we need to install the module for Redis and enable it within Drupal. This can be done using a number of methods, such as Composer, Drush, etc, but for this example we will do it using the Web GUI.

You will first of all need to locate the latest copy of the extension from Drupal direct:

https://www.drupal.org/project/redis

Once you have this, navigate to under the admin section and select -> Extend -> Install new module.

You can also install this using composer and the following command:

composer require 'drupal/redis:^1.5'

The next stage is to enable the module.

To do this we go to Admin -> Extend and search for Redis. Once you find it, tick the box to enable it and click install.

At this stage we have installed Redis into drupal but have not actually configured it. To do the configuration for Drupal we need to add some lines into the settings.php file which should be located under /sites/default/settings.php

To understand this better, we need to understand some of the details of our Redis deployment.

As we used a one click build to deploy Redis, we are going to use the details from the welcome email which includes the IP and the password to be used. If you manually installed Redis you should have already configured this.

For this example, let use:

  • IP: 172.1.2.3
  • Password: Pass123

For this part we are going to offer two solutions.

Solution #1

First, a very simple config for Redis to be able to connect. This is enough to be able to offer Drupal with Redis, but as we may have a situation where the redis server could technically fail we also have a solution which will test for a failure, and if detected will default back to using the Drupal Cache and stop the site going offline.

$settings['redis.connection']['interface'] = 'PhpRedis';
$settings['cache']['default'] = 'cache.backend.redis';
$settings['redis.connection']['host'] = '172.1.2.3';
$settings['redis.connection']['port'] = '6379';
$settings['redis.connection']['password'] = "Pass123";
$settings['queue_default'] = 'queue.redis';
$settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';
$class_loader->addPsr4('Drupal\redis\', 'modules/contrib/redis/src');

In this example you need to set the host / port and password. The rest of the configuration does not need any other changes.

It is also helpful at this point to clean the cache which can also be done under: Admin -> System -> Development -> Performance

Once this is setup and saved, you can check if Redis is running correctly under in Drupal under: Admin -> Reports -> Redis

Solution #2

The final option we will discuss is using a little extra code to confirm the Redis server is live before enabling it as the Drupal cache.

We do this using a quick test script which will connect to the redis remote node and issue a ping command. If Redis resounds correctly with Pong, it will enable the Redis cache and remove the Drupal cache. On the other hand if this test fails it will remove Redis settings and restore Drupal cache to limit any outages.

/**
Redis Configuration.
*/
try {
   $redis = new Redis();
   $redis->connect('172.1.2.3', 6379);
   if ($redis->IsConnected()) {
       $redis->auth('Pass123');
       $response = $redis->ping();
       if ($response) {
           $settings['redis.connection']['interface'] = 'PhpRedis';
           $settings['cache']['default'] = 'cache.backend.redis';
           $settings['redis.connection']['host'] = '172.1.2.3';
           $settings['redis.connection']['port'] = '6379';
           $settings['redis.connection']['password'] = "Pass123";
           $settings['queue_default'] = 'queue.redis';
           $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';
           $class_loader->addPsr4('Drupal\redis\', 'modules/contrib/redis/src');
       }
   }
 }catch(Exception $e) {  
 }

We hope that you have found this guide helpful. While doing our research we found the documentation for Drupal and Redis to be limited so we wanted to cover this in more detail.

Leave a Reply

Your email address will not be published.