Nextcloud on Ubuntu with NGINX: a production setup that lasts
In this article13 sections
Nextcloud is the most popular way to run your own file sync and sharing, with calendars, contacts and documents on top. Run it on a server you control, and your files stay under your own roof instead of with a large provider. That is the appeal, and also the responsibility: a Nextcloud server holds everything your team or family considers private, it is reachable from the internet, and it needs to be patched, backed up and monitored like any other important system.
The installer makes a working demo in ten minutes. A system you are happy to depend on takes a little more care in a few specific places: where the data lives, how PHP and caching are set up, how background jobs run, how HTTPS and headers are configured, and how you back up and upgrade. This guide covers those, in the order I do them on Ubuntu with NGINX and PHP-FPM.
Read Nextcloud’s own administration manual alongside this: the supported PHP and database versions, and the recommended web server configuration, change between releases, and the manual is the authority for your version.
Before you start
- A server with enough memory for your users (start with a comfortable margin; a small team is fine on a modest VPS, and heavy use of previews, search or office documents needs more).
- A domain name pointing at it, such as
cloud.example.com, and a valid certificate. - A plan for the data. Files can grow without bound. Put the data directory on its own volume, outside the web root, so you can enlarge or snapshot it separately.
- A first-hardened server: a named administrator, key-only SSH and a firewall allowing 22, 80 and 443 (see hardening SSH).
1. Install the stack
sudo apt update
sudo apt install -y nginx mariadb-server redis-server unzip curl \
php-fpm php-mysql php-gd php-curl php-mbstring php-xml php-zip php-intl php-bcmath php-gmp php-imagick php-apcu php-redis
php -v
Confirm that the PHP version is one your Nextcloud release supports. Secure the database server and create a database and a dedicated user:
sudo mariadb-secure-installation
sudo mariadb -e "CREATE DATABASE nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
sudo mariadb -e "CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'a-long-random-password';"
sudo mariadb -e "GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost'; FLUSH PRIVILEGES;"
2. Download Nextcloud and verify it
Get the release archive and its checksum from the official download page, and check that they match before you unpack anything:
cd /tmp
curl -LO https://download.nextcloud.com/server/releases/latest.tar.bz2
curl -LO https://download.nextcloud.com/server/releases/latest.tar.bz2.sha256
sha256sum -c latest.tar.bz2.sha256
Unpack it into the web root and give it to the web server’s user:
sudo tar -xjf latest.tar.bz2 -C /var/www/
sudo chown -R www-data:www-data /var/www/nextcloud
Create the data directory outside /var/www/nextcloud, for example on a separate disk mounted at /srv/nextcloud-data:
sudo mkdir -p /srv/nextcloud-data
sudo chown www-data:www-data /srv/nextcloud-data
sudo chmod 750 /srv/nextcloud-data
3. Install from the command line
The occ command is Nextcloud’s administration tool, run as the web server’s user:
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:install \
--database mysql --database-name nextcloud \
--database-user nextcloud --database-pass 'a-long-random-password' \
--admin-user admin-name --admin-pass 'a-strong-admin-password' \
--data-dir /srv/nextcloud-data
Use your own names and passwords, and avoid the obvious admin. Then tell Nextcloud which names it answers to, and its public address:
sudo -u www-data php occ config:system:set trusted_domains 1 --value=cloud.example.com
sudo -u www-data php occ config:system:set overwrite.cli.url --value=https://cloud.example.com
4. PHP-FPM and OPcache settings
Nextcloud needs more memory per request than a simple site, and it needs OPcache set correctly. In the PHP-FPM configuration for your version (for example /etc/php/8.3/fpm/conf.d/90-nextcloud.ini):
memory_limit = 512M
upload_max_filesize = 1G
post_max_size = 1G
max_execution_time = 360
opcache.enable = 1
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.memory_consumption = 128
opcache.save_comments = 1
opcache.revalidate_freq = 60
These follow the values Nextcloud’s manual recommends; check the manual for any change in your version. upload_max_filesize and post_max_size set the largest single upload through the browser, and NGINX must allow the same size (next section). Reload PHP-FPM afterwards (sudo systemctl reload php8.3-fpm, with your version number).
5. NGINX
Nextcloud’s NGINX configuration has a number of details: caching headers for static files, the handling of .well-known redirects for calendar and contacts sync, blocking access to sensitive folders, and the right fastcgi parameters. Start from the example configuration in the official manual for your Nextcloud version and adapt the server name, certificate paths and PHP-FPM socket, instead of writing your own from scratch. Check these points in whatever you end up with:
client_max_body_sizeat least as large as your upload limit (for example1G), andfastcgi_request_buffering offfor large uploads.- Requests for
/.well-known/carddavand/.well-known/caldavredirected to/remote.php/dav. - Access denied for
data,config,lib,3rdparty,templates,occand similar internal paths. - HTTPS only, with a redirect from port 80, and a valid certificate (
sudo certbot --nginx -d cloud.example.com). - The security headers (see NGINX security headers and a CSP): Nextcloud sends several of its own, so avoid sending the same header twice, and start HSTS with a short lifetime.
Test and reload with sudo nginx -t && sudo systemctl reload nginx.
6. Caching and file locking
Without caching, Nextcloud feels slow and is more likely to report file-locking errors. Use APCu for local caching and Redis for file locking. In config/config.php:
'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
'host' => '/var/run/redis/redis-server.sock',
'port' => 0,
],
For the socket to work, enable it in /etc/redis/redis.conf (unixsocket /var/run/redis/redis-server.sock and unixsocketperm 770) and add the www-data user to the redis group (sudo usermod -aG redis www-data), then restart Redis and PHP-FPM. Using the socket keeps Redis off the network. If you prefer a TCP connection, bind Redis to 127.0.0.1 only, and set a password.
7. Background jobs with system cron
Nextcloud runs periodic tasks (cleanup, notifications, indexing). By default these run when someone loads a page, which is unreliable. The manual’s preferred method is the operating system’s cron, which runs them on schedule regardless of traffic.
sudo -u www-data php /var/www/nextcloud/occ background:cron
echo '*/5 * * * * www-data php -f /var/www/nextcloud/cron.php' | sudo tee /etc/cron.d/nextcloud
In the admin settings, under Basic settings, the background jobs mode should now read “Cron”. If the last-run time shown there stops advancing, the cron job is not running. Treat that like a monitoring signal (see monitoring a small server).
Also set a maintenance window and your phone region, which stop two common warnings:
sudo -u www-data php occ config:system:set maintenance_window_start --type=integer --value=1
sudo -u www-data php occ config:system:set default_phone_region --value=GB
(Use your own region code.)
8. Security settings worth turning on
- Two-factor authentication for every user: install the TOTP app in Nextcloud and require it for administrators at least.
- Brute-force protection is built in; make sure the client address is correct, which matters behind a proxy such as Cloudflare. Set
trusted_proxiesand the forwarded headers so Nextcloud sees the visitor’s real address, not the proxy’s. - Keep apps to what you use. Each app is code you must update.
- Run the checks. Open Settings, Administration, Overview: it lists security and setup warnings for your server. Work through them until the list is clear.
- Server-side encryption and end-to-end encryption have trade-offs (performance, recovery, compatibility). Decide deliberately rather than turning them on by default.
9. Backups, and how to restore
You need three things in the same backup: the data directory, the database, and the config/config.php (which holds the instance’s secrets and identifiers). Without the config file, a restore is much harder.
For a consistent backup, put Nextcloud into maintenance mode, copy, and take it out:
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
sudo mysqldump --single-transaction nextcloud > /root/nextcloud-db-$(date +%F).sql
sudo rsync -a /srv/nextcloud-data/ /backup/nextcloud-data/
sudo cp /var/www/nextcloud/config/config.php /backup/config.php
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
Store backups off the server, encrypted (see how we test restores). Then restore into a test machine to prove it works. A volume snapshot of the data disk taken while in maintenance mode is a good complement.
10. Upgrades
- Back up first, and note the current version (
occ status). - Upgrade one major version at a time. Skipping majors is not supported.
- Check apps and PHP compatibility for the target version before you start.
- Use the updater from the command line (
sudo -u www-data php /var/www/nextcloud/updater/updater.phar) or follow the manual’s manual-upgrade steps, then runsudo -u www-data php occ upgradeif prompted, and re-check the Overview page.
Common problems
- “Access through untrusted domain”: the address you used is not in
trusted_domains. Add it withocc config:system:set trusted_domains. - Uploads fail above a certain size: raise
client_max_body_sizein NGINX andupload_max_filesizeandpost_max_sizein PHP, and allow enough time (max_execution_time). - Slow, or “file is locked”: missing APCu or Redis file locking, or Redis not reachable by
www-data. - Warnings about background jobs: the cron job is missing or runs as the wrong user.
- Login or sync loops behind a proxy: set
overwriteprotocol,trusted_proxiesand the forwarded headers correctly. - Out of disk: see the guide on finding what is eating your disk, and check the trash and file versions apps.
When it is worth getting help
A first Nextcloud install is a good project. A system your team depends on, with its data, mail integration and users, benefits from a careful setup, a tested backup and a person who watches it. I install, migrate and maintain self-hosted applications such as Nextcloud, ownCloud, Odoo, Mautic and Mattermost, with documentation and a restore test.
You can see my profile on Upwork, or read about software and app installation. If you need a server to run it on, SiteHarbour offers VPS hosting.