D8 on LEMP with Redis, Ubuntu 16.04

The solution

Turned out a standard apt-get install php-redis is adequate.  Then you need to activate redis for php7.0-fpm by symlinking:

ln -s /etc/php/7.0/mods-available/redis.ini /etc/php/7.0/fpm/conf.d/20-redis.ini


Ubuntu 16.04 Web Server with Nginx, MariaDB, Redis, and Drupal 8 Read more at http://www.geoffstratton.com/ubuntu-1604-web-server-nginx-mariadb-redis-and-drupal-8#p3YOaKeK5kGrqe5g.99

As mentioned in Ubuntu 16.04 and Drupal 7 article, the Ubuntu 16 LTS version has some nice upgrades, including PHP 7, MySQL 5.7, Nginx 1.10, Apache 2.4.18, and MariaDB 10.0.x. Let's see if we can make it work with Nginx, PHP-FPM, MariaDB, and Drupal 8, using the Redis key-value store as the backend cache instead of Drupal's cache tables. The procedure is largely the same as before, with a few differences for the new Drupal version.

Set Up Nginx, MariaDB, and PHP-FPM

root@ubuntu:/# apt-get install nginx php mariadb-server

If you nginx doesn't install, and you get the following in /var/log/nginx/error.log:
mkdir() "/var/cache/nginx/microcache" failed (2: No such file or directory)

then create the /var/cache/nginx directory by hand.

This installs PHP-FPM automatically. Next we need to install a MariaDB-compatible PHP database connector, and some PHP extensions for Drupal:

root@ubuntu:/# apt-get install php-gd php-xml php-mysqli

PHP 7 on Ubuntu 16.04 includes the Zend OPcache:

root@ubuntu:/# php -v

PHP 7.0.4-7ubuntu2 (cli) ( NTS )

Copyright (c) 1997-2016 The PHP Group

Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Set a root password for MariaDB:

root@ubuntu:/# mysql -uroot

MariaDB [(none)]> use mysql;

Database changed

MariaDB [mysql]> UPDATE user SET PASSWORD=PASSWORD("my-password") WHERE user='root';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Ensure PHP-FPM is listening on the correct socket:

root@ubuntu:/# vi /etc/php/7.0/fpm/pool.d/www.conf

# look for the listen directive

listen = /run/php/php7.0-fpm.sock

Finally, let's edit /etc/nginx/sites-available/default and tell Nginx to pass PHP requests along to PHP-FPM. I'm just using the default site and location (/var/www/html) here in the interest of getting this up and running quickly.

server {

        listen 80 default_server;

        listen [::]:80 default_server;


        root /var/www/html;


        # Add index.php to the list if you are using PHP

        index index.php;

        #index.html index.htm index.nginx-debian.html;


        server_name _;


        location / {

                # First attempt to serve request as file, then

                # as directory, then fall back to displaying a 404.

                # setting up rewrite here for Drupal's clean_urls

                try_files $uri @rewrite;

                # $uri/ =404;



        # define rewrite

        location @rewrite {

                rewrite ^ /index.php last;




        location ~ \.php$ {

                include snippets/fastcgi-php.conf;


        #      # With php7.0-cgi alone:

        #      fastcgi_pass;

               # With php7.0-fpm:

               fastcgi_pass unix:/run/php/php7.0-fpm.sock;



Finally, chown your web directories (/var/www/html) so they're accessible to the Debian/Ubuntu web user (www-data:www-data).

root@ubuntu:/# chown -hR www-data:www-data /var/www/html

Installation of Drupal 8

Follow the standard guidelines from Drupal.org(link is external). I typically use the Minimal install to start with a basic set of modules. If Drupal 8 reports any errors, you may need to install one or more additional extensions for your version of PHP.

The Twig C Extension reported on Drupal 8's status page is not compatible with PHP 7(link is external), and may not be needed anyway(link is external). For now I wouldn't worry about it.

For Drupal 8's trusted_host settings, edit your site's settings.php:

$settings['trusted_host_patterns'] = array(




Installation of Redis

Now that Drupal is up and running, let's install Redis and a PHP connector for it (we're using the PhpRedis extension here):

root@ubuntu:/# apt-get install php-redis redis-server

You can start Redis manually by running redis-server, and if it's daemonized (this is the default) it'll run automatically on startup:

13515:M 21 Apr 13:06:50.023 # Server started, Redis version 3.0.6

13515:M 21 Apr 13:06:50.023 * The server is now ready to accept connections on port 6379

The Redis default settings are sufficient for our purposes, although the official documentation(link is external) explains everything in detail.

Next, install and enable the Redis Drupal 8 module(link is external) (currently in alpha, so watch for further changes), and add some directives to your site's settings.php to tell Drupal to use Redis for caching instead of the Drupal cache tables:

$settings['redis.connection']['interface'] = 'PhpRedis';

$settings['redis.connection']['host'] = '';

$settings['cache']['default'] = 'cache.backend.redis';

There's no need to override the default hostname here unless you specified a Unix socket or you're running the Redis server on a different machine. You can see Redis working by running the Redis monitor(link is external) as you click around your Drupal site (/usr/bin/redis-cli).

1461263488.591916 [0] "HGETALL" "9e7becd0db04b05520cc75f45400fd26:cache_menu:links:navigation:tree-data:en:9bd1605e2280833450478f9083b7f8714c2fa28f1012455e2744e5af1a13eec5"

1461263488.592534 [0] "HGETALL" "9e7becd0db04b05520cc75f45400fd26:cache_menu:links:management:page:admin/people:e

So now you're running Drupal 8 with a highly scalable web server, opcode caching, a key-value database store, and the high-performance Percona-XtraDB storage engine, on an Ubuntu 16.04 system supported by security updates until 2021.

root@ubuntu:/# netstat -ntlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

tcp        0      0*               LISTEN      1213/mysqld

tcp        0      0*               LISTEN      1035/redis-server 1

tcp        0      0    *               LISTEN      1049/nginx -g daemo

tcp        0      0    *               LISTEN      924/sshd

tcp6       0      0 :::80                   :::*                    LISTEN      1049/nginx -g daemo

tcp6       0      0 :::22                   :::*                    LISTEN      924/sshd


image: http://www.geoffstratton.com/sites/default/files/images/drupal8.jpg

For SSL, particularly with Varnish added in, look here.

Read more at http://www.geoffstratton.com/ubuntu-1604-web-server-nginx-mariadb-redis-and-drupal-8#p3YOaKeK5kGrqe5g.99

Debugging variables in nginx configuration

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
    add_header X-debug-message "A static file was served" always;

location ~ \.php$ {
    add_header X-debug-message "A php file was used" always;