Skip to content

Tune a Nextcloud Instance

Move to Nextcloud - Tuning

Tune your Nextcloud instance

PHP memory limit

Nextcloud is recommending a memory limit of at least 512 MB or more. Therefore please edit your php.ini. Depending on the PHP version, operating system and the web server you using the location of the file differs. With Apache2 + PHP 8.0 at Ubuntu bases OS you find it here: /etc/php/8.0/apache2/php.ini. Open it with your favorite editor and search for the parameter memory_limit and change it to 512M or higher.

# Before editing
grep memory_limit /etc/php/8.0/apache2/php.ini 
memory_limit = 128M
# edit the file with for example vim
sudo vim /etc/php/8.0/apache2/php.ini 
# After editing
grep memory_limit /etc/php/8.0/apache2/php.ini 
memory_limit = 512M
# restart web server
sudo systemctl restart apache2
PHP-FPM tuning

If PHP-FPM instead of PHP is used (e.g. for HTTP/2), then there are some configurations that should be adapt. Regarding Nextcloud to find the optimal configurations this site can b used. This site of c-rieger is also interesting. Please adapt the settings to your needs, one example.

sudo su
AvailableRAM=$(awk '/MemAvailable/ {printf "%d", $2/1024}' /proc/meminfo)
AverageFPM=$(ps --no-headers -o 'rss,cmd' -C php-fpm8.0 | awk '{ sum+=$1 } END { printf ("%d\n", sum/NR/1024,"M") }')
cp /etc/php/8.0/fpm/pool.d/www.conf /etc/php/8.0/fpm/pool.d/www.conf.bak
cp /etc/php/8.0/fpm/php-fpm.conf /etc/php/8.0/fpm/php-fpm.conf.bak
cp /etc/php/8.0/apache2/php.ini /etc/php/8.0/apache2/php.ini.bak
cp /etc/php/8.0/fpm/php.ini /etc/php/8.0/fpm/php.ini.bak
cp /etc/php/8.0/fpm/php-fpm.conf /etc/php/8.0/fpm/php-fpm.conf.bak
sed -i 's/pm.max_children =.*/pm.max_children = '$FPMS'/' /etc/php/8.0/fpm/pool.d/www.conf
sed -i 's/pm.start_servers =.*/pm.start_servers = '$PStartS'/' /etc/php/8.0/fpm/pool.d/www.conf
sed -i 's/pm.min_spare_servers =.*/pm.min_spare_servers = '$PMinSS'/' /etc/php/8.0/fpm/pool.d/www.conf
sed -i 's/pm.max_spare_servers =.*/pm.max_spare_servers = '$PMaxSS'/' /etc/php/8.0/fpm/pool.d/www.conf
sed -i "s/;pm.max_requests =.*/pm.max_requests = 2000/" /etc/php/8.0/fpm/pool.d/www.conf
sed -i "s/allow_url_fopen =.*/allow_url_fopen = 1/" /etc/php/8.0/fpm/php.ini
sed -i "s/output_buffering =.*/output_buffering = 'Off'/" /etc/php/8.0/apache2/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 3600/" /etc/php/8.0/apache2/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/8.0/apache2/php.ini
sed -i "s/post_max_size =.*/post_max_size = 16G/" /etc/php/8.0/apache2/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 16G/" /etc/php/8.0/apache2/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/8.0/apache2/php.ini
sed -i "s/memory_limit = 128M/memory_limit = 1024M/" /etc/php/8.0/fpm/php.ini
sed -i "s/output_buffering =.*/output_buffering = 'Off'/" /etc/php/8.0/fpm/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 3600/" /etc/php/8.0/fpm/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/8.0/fpm/php.ini
sed -i "s/post_max_size =.*/post_max_size = 16G/" /etc/php/8.0/fpm/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 16G/" /etc/php/8.0/fpm/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/8.0/fpm/php.ini
sed -i "s/;session.cookie_secure.*/session.cookie_secure = True/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.enable=.*/opcache.enable=1/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.memory_consumption=.*/opcache.memory_consumption=128/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=8/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.max_accelerated_files=.*/opcache.max_accelerated_files=10000/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.revalidate_freq=.*/opcache.revalidate_freq=2/" /etc/php/8.0/fpm/php.ini
sed -i "s/;opcache.save_comments=.*/opcache.save_comments=1/" /etc/php/8.0/fpm/php.ini
sed -i "s|;emergency_restart_threshold.*|emergency_restart_threshold = 10|g" /etc/php/8.0/fpm/php-fpm.conf
sed -i "s|;emergency_restart_interval.*|emergency_restart_interval = 1m|g" /etc/php/8.0/fpm/php-fpm.conf
sed -i "s|;process_control_timeout.*|process_control_timeout = 10|g" /etc/php/8.0/fpm/php-fpm.conf
systemctl restart php8.0-fpm
Memory cache - Redis

