Introduction: Redis as Backend NoSQL Cache for Drupal 8
This Tutorial will show how to install Redis as a backend cache on a VPS hosting plan for a Drupal 8 website with an overview of the modules needed for platform integration.
Redis is a NoSQL database that can be used as a backend cache on a LAMP web server running Drupal 8 to drastically reduce the number of MySQL database queries.
Redis can be combined with PHP caching (Memcached, Zend Opcache, APC), HTML/CSS & JavaScript caching (Adv Agg), reverse-proxy caching (NGINX), & Varnish Cache to run Drupal 8 at high performance levels for demanding apps.
Step One: Install Redis on the Web Server
HostAdvice has published a number of excellent Tutorials with step-by-step instructions for installing Redis on a Linux web server:
- How to install Redis Server on Ubuntu 18.04
- How to Install Redis on a CentOS 7 Dedicated Server or VPS
- How to Install WordPress with Nginx & Redis
With CLI tools & LAMP servers, the following commands can be used to install Redis:
Using Yum:
yum install redis
Using Apt-Get:
apt-get install redis-server
Using Sudo:
sudo apt-get install redis-server
In CentOS, the Redis extension can be selected as part of the “Select PHP Version†section of the administration:
Install Redis on the web server according to the Linux version in use and then proceed to the next step to add the integration module for Drupal 8.
Step Two: Install the PHP Redis Client (PhpRedis)
Before the Drupal 8 integration module is installed, make sure that the PECL module for the PHP Redis Client is present on the web server & enabled. Run the command:
pecl install redis
The PHP Redis Client enables the software to store PHP session variables on a web server in addition to MySQL database queries, adding an extra caching layer.
- Learn more about the PHP Redis project at GitHub.
Use the following code to install PHP Redis using Apt-Get & Git:
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
Note: PHP Redis is from an open source development team independent from the Redis NoSQL development project, but this functionality is required by the D8 module.
Step Three: Install the Redis Module for Drupal 8
Next download the Redis module for Drupal 8 and install at: /admin/modules/install or use Drush, Composer, FTP, etc. to install/enable the module according to preference.
REQUIRED FILES – DOWNLOAD:
- Redis Module: (Download Files)
The Redis module for Drupal 8 has no formal administration screen but provides the bridge for integration with the PHP Redis framework for PHP session variable caching.
Install & enable the Redis module in Drupal 8 admin or use the Drush command:
drush en redis
Consider the use of modules like Purge for management of the Redis cache settings through the Performance section of Drupal 8 administration.
Note: The back-end caching of PHP & MySQL server functions provided by Redis operates in the data center at a layer beneath HTML, CSS, & JavaScript compression.
Step Four: Configure Drupal 8 for PHP Redis Integration
In order for the Drupal 8 integration for PHP Redis to work successfully, administrators will need to add the following lines to the settings.php file in /sites/default folder:
$settings['redis.connection']['host'] = 'localhost'; $settings['redis.connection']['port'] = NULL; $settings['cache']['default'] = 'cache.backend.redis'; $settings['redis.connection']['base'] = 101;
The Redis Connection Base Variable can be any number and should be used to keep different Drupal 8 installations separate from other websites or domains running Redis on the same web server. A custom IP Address can be used in place of “localhostâ€.
For more advanced configuration of Redis NoSQL variable functions, use the code:
$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'] = 'example-text'; // 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('Drupalredis', 'modules/redis/src');
Set the Cache Prefix values with any label taxonomy and leave the other settings as default. Save the settings.php file and re-upload to the web server for production.
Step Five: Testing the Drupal 8 + Redis Integration
In order to confirm the successful integration of Redis & Drupal 8 on the web server, navigate to the Status Report page at: /admin/reports/status
Administrators should see the “Connected, using the PhpRedis client†message under the REDIS section of the Status Report. Otherwise, use the command:
redis-climonitor
The Redis CLI Monitoring Tools will automatically diagnose the installation on the web server based on the default URL configuration for the Drupal 8 domain.
Conclusion: Redis NoSQL for High Traffic Drupal 8 Websites
In production, high traffic Drupal 8 web publishers see major gains in MySQL database query reduction which reduces the backend strain on web servers for more web traffic support. The PHP Redis integration extends the caching to PHP session variables.
Combined with Memcached, Zend Opcache, APC, NGINX, & Varnish Cache solutions, high performance Drupal 8 web servers will be most optimized for page speed requirements. Redis can easily be considered a default option for Drupal 8 hosting, running on both Apache & NGINX as well as Linux or Windows equally.
Check out these top 3 Drupal hosting services:
- Your query to the best web hosting can end by clicking on this link.