How To Install NextCloud 21 on Ubuntu Server 20.04 with Nginx, PHP-FPM, MariaDB, and Redis
This guide is for a manual install of NextCloud 21 on a fresh Ubuntu 20.04 server using Nginx, MariaDB, and PHP 7.4; and Redis for memcache.
1. Install Dependencies
First of all, install dependencies. Let’s start with Nginx (web server) and MariaDB (MySQL-equivalent database engine).
Note that I’m running commands shown below from a root prompt. If you’re not elevated to root, remember to use sudo for package install commands, etc.
apt update apt install nginx mariadb-server
Now, PHP, Redis, and various modules.
apt install php7.4 php7.4-fpm php7.4-common php7.4-gd php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-intl php7.4-imap php7.4-bcmath php7.4-redis php7.4-mysql php7.4-gmp php7.4-imagick redis imagemagick
2. SQL Config and Database Creation
Secure the MariaDB server. Issue the following command, and follow the prompts. Set a root password for the SQL server, but otherwise accept all defaults.
Now, log into the mysql prompt and create a Nextcloud database and user. Replace the generic password shown below with one of your own, and record this. The commands below create a database and user, allow all privileges on the database to said user, apply the privilege changes, and exit the sql prompt.
mysql -u root -p MariaDB [(none)]> CREATE DATABASE nextcloud; MariaDB [(none)]> CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'Your_Strong_Password'; MariaDB [(none)]> GRANT ALL ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'Your_Strong_Password'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> exit
3. Download and Unzip NextCloud
Visit the NextCloud Server Package Download page, and copy the link from the big blue “Download Nextcloud” button.
On your server, download the ZIP file using wget. (Obviously, replace the link shown below with whatever is current.)
Install the unzip utility, and unzip the file. Move the unzipped folder to a web server directory, and set permissions and ownership.
apt install unzip unzip nextcloud nextcloud-21.0.1.zip mv nextcloud /var/www/ chown -R www-data:www-data /usr/share/nginx/nextcloud
4. Configure Nginx
Now, create an Nginx config file.
Make changes to this config as necessary (change your server hostname, and ensure the path to the web application root directory is correct), and paste into the config file. Save and exit.
Note, below I am using the self-signed certs that come with Ubuntu so that Nginx can have all SSL config in place and reload without errors. We’ll grab a proper cert later from Letsencrypt.
Create a symlink of this file in the Nginx “sites-enabled” directory to enable the website.
cd /etc/nginx/sites-enabled/ ln -s ../sites-available/nextcloud . systemctl reload nginx
If Nginx does not reload without error, check for helpful messages by running “systemctl status nginx.” Correct issues as needed.
5. Run NextCloud Setup
If you are setting this up on your local network, you’ll want to add an A Record to your DNS server pointing your cloud server domain name to the IP address of your Nextcloud server. Depending on your network setup this might be done in your router, or in your Windows server if you have an Active Directory domain network configured. At this point, you should be able to access your “cloud.example.com” address in your browser, and set up NextCloud.
Specify an admin username and password as directed. The data directory is outside the scope of this basic article; if you want to store your data elsewhere you may, but if you are unsure, leave this at its default setting.
In the Database section, enter nextcloud as the database user and the password you specified during setup in the MySQL prompt. Enter nextcloud for database name, and leave the database set to its default value, localhost. Click on Finish Setup.
You should now have a minimal NextCloud setup! At this point it is functional, but we still need to do more tuning. First though, let’s get a certificate from LetsEncrypt.
6. Grab a free signed certificate from LetsEncrypt
Note that to obtain a LetsEncrypt certificate, Port 80 must be forwarded to your nextcloud server. (This also can be handled via a reverse proxy server, but we’re going to keep it simple for this article.) You should have 80 and 443 forwarded for external web access anyhow.
First, let’s install Certbot. I dislike having to install Snap as per the official certbot instructions, so let’s roll differently and install it as a normal package.
apt update apt install certbot python3-certbot-nginx
Now, let’s grab a new cert from Letsencrypt. Use your custom nextcloud domain name where I show cloud.example.com.
certbot --nginx -d cloud.example.com
Go through the wizard. Provide an email address for certificate expiration notices; agree to the terms; decide whther or not you agree to data sharing. The certificate should be created and deployed automatically. At the end of the process you will be asked whether to reconfigure the web server to redirect all HTTP requests to HTTPS. You can safely select No here, as our Nginx config is already set up to do this.
Visit your NextCloud instance in your browser now, and verify the new certificate is in use.
7. Performance Tuning
In your shiny new NextCloud instance, click your user avatar in the upper right. From the dropdown, click Settings; then choose “Overview” under the Administration section. You will probably see something like this.
We will work through these errors one by one. Note that if you have installed all packages shown in the opening parts of this tutorial, you probably will not see the last error regarding imagemagick.
1. PHP Memory Limit
This is very easy to rectify. Open the file
/etc/php/7.4/fpm/php.iniand search for memory_limit. Change the value from 128M to 512M.
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 512M
2. PHP environment variable warnings
/etc/php/7.4/fpm/pool.d/www.confand un-comment the folllowing lines. (to uncomment, erase the semicolon in front of each)
env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
3. Nginx HSTS
/etc/nginx/sites-enabled/nextcloudand uncomment the following line (delete the #)
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
Let’s make our changes so far, effective.
systemctl restart nginx php7.4-fpm
Refresh the NextCloud overview page and the top three errors should have gone away. Let’s tackle the rest of them…
4. No default region set
Add a line: ‘default_phone_region ‘ => ‘CA’, (if Canada) or ‘default_phone_region’ => ‘US’, (if United States, etc) to the file
For example, the bottom several lines of your config.php file might look like this when done. Remember the comma at the end of your newly-added line. If you do not know the two-letter abbreviation for your country, you can look it up here.
'dbuser' => 'nextcloud', 'dbpassword' => 'WhatAScaryOldPassword...', 'installed' => true, 'default_phone_region' => 'CA', );
Save and close the file. You do not need to restart any services to make this take effect. If you refresh the Nextcloud page now, that error should be gone.
5. Configure memory caching
There are several options for caching; we’ll roll with Redis. If you carefully followed the guide so far, we already have the Redis server and PHP redis module installed. Add the following lines to your Nextcloud config.php file:
'filelocking.enabled' => true, 'memcache.locking' => '\OC\Memcache\Redis', 'memcache.local' => '\OC\Memcache\Redis', 'memcache.distributed' => '\OC\Memcache\Redis', 'redis' => array( 'host' => 'localhost', 'port' => 6379, 'timeout' => 0.0, 'password' => '', ),
In my case I added these just after the “trusted_domains” array but it’s not super important where in the file you place these settings. Save the file, refresh your Nextcloud page; and hopefully you’ll see something like this.
8. Additional Tweaks
1. Configure a cron job
root@nextcloud:~# which php /usr/bin/php root@nextcloud:~# crontab -u www-data -e # Add the following line to the end of the file: (will call the cron script every 5 minutes) */5 * * * * /usr/bin/php -f /var/www/nextcloud/cron.php
With this done, change the setting to Cron within Nextcloud. Settings > Basic Settings.