There are some ways to use memory caching in Nextcloud.

You can significantly improve your Nextcloud server performance with memory caching, where frequently-requested objects are stored in memory for faster retrieval.

I will use redis here. At first edit the redis.conf file. Afterwards add the needed parameter to the config.php of Nextcloud. The config.php is THE file were Nextcloud stores it configuration parameters, so please handle with care.

At first open the /etc/redis/redis.conf file and edit/adapt the following parameter.

bind ::1
protected-mode yes
port 6379
unixsocket /var/run/redis/redis-server.sock
requirepass YourSecretPasswordHere
# All other settings should be fine

Add the web server user to the redis group and restart redis.

sudo usermod -a -G redis www-data
sudo systemctl restart redis-server

Edit the /var/www/html/nextcloud/config/config.php and add the needed parameter for redis.

'memcache.local' => '\\OC\\Memcache\\Redis',
'memcache.distributed' => '\\OC\\Memcache\\Redis',
'filelocking.enabled' => true,
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' =>
array (
  'host' => '/var/run/redis/redis-server.sock',
  'port' => 0,
  'dbindex' => 0,
  'password' => 'YourSecretPasswordHere',
  'timeout' => 1.5,

Restart the web server and enable redis so it is started automatically after reboot.

sudo systemctl restart apache2
sudo systemctl enable redis-server
Default Phone region

Nextcloud is complaining about the missing "default_phone_region" in the settings after a fresh installation. Check the ISO 3166-1 Wiki page and choose your code depending on your country (in my case DE) and add it to the config.php.

'default_phone_region' => 'DE',
auto logout

This is optional, but i think it is needed. If one is logged in to your Nextcloud instance, but there is no activity anymore, Nextcloud is able to logout the user automatically. To configure a logout after 15 minutes, add the following lines to the config.php.

# auto logout enabled
'auto_logout' => true,
# Enable or disable session keep-alive when a user is logged in to the Web UI.
'session_keepalive' => true,
# The lifetime of a session after inactivity.
'session_lifetime' => 900, # --> in Seconds. Also possible value: 60*15
# Optional: Lifetime of the remember login cookie, which is set when the user clicks the remember checkbox on the login screen.
# Seems to be used by the DEsktop client too
'remember_login_cookie_lifetime' => 60*60*24*15, # --> default's to 15 days. If it is set to 0 remember function is disabled.

In order to increase performance and stability of the database one could use mysqltuner. Installation and using is really easy.

sudo apt install mysqltuner
sudo mysqltuner
 >>  MySQLTuner 1.7.13 - Major Hayden <[email protected]>
 >>  Bug reports, feature requests, and downloads at
 >>  Run with '--help' for additional options and output filtering
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
    Control warning line(s) into /var/log/mysql/error.log file

The recommendations can be added to the server.cnf of the mysql installation. In Ubuntu for example /etc/mysql/mariadb.conf.d/50-server.cnf. Additionally add the following to the config as per Nextcloud admin guide.

  • “READ COMMITED” transaction isolation level
  • Disabled or BINLOG_FORMAT = ROW configured Binary Logging
binlog_format = ROW
transaction_isolation = READ-COMMITTED

See also Nextcloud database sample configuration.