Redis can be used as a powerful caching system to use with Drupal. This article will show you how to set up a Redis with docker for your Drupal project.
If you are new to how to set up Docker for Drupal, I strongly suggest you read first the article Dockerize your Drupal environment
To set up Redis with Drupal, you’ll need to follow these steps:
Install Redis image on your Docker project:
You need to set up a volume to redis save the data persistently. You use the key volume for this proposal. You also need to add the service redis. This is an example of the docker-compose file
docker-compose.yml
volume:
redis-cache:
services:
redis:
image: redis:6.2.7
restart: always
ports:
- 6379:6379
command: --port 6379
expose:
- 6379
volumes:
- redis-cache:/data:delegated
You also need to install the Redis PHP Extension from your Dockerfile. In this example, I used the php7.4 and apache as my based system, and then I installed redis from pecl package
dockerfile
FROM php:7.4-apache
ARG env
#To allow install php package https://hub.docker.com/_/php
RUN rm /etc/apt/preferences.d/no-debian-php
#Installation of Redis
RUN pecl install redis \
&& docker-php-ext-enable redis
If you are using the recommended compose system to manage your dependencies, you can use the following command from your project root.
composer require drupal/redis
and then enable the module with drush
drush en redis
Otherwise, you need to install and configure Redis Drupal Module using the following steps:
- Log in to your Drupal site as an administrator.
- Go to the Drupal website and download the Redis module (https://www.drupal.org/project/redis).
- Extract the downloaded module and place it in your Drupal modules directory (usually sites/all/modules).
- Enable the Redis module by navigating to
Admin > Extendin your Drupal administration interface.
Once the module is installed, you need to enable it. You can do it with the drush command.
drush en redis.
Now, you need to set up the project. In the setting file of your Drupal you can add the following code.
/**
* Redis config
*
* Configuration Redis for the cache
**/
try {
$redis = new Redis();
$redis->connect('Redis', 6379);
if ($redis->IsConnected()) {
$redis->auth(NULL);
$response = $redis->ping();
if ($response) {
# Configuration Redis for the cache
$settings['redis.connection']['host'] = 'redis';
$settings['redis.connection']['password'] = NULL;
$settings['redis.connection']['port'] = '6379';
$settings['redis.connection']['instance'] = 'cache';
$settings['redis.connection']['interface'] = 'PhpRedis';
$settings['cache']['default'] = 'cache.backend.redis';
$settings['container_yamls'][] = 'modules/redis/example.services.yml';
$conf['redis_perm_ttl'] = 2592000;
$settings['redis_compress_length'] = 100;
$settings['redis_compress_level'] = 3;
}
}
} catch (Exception $e) {
}
That is it!!! You can check that everything is working properly by going to the redis report page at /admin/reports/redis If everything is fine you should see something like this one.
Thank you for reading this article
