Enable caching in nextcloud server

This is the first of a series of tutorials to improve the performance and security of your nextcloud server installation. In this tutorial we shall install ACPu and Redis on an existing nextcloud server.

Over time, as your Nextcloud file and folders increase in size, your server performance will begin to slow. To counter this we can implement caching which will assist by maintaining the performance and speed for your server.

ACPu will handle a lot of the caching initially, leaving Redis to manage file locking. As the server content grows and ACPu demands more resources, we will configure Redis to handle distributed caching.

Stop Apache server

in order to install ACPu and Redis we will need to stop Apache server with the following:

sudo service apache2 stop

Install and configure ACPu and Redis

To install all the required software, run the following command:

sudo apt install php-apcu redis-server php-redis

Once installed we shall start off by editing the Redis configuration file /etc/redis/redis.conf. This is achieved by running the following command:

sudo nano etc/redis/redis.conf

Now find port 6379 and change it to 0. Then uncomment the following:

unixsocket /var/run/redis/redis.sock

Look for and change the permissions from 700 to 770 in the unixsocketperm directive as illustrated below:

unixsocketperm 700

Save the file with ctrl & x

We now need to add the Apache user www-data to the Redis group with the following:

sudo usermod -a -G redis www-data

Finally restart Apache server with:

sudo service apache2 restart

And start the Redis service with:

sudo service redis-server start

Now that Redis is installed and configured we now need to add the caching configuration to the nextcloud configuration file located at /var/www/html/nextcloud/config/config.php

sudo nano /var/www/html/nextcloud/config/config.php

Then add the following piece of code to the config file.

'memcache.local' => '\\OC\\Memcache\\Redis',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'filelocking.enabled' => 'true',
'redis' => 
array (
'host' => '/var/run/redis/redis.sock',
'port' => 0,
'timeout' => 0.0,
),

The server will have to be restarted for the changes to take effect but before we do that we will need to make sure that the Redis server is enabled to start on boot with:

sudo systemctl enable redis-server

Restart the server and caching will now be running.