Skip to content

Instantly share code, notes, and snippets.

@schankam
Last active December 13, 2019 16:05
Show Gist options
  • Save schankam/f80403ef91a10f69a68c to your computer and use it in GitHub Desktop.
Save schankam/f80403ef91a10f69a68c to your computer and use it in GitHub Desktop.
Drupal 7.x Redis Setup

Global overview

This guide gives you all the needed instructions to get a working Redis installation used as a Drupal Caching Backend. The following given commands assume that your server is running under a Debian/Ubuntu installation, using Apache2 as a webserver and that you have Drush installed.

Installing the Redis Server

In a terminal console, type the following command:
sudo apt-get install redis-server

Once installed, you can check if Redis is correctly installed and run by entering:
redis-cli ping
The server should reply by PONG

Installing the Redis PHP extension

As there are no packaged binaries yet for Debian/Ubuntu, you will need to get the source code and compile it by yourself. To do that, you just need to open your command line and type the following instructions:

First, make sure you already have all the requirements to compile the packaged binary:

sudo apt-get install php-pear php5-dev git-core make

Then, we do clone the last stable release of phpredis:
git clone https://github.com/nicolasff/phpredis.git

And we start the compiling process:
cd phpredis
phpize
./configure
make
make install
./mkdeb-apache2.sh

The package is now created, so we juste have to install it running dpkg:

sudo dpkg -i phpredis-x86_64.deb

Note: it is possible you get the following error while launching this command:

unable to open '/etc/php5/apache2/conf.d/redis.ini.dpkg-new': No such file or directory

If you got this error message, I found this post giving you a solution on how to fix it and make it works.

Once it is done, do not forget to restart your Apache server:

sudo service apache2 restart

You can check if phpredis has been setup correctly by calling the phpinfo() function.

Setting up Redis for Drupal

First, if the directory does not already exist, you will need to create a folder named "libraries" into the "sites/all/" directory.

Go to this folder and unzip the last Predis library, and renammed it as "predis".
To be sure you correctly put the library in the right folder, the "autoload.php" file should be situated in "sites/all/libraries/predis/".

Then, download and enable the redis module for Drupal:
drush dl redis
drush en redis -y

In your settings.php file, add the following lines:

$conf['redis_client_interface'] = 'Predis';
$conf['cache_backends'][] = 'sites/all/modules/redis/redis.autoload.inc';
$conf['cache_default_class'] = 'Redis_Cache';
$conf['cache_class_cache'] = 'Redis_Cache';
$conf['cache_class_cache_bootstrap'] = 'Redis_Cache';
$conf['cache_class_cache_menu'] = 'Redis_Cache';
$conf['cache_class_cache_block'] = 'Redis_Cache';
$conf['cache_class_cache_content'] = 'Redis_Cache';
$conf['cache_class_cache_filter'] = 'Redis_Cache';
$conf['cache_class_cache_form'] = 'Redis_Cache';
$conf['cache_class_cache_page'] = 'Redis_Cache';

// The following can be omitted unless you changed the default
$conf['redis_client_host'] = '127.0.0.1'; // default is localhost
$conf['redis_client_port'] = 6379; // default is 6379
$conf['redis_client_base'] = 0; // default database is 0
$conf['redis_client_password'] = ""; // default is no password

You can check if your setup is working correctly by displaying a page on your website, then running redis-cli from the command line and typing keys *. You should see some variables related to the drupal content (block, menu...).

/!\ IMPORTANT

Due to a change of repository structure of the Predis library, you now need to define the PREDIS_BASE_PATH in the settings.php to

define('PREDIS_BASE_PATH', DRUPAL_ROOT . '/sites/all/libraries/predis/src/');

You will also need to apply a patch to the current redis module for Drupal (When I am writting these lines, current version is 7.x-2.12), on the lib/Redis/Client/Predis.php file; Just replace the following line $filename = PREDIS_BASE_PATH . str_replace('\', '/', $classname) . '.php'; by $filename = PREDIS_BASE_PATH . str_replace('Predis\', '', $classname) . '.php';

@zenphp
Copy link

zenphp commented Sep 16, 2016

Arent PHPRedis and Predis mutually exclusive backends?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment