Technitium DNS server zone high-availability

NextCloud server tuning and optimization

NextCloud recommend few server tuning that can help to improve your setup's performance. 

Let apply the tuning on your local or oracle cloud's VPS, running on Ubuntu 22.04 minimal instance.

1. Create /var/www/html/info.php with below php script. Browse to http://xxx.xxx.xxx.xxx/info.php to check the php module. Remove the info.php after finish testing.>

<?php phpinfo(); ?>

2. Use php-fpm module to replace apache2's default mod_php module for better performance. Install php8.1-fpm package, disable the default php module, enable php-fpm's dependencies and configuration. Restart apache2 service for it to take effect.

sudo apt-get install php8.1-fpm
sudo a2dismod php8.1 mpm_prefork
sudo a2enmod mpm_event proxy proxy_fcgi
sudo a2enconf php8.1-fpm
sudo systemctl restart apache2

3. Browse to http://xxx.xxx.xxx.xxx/info.php again to confirm apache2 is using php-fpm module.

4. With apache2 using php-fpm instead of mod_php, update the nextcloud's php setting to /etc/php/8.1/fpm/php.ini.

memory_limit = 512M
upload_max_filesize = 20M
max_execution_time = 360
post_max_size = 20M
date.timezone = Asia/Kuala_Lumpur

5. Follow link on how to customize php-fpm. I am using NextCloud's official guide as a starting point. Remember to restart php8.1-fpm service afterward with the new settings.

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

pm = dynamic
pm.max_children = 120
pm.start_servers = 12
pm.min_spare_servers = 6
pm.max_spare_servers = 18
sudo systemctl restart php8.1-fpm

6. Improve PHP performance by customizing opcache module. Edit /etc/php/8.1/mods-available/opcache.ini with below setting. Again, remember to restart php8.1-fpm service afterward.

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
opcache.save_comments=1

; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
; For Development / testing, keep 1
; For performance / production, keep 0
opcache.validate_timestamps=0

; Prevents caching files that are less than this number of seconds old.
; It protects from caching of incompletely updated files.
; In case all file updates on your site are atomic,
; you may increase performance setting it to "0"".
opcache.file_update_protection=0

; The amount of shared memory to reserve for compiled JIT code.
; A zero value disables the JIT.
opcache.jit_buffer_size=100M

7. Enable HTTP2 for speed improvement. Make sure you have enable HTTPS and php-fpm before proceed. Use your browser's dev tool to check it is using the h2 protocol.

sudo a2enmod ssl and http2
sudo systemctl restart apache2

8. Enable Pretty URL. It will remove the index.php part, make the URL shorter and nicer.

sudo a2enmod env rewrite
sudo systemctl restart apache2
 
sudo -u www-data php /var/www/nextcloud/occ config:system:set overwrite.cli.url --value="https://my.server.com"
sudo -u www-data php /var/www/nextcloud/occ config:system:set htaccess.RewriteBase --value="/"
sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccess

9. Enable swap if your instance is lacking in physical server memory.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak # optional
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab


Comments