How to install NextCloud 16 on Ubuntu 16.04/18.04/18.10/19.04

NextCloud Ubuntu Logo

NextCloud is a Dropbox-like solution for self-hosted file sharing and syncing. Installing NextCloud 16 on Ubuntu is trivial. Whether you want to backup, have file-syncing or just have a Google Calendar alternative, this guide is for you.

What is NextCloud? Is it like a “cloud”?

cloud computing

If you stumbled here by chance and don’t know what NextCloud is, here is an article explaining its principal features and advantages/disadvantages. In this other article you can find NextCloud 16 new features. To tell you the truth, NextCloud is a SaaS cloud, if you want to know more about cloud types you can read this article.

In this article we will cover the installation of the server (not the client).

What’s the newest version?

The newest version of this tutorial is the following:

Looking for an earlier version of this tutorial?

Step 1: Install software

Important
I take NO responsibility of what you do with your machine; use this tutorial as a guide and remember you can possibly cause data loss if you touch things carelessly.

The first step in order to install NextCloud 16 is to install a web server and PHP. Although you can adapt this guide for many Ubuntu versions I suggest you to stick with Ubuntu 18.04 or higher since PHP7 is included. PHP7 brings many improvements over the past versions and will boost NextCloud too, as a matter of fact PHP7 is required since NextCloud 11. You will need root access during this procedure. The following procedure will install apache as webserver. Input the commands one by one to avoid errors!

Ubuntu >= 18.04Ubuntu >= 16.04

Open a terminal and input the following commands:

# apt-get install apache2 php7.2 bzip2
# apt-get install libapache2-mod-php php-gd php-json php-mysql php-curl php-mbstring
# apt-get install php-intl php-imagick php-xml php-zip

Open a terminal and input the following commands:

# apt-get install apache2 php7.0 bzip2
# apt-get install libapache2-mod-php php-gd php-json php-mysql php-curl php-mbstring
# apt-get install php-intl php-mcrypt php-imagick php-xml php-zip

Step 2: Database selection

Now that you have set up the environment, all that is left is to choose a database that will support the installation. You have three choices:

  • SQLite: is a single-file database. It is suggested only for small installations since it will slow NextCloud down sensibly.
  • MariaDB/MySQL: are popular open source databases especially amongst web developers. It is the suggested choice.
  • PostgreSQL: a popular enterprise-class database. More complicated than MySQL/MariaDB.

Now, this choice won’t really alter the functionality of NextCloud (except if you use SQLite), so pick whatever you know best. If you’re unsure pick MariaDB/MySQL.

SQLiteMySQL/MariaDBPostgreSQL

Install the software:

# apt-get install sqlite3 php-sqlite3

Install the software:

# apt-get install mariadb-server php-mysql

Or if you prefer MySQL:

# apt-get install mysql-server php-mysql

During the installation you will be prompted to choose a root password, pick a strong one. If you’re not prompted to choose a password, the default one will be blank. (This is potentially insecure, change it!)

Now you need to enter the database (you will be asked the password you just set):

$ mysql -u root -p

Now that you are in create a database:

CREATE DATABASE nextcloud;

Now you need to create the user that will be used to connect to the database:

CREATE USER 'nc_user'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD_HERE';

The last step is to grant the privileges to the new user:

GRANT ALL PRIVILEGES ON nextcloud.* TO 'nc_user'@'localhost';
FLUSH PRIVILEGES;

When you’re done type Ctrl-D to exit.

Install the software:

# apt-get install postgresql php-pgsql

Now you need to enter the database:

$ sudo -u postgres psql

Now that you are in create a database:

CREATE DATABASE nextcloud;

Now you need to create the user that will be used to connect to the database:

CREATE USER nc_user WITH PASSWORD 'YOUR_PASSWORD_HERE';

The last step is to grant the privileges to the new user:

GRANT ALL PRIVILEGES ON DATABASE nextcloud to nc_user;

