Switching Redis from TCP port to UNIX socket in Ubuntu 16.04, with Nextcloud running under Apache

This presumes that Apache, Nextcloud and Redis have already been installed, with Redis running on the default TCP port 6379 and Nextcloud is configured to use that port.

When switching from using a port to using a socket, any “Redis Server Went Away” issues that crop up in Nextcloud are probably due to file permissions: Apache’s www-data user needs access to Redis’ socket file inside /var/run/redis. Here’s the full list of steps I took to make it work:

  1. open ${NEXTCLOUD_WWW_ROOT}/config/config.php in a text editor
  2. turn on maintenance mode by setting 'maintenance' => true and saving the file
  3. in the 'redis' array,
    1. set 'host' => '/var/run/redis/redis.sock'
    2. set 'port' => 0
  4. save changes
  5. open /etc/redis/redis.conf in a text editor
  6. change port 6379 to port 0
  7. uncomment unixsocket /var/run/redis/redis.sock
  8. uncomment unixsocketperm 700
  9. change unixsocketperm 700 to unixsocketperm 770
  10. save changes
  11. sudo adduser www-data redis
  12. sudo systemctl restart apache2 (for the added group to take effect)
  13. sudo systemctl restart redis-server
  14. open ${NEXTCLOUD_WWW_ROOT}/config/config.php in a text editor
  15. turn off maintenance mode by setting 'maintenance' => false and saving the file

There’s no need to change the value of open_basedir, or to tweak Apache’s site configuration (but do remember step #12 above: restart Apache after adding www-data to the redis group).