Add Caching and thumbnail generation to Nextcloud PHP
To make nextcloud faster, an easy upgrade is to implement memory caching and automated thumbnail generation. This will take the load off the cpu when a user goes to open the photos section of Nextcloud.
- Installed caching services
- APCu: Speeds up PHP by storing frequently used data in RAM
- Redis: Handles file-locking and distributed cache, reducing database load
- Both caches keep repeated operations in memory, so pages and thumbnails load faster
- Configured Nextcloud to use caching
- Updated config.php to enable APCu for local cache and Redis for locking
- Verified that Nextcloud recognized the caches using the OCC tool
- Enabled and automated thumbnail generation
- Installed the Preview Generator app
- Ran preview:generate-all to pre-create thumbnails of all images
- Set up a cron job to run preview:pre-generate in the background daily, ensuring thumbnails are always up to date without manual intervention
Outcome:
Faster page loads, folder browsing, and photo viewing
Reduced CPU spikes because pages and thumbails are served from memory/cache rather than recalculated on-demand
Automatic maintenance with the cron job keeps thumbnails current for new uploads
What is ‘caching’ in Nextcloud?
Caching stores temporary copies of data (lke file lists, session info, or thumbnails) in RAM so they can be fetched almost instantly instead of recalculated or re-read from disk.
Nextcloud supports three main types of caching:
- Local cache (APCu) speeds up PHP execution
- Distributed cache (Redis) speeds up data sharing between PHP workers
- File locking cache (Redis) prevents race conditions when multiple users or processes edit files
Install APCu and Redis for PHP 8.3
First confirm the version of php that is running
php -v
Then update the package list and install
sudo apt update
sudo apt install php8.3-apcu php8.3-redis redis-server
Check that redis is now running
systemctl status redis-server
Edit the Nextcloud config.php
sudo nano /var/www/nextcloud/config/config.php
Then add these lines to the array
'memcache.local' => '\\OC\\Memcache\\APCu',
'memcache.distributed' => '\\OC\\Memcache\\Redis',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' => [
'host' => 'localhost',
'port' => 6379,
],
Restart the PHP-FPM and Web Server
sudo systemctl restart php8.3-fpm
sudo systemctl restart apache2.service
Verify caching is active
cd /var/www/nextcloud
sudo -u www-data php occ status
sudo -u www-data php occ config:system:get memcache.local
sudo -u www-data php occ config:system:get memcache.locking
Optionally check redis activity live, looking for any get/set activity
redis-cli monitor
Generate Previews for Thumbnails
You already have caching, so Nextcloud can now generate thumbnails efficiently
Install and enable
sudo -u www-data php /var/www/nextcloud/occ app:install previewgenerator
sudo -u www-data php /var/www/nextcloud/occ app:enable previewgenerator
Generate existing thumbnails
sudo -u www-data php /var/www/nextcloud/occ preview:generate-all
Automate this process with a cron job, decide if you want this to run daily or weekly
For weekly pre-generation edit the crontab for the www-data user
sudo crontab -u www-data -e
Add this line
@weekly php -f /var/www/nextcloud/occ preview:pre-generate
For daily
@daily php -f /var/www/nextcloud/occ preview:pre-generate