By default, Nextcloud has object caching disabled. It also triggers the background tasks to be run on every page load by default, using AJAX instead of using a cron job on a timer. This was causing a significant delay for my friends accessing my server.
Following a couple other guides on the internet, I found some of the steps to have already been completed on my installation (I’m using a Turnkey container). I’ll include those steps here anyway– I should make a fresh install one day!
First, let’s install redis:
apt-get install redis-server -y
Start that puppy:
systemctl start redis-server && systemctl enable redis-server
Edit the config– mine was already correct except for the last line:
nano /etc/redis/redis.conf
port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 700
and add redis to www-data group:
usermod -aG redis www-data
We’re using APCu for local cache and redis for file-locking cache, so we gotta tell nextcloud:
nano /var/www/nextcloud/config/config.php
‘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,
),
Add that block above these two closing parenthesis at the end of the file:
),
);
Enable opcache- (mine was already enabled except for the opcache.enable_cli=1 line)
nano /etc/php/7.3/apache2/php.ini
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
Now install APCu:
apt-get install php-apcu
And let’s tell Nextcloud to use it:
nano /var/www/nextcloud/config/config.php
Find the line you added a minute ago:
‘memcache.local’ => ‘\OC\Memcache\Redis’,
and change it to
‘memcache.local’ => ‘\OC\Memcache\APCu’,
Almost done! In order to get APCu working, I had to edit these few files: (edit for your php version)
/etc/php/7.4/cli/php.ini
/etc/php/7.4/cli/conf.d/20-apcu.ini
/etc/php/7.4/apache2/php.ini
/etc/php/7.4/apache2/conf.d/20-apcu.ini
and add these lines:
extension=apcu.so
apc.enable_cli=1
Now restart your Nextcloud container and check to see if APCu is enabled:
php -i|grep APCu

It should be a whole lot faster already! Let’s nuke that stupid AJAX stuff too.
Go to your admin panel in Nextcloud and press this button:

That doesn’t actually create the cron job, so let’s do that the easy way with systemd.
Create these two files and paste this stuff into them:
nano /etc/systemd/system/nextcloudcron.service
[Unit]
Description=Nextcloud cron.php job[Service]
User=www-data
ExecStart=/usr/bin/php -f /var/www/nextcloud/cron.php
KillMode=process
nano /etc/systemd/system/nextcloudcron.timer
[Unit]
Description=Run Nextcloud cron.php every 5 minutes[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=nextcloudcron.service[Install]
WantedBy=timers.target
And enable that mug:
systemctl enable –now nextcloudcron.timer
And restart the container just for fun. Now you’re running the cleanup tasks every 5 mins instead of everytime a page loads!
Leave a Reply