When you’re done type \q and press enter to exit.

Step 3: Install NextCloud

The last step is to actually get the software, configure it and run it.

Ubuntu

With these step we download the software and extract it:

# cd /var/www
# wget https://download.nextcloud.com/server/releases/nextcloud-16.0.0.tar.bz2 -O nextcloud-16-latest.tar.bz2
# tar -xvjf nextcloud-16-latest.tar.bz2
# chown -R www-data:www-data nextcloud
# rm nextcloud-16-latest.tar.bz2

Now we need to create a new file in /etc/apache2/sites-available/nextcloud.conf . Feel free to use whatever editor you feel comfortable with and add the following lines:

Alias /nextcloud "/var/www/nextcloud/"

<Directory /var/www/nextcloud/>
  Options +FollowSymlinks
  AllowOverride All

 <IfModule mod_dav.c>
  Dav off
 </IfModule>

 SetEnv HOME /var/www/nextcloud
 SetEnv HTTP_HOME /var/www/nextcloud

</Directory>

Once done it’s time to enable the new site, enable apache mods that are needed by NextCloud and raise PHP’s memory limit:

# a2ensite nextcloud
# a2enmod rewrite headers env dir mime
# sed -i '/^memory_limit =/s/=.*/= 512M/' /etc/php/7.2/apache2/php.ini
# systemctl restart apache2

Step 4: Configuring firewall

This step is essential when your firewall is enabled. If your firewall is enabled you won’t be able to access your NextCloud 16 instance; on the other hand if it isn’t enabled you shouldn’t have any problems and you can simply skip this step.

Tip!
Keep in mind having a firewall enabled is a good security practice and you should already have one enabled.

In order for the firewall to work, it must be enabled. This guide will not include this part. When you enable a firewall many things can go wrong, e.g. you’re using SSH, you enable the firewall and your connection is cut and can’t connect otherwise, hence you should carefully review the documentation from your distribution.

To open the ports needed by NextCloud 16 follow these steps:

UFWIPtables

UFW is the default firewall in Ubuntu, if you’re using one, you’re probably using UFW.

# ufw allow http
# ufw allow https

IPtables is an older firewall (still widely used), if you’re not using UFW you can use IPtables directly.

# iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

Step 5: Install

Once you’re done with selecting the database, it’s time to install everything. Head to http://YOUR_IP_ADDRESS/nextcloud/ and you will be facing the following screen:

Nextcloud 16 Installation
Nextcloud 16 Installation

Select an administrator username and password, then you can select the data folder, but if you don’t know what you’re doing it’s best if you leave it with the default value. Then click on “Storage & Database” to select the database you chose during step 2. Fill everything and if you’ve followed all the steps correctly you should be seeing the following screen:

NextCloud 16 Files app as viewed when installing for the first time
NextCloud 16 Files app

Step 6: Enable Caching (suggested)

NextCloud is good but it can be very slow if you don’t configure a caching solution. There are two caching solutions covered in this guide:

  • PHP OPcache: a PHP inbuilt cache solution that speeds up scripts execution.
  • Redis server: a fast in-memory key-value store that speeds up everything in NextCloud.

Enabling OPcache

Ubuntu

Open a terminal and input the following commands:

# apt-get install php-opcache

Now you need to edit a file located at /etc/php/7.2/apache2/conf.d/10-opcache.ini . Replace 7.2 with the version of PHP you have installed. With your favorite editor, edit the file adding the missing lines:

; configuration for php opcache module
; priority=10
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

These values are suggested by NextCloud, but you’re free to tweak them to suit your needs. Once you’re done you can restart apache:

# systemctl restart apache2

Installing and configuring Redis

Ubuntu

Open a terminal and input the following commands:

# apt-get install redis-server php-redis

Now you must configure NextCloud to use Redis. To do so you need to edit the NextCloud configuration file located at /var/www/nextcloud/config/config.php . The file will look like this, add the highlighted lines:

