Performance Tune Nextcloud with Caching

Nextcloud has become one of the most widely-used on-premises cloud solutions on the planet. There’s a reason for that. This cloud software is not only open source, but it’s also scalable, extendible, and reliable. But for larger companies, looking to get as much performance as possible, Nextcloud might need a bit of tweaking out of the box. Why? Because by default Nextcloud does not enable caching.
Why would you need caching? As you probably already know, caching increases performance by storing frequently-requested objects in memory. By doing this the retrieval of those objects is considerably faster. Since we’re talking about the cloud, you could have hundreds (even thousands) of users hitting that server simultaneously. Without caching enabled, that server is going to take a significant hit as each user retrieves objects.
Fortunately, with Nextcloud, enabling caching isn’t all that challenging. It’s not as simple as clicking a checkbox in the Settings, but you can add this feature without having to jump through too many hoops.
Let’s do just that. We’ll enable caching on Nextcloud 16, running on the Ubuntu Server 18.04 platform, to help make your Nextcloud server run like a champ.
What You’ll Need
Obviously, you’ll need a running instance of Ubuntu Server. I will also assume you’ve already installed Nextcloud. If not, check out “How to Install Nextcloud 16 on Ubuntu Server 18.04”. You will also need a user with sudo privileges. That’s it. You’re ready to tune Nextcloud.
Installing Redis
Before you can configure Nextcloud caching, you must first install a caching service. We’ll use the in-memory data structure store, Redis (as Redis is also open source, scalable, and integrates well with Nextcloud).
To install Redis, log into your Nextcloud server, open a terminal (if your server has a GUI), and issue the command:
1 |
sudo apt-get install redis-server -y |
This should install without issue. Once the installation completes, start and enable Redis with the following two commands:
1 2 |
sudo systemctl start redis-server sudo systemctl enable redis-server |
Configure Redis
With Redis installed, it must now be configured. Open the config file with the command:
1 |
sudo nano /etc/redis/redis.conf |
Edit the following lines to reflect these changes:
1 2 3 |
port 0 unixsocket /var/run/redis/redis.sock unixsocketperm 700 |
Save and close the configuration file.
Before we jump into the Nextcloud configuration, we must also add the redis user to the www-data group. Without doing this, Nextcloud will fail. To add the redis user to the group, issue the command:
1 |
sudo usermod -aG redis www-data |
That’s it for the Redis configuration.
Configure Nextcloud
We now have to configure Nextcloud to make use of our newly-added Redis service. To do that, open the Nextcloud configuration with the command:
1 |
sudo nano /var/www/html/nextcloud/config/config.php |
Scroll down to the bottom of that file. The last two lines should be:
1 2 |
), ); |
Above those two lines, paste the following:
1 2 3 4 5 6 7 8 9 10 |
'memcache.local' => '\\OC\\Memcache\\Redis', 'memcache.locking' => '\\OC\\Memcache\\Redis', 'redis' => array ( 'host' => '/var/run/redis/redis.sock', 'port' => 0, 'timeout' => 0, 'password' => '', 'dbindex' => 0, ), |
The configuration should now look similar to that shown in Figure 1, below.

Figure 1: The Redis configuration added to Nextcloud.
Save and close the file. Finally, restart Apache with the command:
1 |
sudo systemctl restart apache2 |
And that’s all there is to enable caching with Redis for Nextcloud.
Enable Opcache
You can also enable opcache in the php.ini file — a recommended configuration, by Nextcloud, to improve basic functionality. Without opcache enabled, you will probably see a warning when you log in as the Nextcloud admin user. To enable PHP opcache, you must configure the php.ini file. First, you’ll need to find out the latest version of PHP installed on your server. To locate the most recent version of PHP issue the command:
1 |
ls /etc/php/ |
You should see directories with all the installed versions (Figure 2).

Figure 2: Versions 5.6 to 7.3 are installed.
If PHP 7.3 is the most recent version installed, the command to open the configuration file would be:
1 |
sudo nano /etc/php/7.3/apache2/php.ini |
Within that file, make sure the following lines reflect the changes below (the configuration options begin around line 1799):
1 2 3 4 5 6 7 |
opcache.enable=1 opcache.enable_cli=1 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.memory_consumption=128 opcache.save_comments=1 opcache.revalidate_freq=1 |
Once you’ve made the changes, save and close the file. To make the changes take effect, restart Apache with the command:
1 |
sudo systemctl restart apache2 |
Install and Enable PHP APCu
We can configure PHP APCu to handle the local cache, alongside Redis controlling the file locking cache. NOTE: Only do this if your server has the memory overhead to do so.
To install PHP APCu, issue the command:
1 |
sudo apt-get install php-apcu -y |
After the installation completes, open the Nextcloud configuration again with the command:
1 |
sudo nano /var/www/html/nextcloud/config/config.php |
In that file, change this line:
1 |
'memcache.local' => '\\OC\\Memcache\\Redis', |
To:
1 |
'memcache.local' => '\OC\Memcache\APCu', |
Save and close the file. Restart Apache with the command:
1 |
sudo systemctl restart apache2 |
At this point, your users should start seeing significant improvement in the performance of the Nextcloud server, all thanks to a bit of caching. Of course, due to the nature of caching, users might not experience this improvement immediately. However, over time they should notice Nextcloud responding much more efficiently.