Drupal 8 is packed with features designed to optimize performance (e.g. BigPipe). However, for busier sites, it may be desirable, or even necessary, to get a little bit more horsepower. One of the easiest ways to do this is to use a "caching backend", such as Redis, a very popular and reliable NoSQL platform. In this article, we'll explore just how simple it is to install and configure Redis as a caching backend for Drupal 8 on a Linux host.
For the purposes of this article, we'll assume that you have Drupal 8 up and running, and that you are familiar with some of the more basic principles of Drupal 8 (such as rebuilding the Drupal cache).
First, let's see if you have Redis installed already ... who knows? You might!
$ yum list installed | grep -i redis redis.x86_64 2.8.19-2.el7 @epel
If you see something like the above in the output, you are probably ready to roll and can likely skip this step. If not, use your favorite package manager to install Redis.
$ yum install redis
There are entire books and web sites dedicated to configuring and tuning Redis, and we aren't going to attempt to displace those in this short article! However, there are a few quick and dirty things you should do before starting up the Redis server process. Open your /etc/redis.conf file and let's hit a few highlights.
# You want this option set to yes, so you can just let Redis easily run in the background as a service daemonize yes # Set whatever port you want, the default value of 6379 should be fine for most environments port 6379 # You can have Redis listen for traffic over a TCP/IP socket connection, or a unix socket, or # both. The defaults are probably fine for most folks. bind 127.0.0.1 unixsocket /tmp/redis.sock unixsocketperm 700 # Very important - leave this alone unless you know what you are doing! The default value of 16 # is just fine. databases 16 # This is an important one. This sets the size of the Redis cache. Redis will use no more than # this much memory to store cached objects. The default value is really low, so I would suggest # bumping it up a bit (8mb, 16mb, etc.) Whatever you feel you can spare. maxmemory 32mb
Once you are satisfied with your configuration, go ahead and start up Redis (exact command may vary depending on your Linux distro):
$ service redis start or $ systemctl start redis.service etc.
You can test the Redis connection by trying to Telnet to the port you configured for Redis and trying a few commands.
$ telnet localhost 6379 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
Once connected type "info" and press enter. You should see a long list of various informational values about the Redis server. Type "quit" to exit.
Installing PHP Redis Client (Predis)
Ok, now that you have Redis installed, it is time to install a PHP Redis client library. Currently, only PhpRedis is supported by the Redis Module for Drupal 8 (which you will install in the next step). You can try some commands similar to the ones below, depending on your distro, or you may find you need to build it yourself. You can visit the official PhpRedis page here.
$ yum install php55w-pecl-redis.x86_64 or $ pecl install redis
Installing Redis Module for Drupal 8
This one is an easy step :) Just search the Drupal module repository for "redis", or visit this page. Install the Drupal 8 version as you would any other Drupal module.
Configuring Drupal 8
Now we're all set to configure Drupal 8 to use your fancy new Redis server! Navigate to ./sites/default and open up the settings.php file. Place these lines (tailor accordingly) at the bottom of that file:
$conf['redis_cache_socket'] = '/tmp/redis.sock'; $settings['cache']['default'] = 'cache.backend.redis'; $settings['redis.connection']['base'] = 1;
This will configure Drupal to communicate over the UNIX socket and use it as a backend cache. If you would prefer to use a TCP/IP socket connection (handy if your Redis server is on another physical host), then use lines similar to the following:
$settings['redis.connection']['host'] = '188.8.131.52'; // Your Redis instance host name or IP ('localhost' is fine) $settings['redis.connection']['password'] = "mypassword"; // If you are using passwords, otherwise, omit $settings['cache']['default'] = 'cache.backend.redis'; $settings['redis.connection']['base'] = 1;
Clear your Drupal cache, and visit the status report page. You should see an entry for Redis in there which will tell you if you are connected or not.
Monitoring Your Redis Cache
Once everything is running, you can use our free SiteCommander module for Drupal to monitor and manage your Redis cache!
We hope you've enjoyed this little tutorial! Be sure to check back often, as we publish all sorts of goodies from time to time, including our internal open source projects!
- Log in to post comments