<?php
$CONFIG = array (
  'instanceid' => '',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' =>
  array (
    0 => 'YOUR_IP',
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '15.0.0.10',
  'overwrite.cli.url' => 'http://YOUR_IP/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nc_user',
  'dbpassword' => 'YOUR_PASSWORD_HERE',
  'installed' => true,
  'memcache.locking' => '\OC\Memcache\Redis',
  'memcache.distributed' => '\OC\Memcache\Redis',
  'memcache.local' => '\OC\Memcache\Redis',
    'redis' => [
      'host' => 'localhost',
      'port' => 6379,
      'timeout' => 3,
    ],
);

These settings will enable NextCloud to use Redis for caching and file locks. Of course these settings are just an example, you can tweak them to suit your needs.

Lastly, restart the webserver:

# systemctl restart apache2

Step 7: Expose NextCloud to Internet (optional)

Important
Hosting applications available to the Internet is potentially dangerous. In order to keep your applications safe you need to be proficient in system security and to follow security best practices.

Most people will want to access their files from whatever location they are. To do so, your newly created NextCloud instance needs to be connected to the Internet.

Given that you need to take care of port-forwarding (if you’re a home user) and domain configuration (which varies according to your provider), here you can find the instructions to create a virtual host with Apache.

Ubuntu

Using your favorite text editor, edit the file we created previously at /etc/apache2/sites-available/nextcloud.conf . And make it look like this:

<VirtualHost *:80>
  ServerName YOURDOMAIN.TLD
  ServerAdmin [email protected]
  DocumentRoot /var/www/nextcloud

  <directory /var/www/nextcloud>
    Require all granted
    AllowOverride All
    Options FollowSymLinks MultiViews
    SetEnv HOME /var/www/nextcloud
    SetEnv HTTP_HOME /var/www/nextcloud
  </directory>
</VirtualHost>

It is important to set ServerName according to a domain you own and have configured correctly. Now you need to add YOURDOMAIN.TLD to the trusted domains in the NextCloud config file. You can do so with the following command:

$ sudo -u www-data php /var/www/nextcloud/occ config:system:set trusted_domains 2 --value=YOURDOMAIN.TLD

Once you complete this step you won’t be able to access NextCloud through http://YOUR_IP_ADDRESS/nextcloud anymore. Instead you will be able to access it through http://YOURDOMAIN.TLD (notice /nextcloud is gone).

Lastly, restart the webserver:

# systemctl restart apache2

Step 8: Get a free SSL certificate with Let’s Encrypt! (SUGGESTED!)

Now that you have your NextCloud instance up and running you’re good to go, but beware: you’re not safe. Internet is a dangerous place for your data and you will most likely need an SSL certificate to ensure your communications are encrypted. Provided you own a domain name you can get one for free using Let’s Encrypt! No catches, free forever.

Warning!
Let’s Encrypt has rate limits in place to prevent inappropriate usage of the CA. There’s a limit on the numbers of attempts you can do before getting a temporary ban. During this setup, if things go wrong, I suggest you to use the –staging option to avoid the temporary ban. The –staging option will use a testing server and will not issue valid certificates. When you have completed the procedure against the test server successfully, you can remove the –staging option to obtain the real certificate.
Ubuntu

Open a terminal and input the following commands:

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-apache 

Now you will run the command to install a certificate, follow the procedure and you will get everything configured out of the box:

$ sudo certbot --apache

Lastly, restart the webserver:

# systemctl restart apache2

If you need further help you can follow my other tutorial on Let’s Encrypt on Ubuntu (the apache part).

Image courtesy of mark | marksei
mark

You may also like...

23 Responses

  1. Marcin says:

    Internal Server Error

    The server encountered an internal error and was unable to complete your request.
    Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.
    More details can be found in the webserver log.

    • Horris says:

      Also make sure to set apache as the owner of the directory by typing:
      sudo chown -R www-data:www-data /var/www.

      • Marcin says:

        It was working right up to editing nextcloud.conf
        And my letsencrypt did work

        • Horris says:

          What does your nextcloud.conf look like could you copy and paste it if possible?

        • Zach Taylor says:

          Same here. I reverted back to a snapshot and skipped the virtual host thing for now. Not sure if I need it as I have a dedicated public ip for this machine.

          • mark says:

            You need a public IP address AND a domain name in order to set up that part.

          • Zach Taylor says:

            Thanks Mark. I do have a dedicated IP and public FQDN NAT’d which works fine. Before that step, my config looks like this:

            Alias /nextcloud “/var/www/nextcloud/”

            Options +FollowSymlinks
            AllowOverride All

            Dav off

            SetEnv HOME /var/www/nextcloud
            SetEnv HTTP_HOME /var/www/nextcloud

            Should I try again and do I need it?

            Also, the instruction say “make it look like this” obviously replacing your domain and email. I assumed it meant replace the whole config as opposed to append?

          • mark says:

            Hello Zach, thank you for using my guide. Yes you read that correctly, it means you have to replace the previous content. The one described in step 3 is only suitable for local setups.
            I have tested this setup before publishing it, and it works. Unfortunately, setting up DNS and port forwarding can sometimes produce a few hiccups down the road.
            That step is needed only if you wish to access your NextCloud instance from outside your local network. If that’s the case you should verify DNS resolution, port forwarding, firewall and that Apache is listening in order to cover the whole path.

          • Zach Taylor says:

            Hey Mark. I finally figured it out! You need to add the forward slashes / after these two lines:

            DocumentRoot /var/www/nextcloud

            should be:
            DocumentRoot /var/www/nextcloud/

            I did that and it’s now working. Thanks so much for the great tutorial. It’s by far the best out there!

          • mark says:

            Hello Zach, I’m glad you found a solution and thank you for sharing it with us : ) Though that’s actually strange, according to the Apache documentation the DocumentRoot directive shouldn’t include the trailing slash. I will investigate this issue as soon as I can reproduce it.

          • Zach Taylor says:

            Understood Mark. This is from the current Nextcloud documentation:


            DocumentRoot /var/www/nextcloud/
            ServerName your.server.com


            Require all granted
            AllowOverride All
            Options FollowSymLinks MultiViews


            Dav off



    • Galactica says:

      I had that happen to me after the Redis config,
      I went back and removed the “installed=true” line and just left the rest of it exactly how it pasted, I didnt touch it or move anything. reloaded apache2 and it worked.

    • mark says:

      Hello Marcin, please add some log : )

  2. Ken Wright says:

    I’ve just installed Nextcloud 16.0.3 on my Ubuntu Server 18.04. After your Step 5, all I got was a 404 status. I’m using nginx as my webserver and PHP 7.2, both of which are working well. Can you help?

    • mark says:

      Hello Ken, thank you for using my guide, are you sure you have configured Nginx to point at the correct root directory?

  3. ShiftYo says:

    Great guide, however during the php memory limit section you accidentally left a tag in the commands, instead of putting it on a few line, took me a few minutes to see that :D

    # a2enmod rewrite headers env dir mime# sed -i '/^memory_limit =/s/=.*/= 512M/' /etc/php/7.2/apache2/php.ini

    • mark says:

      Hello, sorry for the trouble, unfortunately Gutenberg and my syntax highlight plugin do not play well along, and sometimes this happens, I try to remove as many of these misfits as I can but they seem to randomly pop up revision after revision… Fixed : )

  4. Jean-Yves Lorne says:

    My instance became unreachable after step 7
    I own a domain name with a public IP. It’s working properly, a transmission client is working fine.

    YOURDOMAIN.TLD has to set in the config files as “www.yourdomain.tld” or “yourdomain.tld”?

    • mark says:

      It doesn’t really matter either way, in the guide the non-www variant is used. Check your firewall, maybe that’s the problem : )

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: