Running Nextcloud with NGINX allows for efficient use of system resources along with flexible de­ploy­ment options. To set it up properly, you’ll need a com­pat­ible system en­vir­on­ment and the right con­fig­ur­a­tion. A few specific ad­just­ments are also required for smooth in­teg­ra­tion with NGINX.

What is NGINX and why pair it with Nextcloud?

NGINX is an open-source-based web server software that can also serve as a reverse proxy server, load balancer and HTTP cache. Ori­gin­ally developed by Russian software engineer Igor Sysoev, NGINX is dis­trib­uted under the BSD license (BSD is a Unix variant).

NGINX is designed to handle a large number of con­nec­tions sim­ul­tan­eously. To do this, the software uses an event-driven, non-blocking ar­chi­tec­ture. Unlike tra­di­tion­al web servers that spawn a new process or thread for each con­nec­tion, NGINX runs a master process alongside multiple worker processes. The master handles con­fig­ur­a­tion, while the workers process incoming client requests.

Pairing Nextcloud with NGINX offers several key benefits for high-per­form­ance en­vir­on­ments, including:

  • Efficient resource usage: NGINX can handle multiple con­nec­tions at once, with minimal resource con­sump­tion.
  • Excellent scalab­il­ity: NGINX supports flexible load balancing and is easily scalable by adding ad­di­tion­al servers.
  • Highly cus­tom­is­able ar­chi­tec­ture: Thanks to its modular structure, NGINX can be adapted to suit a wide range of scenarios.
  • Reliable per­form­ance under heavy server load: Even when the server is under heavy use, NGINX is reliable, keeping your services con­tinu­ously available.
Note

Since Nextcloud only of­fi­cially supports Apache 2.x as its web server, there is no official support for NGINX. Running Nextcloud with NGINX is therefore best suited to users with ex­per­i­ence in web server con­fig­ur­a­tion.

What are the re­quire­ments for running Nextcloud with NGINX?

To run Nextcloud on NGINX, you’ll need a server running Ubuntu, Debian, or another com­pat­ible system. The server should have at least 4 GB of RAM and two CPUs. For larger setups with multiple Nextcloud apps, more memory and CPU cores are strongly re­com­men­ded. You’ll also need adequate storage space for data and backups.

For in­stall­a­tion, you’ll also need a com­pat­ible database like MySQL or MariaDB, as well as the PHP scripting language (minimum version 8.1, version 8.3 re­com­men­ded). The database is used to store user data and plugin data, as well as file metadata. PHP is required to run Nextcloud’s core functions. You’ll also need an account with admin rights to install the software.

Managed Nextcloud
Cloud storage that puts you in control
  • Keep your data safe with industry-leading security
  • Save time on updates and main­ten­ance
  • Easily add apps and online office tools

How to install Nextcloud with NGINX

Some essential pre­par­a­tions should be completed before in­stalling Nextcloud. This guide assumes an Ubuntu server where the system and all required de­pend­en­cies are already in place. These include NGINX as the web server, MySQL as the database, and PHP 8.3 with all the necessary ex­ten­sions.

We have also installed Certbot for easy SSL cer­ti­fic­ate setup with Let’s Encrypt. We also assume you have a domain set up so you can access Nextcloud via a URL rather than an IP address.

Tip

How to download and unpack Nextcloud

Start by creating a folder called “nextcloud” in your home directory to store the in­stall­a­tion files. While this can be done manually via the Nextcloud changelog, it’s quicker to use the following Curl command:

mkdir ~/nextcloud && cd ~/nextcloud
curl --output nextcloud.zip https://download.nextcloud.com/server/releases/nextcloud-30.0.5.zip
bash
Note

If you’re in­stalling a different version of Nextcloud, make sure to update the URL ac­cord­ingly.

Then unzip the file, move it to the web root ./var/www and set the file per­mis­sions:

unzip nextcloud.zip && sudo mv nextcloud /var/www/ && sudo chown -R www-data:www-data /var/www/nextcloud
bash

How to set up the database

To set up the MySQL database, run the sudo command sudo mysql_secure_installation. Type “y” to set your root password (VALIDATE PASSWORD) and choose “2” for a strong password (STRONG), which you can then set up. Press “y” to confirm the password, then press “y” again to remove anonymous users, disable remote logins, remove the test database, and reload the privilege tables.

