1. Home
  2. Performance
  3. How to Enable Memcached or Redis on Drupal with LiteSpeed Cache

How to Enable Memcached or Redis on Drupal with LiteSpeed Cache

In this article, we will look at how to activate Memcached or Redis on Drupal with LiteSpeed Cache.

Note: The glossary contains explanations on a wide range of subjects and can be consulted to clarify certain terms.

Prerequisites

Make sure Composer is installed (if needed, please refer to How to install Composer and PHPMailer).

Enter the following address in your web browser: https://mg.n0c.com/en/.

How to Activate Memcached on Drupal

Preparations for Memcached

Before installing the Memcached module, please follow these instructions:

  1. Activate Memcached, as explained in How to use Memcached.
  2. Take note of the Path to Linux sock to establish the link between Memcached and your web application (path /home/N0C_USERNAME/.memcached/memcached.sock), where N0C_USERNAME must be replaced by your real username:
  1. Confirm that the Memcached server has been started (the Play button is automatically grayed out):
  1. Go to Languages -> PHP, and activate the MEMCACHE and MEMCACHED extensions, as explained in How to use the PHP interface:
  1. Go to Files -> Terminal to connect to the N0C hosting terminal:
  1. Change the directory for the Drupal installation folder (here, public_html) by typing:
cd public_html

Installing the Memcached Module

You now need to install the Memcached module itself.

  1. Still using the N0C hosting terminal and from the public_html working folder, run this command:
composer require drupal/memcache
  1. You will get an installation confirmation:

Activating the Memcached Module

Once you have installed the Memcached module, you need to activate it.

  1. Log on to Drupal administration at https://yoursite/admin, where yoursite is the name of your website.
  2. Once in the admin interface, go to Manage -> Extend and check Memcache:
  1. At the bottom of the page, click on the Install button.
  2. If the installation is successful, Drupal will return a notification of success.

Configuring Memcache in Drupal

Once the Memcache module has been activated, you need to configure it in Drupal.

  1. Using N0C’s terminal or file editor, open the file sites/default/settings.php and put the following code at the end of the file (the code contains the socket path, which must be replaced by the path to the Linux socket noted in the Preparations for Memcached):
$settings['cache']['default'] = 'cache.backend.memcache';// Use UNIX socket instead of IP address and port
$settings['memcache']['servers'] = ['unix:///home/N0C_USERNAME/.memcached/memcached.sock'=> '0']; // Replace with your socket path and use port '0'// Configure cache bins
$settings['memcache']['bins'] = ['default' => 'default','cache' => 'cache','config' => 'config','data' => 'data',];// Key prefix to avoid conflicts
$settings['memcache']['key_prefix'] = 'drupal_';
  1. In the terminal of the hosting account, check that the cache is working properly by doing a clear cache :
[quuwmcjj@hc-probear-staging public_html]$  ./vendor/bin/drush cache:clear

 Choose a cache to clear [render]:
  [0] drush
  [1] theme-registry
  [2] router
  [3] css-js
  [4] render
  [5] plugin
  [6] bin
  [7] container
  [8] views
 > 0

 [success] 'drush' cache was cleared.
[quuwmcjj@hc-probear-staging public_html]$ 

This completes the activation of Memcached for Drupal.

How to activate Redis on Drupal

Preparations for Redis

Before installing the Redis module, please follow these instructions.

  1. Activate Redis, as explained in How to use Redis.
  2. Take note of the Path to Linux sock to establish the link between Redis and your web application (path /home/N0C_USERNAME/.redis/redis.sock), where N0C_USERNAME must be replaced by your real username:
  1. Confirm that the Redis server has been started (the Play button is automatically grayed out) :
  1. Go to Languages -> PHP, and activate the REDIS extension:
  1. Go to Files -> Terminal to connect to the N0C hosting terminal:
  1. Change the directory for the Drupal installation folder (here, public_html) by typing:
cd public_html

Installing the Redis module

Now you need to install the Redis module itself.

  1. Still using the N0C hosting terminal and from the public_html working folder, run this command:
composer require drupal/redis
  1. You will receive a confirmation of installation:

Activating the Redis Module

After installing the Redis module, you need to activate it.

  1. Log on to Redis administration at https://yoursite/admin, where yoursite is the name of your website.
  2. Once in the admin interface, go to List and check Redis:
  1. Click on the Install button at the bottom of the page.
  2. If the installation is successful, Drupal will return a notification of success.

Configuring Redis in Drupal

Once the Redis module has been activated, you need to configure it in Drupal.

  1. Using the terminal or N0C’s file editor, open the file sites/default/settings.php and put the following code at the end of the file (the code contains the path to the socket, which must be replaced by the path to the Linux socket noted in the Preparations for Redis):
// Redis configuration 
$settings['redis.connection']['interface'] = 'PhpRedis'; // Make sure the PhpRedis extension is installed 
$settings['redis.connection']['host'] = '/home/N0C_USERNAME/.redis/redis.sock'; // Full path to Unix socket 
$settings['redis.connection']['port'] = 0; // Port 0 as this is a Unix socket, no need to specify a port 
$settings['redis.connection']['database'] = 0; // Specifies the Redis database to use (default 0) // Configure caching to use Redis 
$settings['cache']['default'] = 'cache.backend.redis'; // If you're using Redis for sessions, add this configuration too: $settings['session_storage'] = 'redis'; $settings['redis. session_storage']['interface'] = 'PhpRedis'; $settings['redis.session_storage']['host'] = '/home/quuwmcjj/.redis/redis.sock'; // Unix socket path $settings['redis.session_storage']['port'] = 0; // No port to specify for a Unix socket.
  1. In the terminal of the hosting account, check that the cache is working properly by doing a clear cache:
[quuwmcjj@hc-probear-staging public_html]$  ./vendor/bin/drush cache:clear

 Choose a cache to clear [render]:
  [0] drush
  [1] theme-registry
  [2] router
  [3] css-js
  [4] render
  [5] plugin
  [6] bin
  [7] container
  [8] views
 > 0

 [success] 'drush' cache was cleared.
[quuwmcjj@hc-probear-staging public_html]$

This completes the activation of Redis for Drupal.

Updated on February 5, 2025

Related Articles