Then follow these steps:

  1. Log in as the root user using sudo mysql -u root -p.
  2. Create the database using: create database nextcloud;.
  3. Create the user using: create user 'nextcloud'@'localhost' identified by <new_password>;.
  4. Grant per­mis­sions using: grant all privileges on nextcloud.* to 'nextcloud'@'localhost';.
  5. Finally, update the priv­ileges using flush privileges; and exit the database console using exit.

Once the database has suc­cess­fully been set up, you need to add the cre­den­tials (DB_NAME, DB_USER, DB_PASSWORD) to the Nextcloud con­fig­ur­a­tion file. Open the file ./var/www/nextcloud/config/config.php and add the relevant entries:

'dbtype' => 'mysql',
'dbname' => 'nextcloud',
'dbuser' => 'nextcloud',
'dbpassword' => '<your_password>',
'dbhost' => 'localhost',
'dbtableprefix' => 'oc_',
'mysql.utf8mb4' => true,
txt
VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centres
  • 24/7 premium support with a personal con­sult­ant

How to disable NGINX’s default con­fig­ur­a­tion

Before in­teg­rat­ing Nextcloud into NGINX, you need to make sure no other con­fig­ur­a­tion files override the Nextcloud in­stall­a­tion. By default, there may still be a default con­fig­ur­a­tion file in ./etc/nginx/sites-enabled/ that could interfere with your set up. Remove it using:

sudo rm /etc/nginx/sites-enabled/default
bash

Then activate your custom con­fig­ur­a­tion with this command:

sudo ln -s /etc/nginx/sites-available/exampledomain.co.uk /etc/nginx/sites-enabled/
bash

How to configure the NGINX server

To configure NGINX as the web server, a domain should already be set up and linked to the server’s IP. Make sure the DNS is also correctly set up. Create a new NGINX config file in the sites-available directory using:

sudo touch /etc/nginx/sites-available/exampledomain.co.uk
bash

(We used exampledomain.co.uk as the URL.)

Open the config file in a text editor (such as Vim) using:

sudo vim /etc/nginx/sites-available/exampledomain.co.uk
bash

Insert the code block from the ‘Nextcloud in a subdir of the NGINX webroots’ section of the Nextcloud NGINX config doc­u­ment­a­tion.

Since the HTTPS cer­ti­fic­ate will be added with Certbot, remove any SSL-related lines from the NGINX config file. Certbot will handle these auto­mat­ic­ally.

How to set up an SSL cer­ti­fic­ate with Certbot

To run Nextcloud over a secure HTTPS con­nec­tion, you need to set up an SSL cer­ti­fic­ate, in this case from Let’s Encrypt. To begin with, configure the firewall to allow HTTPS and SSH con­nec­tions using the following sudo commands:

sudo ufw allow 'Nginx Full'
sudo ufw allow 'OpenSSH'
bash

Next, enable the firewall with sudo ufw enable and check with sudo ufw status to ensure the con­nec­tions are allowed. Then you can create the SSL cer­ti­fic­ate using Certbot:

sudo certbot --nginx -d exampledomain.co.uk
bash

Follow the prompts to provide your email address for cer­ti­fic­ate renewals and to accept the terms of service. Choose option 2 to auto­mat­ic­ally redirect all HTTP requests to HTTPS. Certbot saves the cer­ti­fic­ate files in the .etc/letsencrypt/live/exampledomain.co.uk/ directory. The file fullchain.pem contains the SSL cer­ti­fic­ate, and privkey.pem contains the private key. You need to add them to your NGINX con­fig­ur­a­tion file:

ssl_certificate /etc/letsencrypt/live/exampledomain.co.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/exampledomain.co.uk/privkey.pem;
txt

Since Certbot modified the con­fig­ur­a­tion file, you need to add the ssl attribute between 443 and http2 again. The block should look like this:

server { 
    listen 443 ssl http2; 
    listen [::]:443 ssl http2; 
    server_name exampledomain.co.uk;
}
txt

How to restart services and access Nextcloud

For the changes to take effect, you need to reload the PHP-FPM and NGINX services:

sudo systemctl reload php8.3-fpm.service
sudo systemctl reload nginx.service
bash

Now, open your domain in the browser - ex­ample­do­main.co.uk in our case. If everything has been con­figured correctly, the Nextcloud welcome page should appear.

Note

If Nextcloud doesn’t load, it may be because your domain isn’t listed as a Trusted Domain in config.php. Add it there to fix the issue.

Go to Main Menu