Hello there, In this tutorial, we'll see how to install Nextcloud on Ubuntu 18.04 server. This guide will be applicable even if you have Ubuntu 16.04 or Ubuntu 14.04.

Nextcloud is a self-hosted cloud storage solution, similar to Dropbox, but it is open-source. It allows you to take full control over your instance with features like account management, Quota management, etc. and improves security by not leaking metadata.

First, we'll set up our new VM by updating packages and improving security; then we'll install Apache server for serving our site, MySQL or PostgreSQL for database, Let's Encrypt to add SSL to our domain, then finally install and configure Nextcloud instance. So let's get started.


In this tutorial, we'll use:

  1. Ubuntu 18.04 server
    • The best solution out there for developers is DigitalOcean. You can use my referral code to sign up.
    • You'll need a droplet(or VM) with minimum 512 MB Ram.
  2. A Domain name pointing to the IP of your VM
    • If my VM IP is, then your domain A DNS - cloud.example.com should to

Initial Server Setup

Whenever you create a new cloud server, you'll need some configuration steps before using the server which will increase security and usability. This DigitalOcean guide is the best. Follow the guide and then come back here and check some points below:

  1. You should be able to login to your server with a normal user and not the root user.
  2. You should have ufw setup or network firewall enabled if provided by your provider with port 22 enabled.
  3. You should always use ssh keys to log in and not the password, Also disable root login. To do this:
    $ sudo nano /etc/ssh/sshd_config
    • Change the following as below:
      PasswordAuthentication no
      PubkeyAuthentication yes
      ChallengeResponseAuthentication no
      PermitRootLogin no
    • Then, run:
      $ sudo systemctl reload sshd

Installing required packages

  1. Upgrade your packages and system:
    • This will ensure you'll install all the latest packages from ubuntu repo. Run the following commands.
      $ sudo apt-get update
      $ sudo apt-get upgrade
      $ sudo apt-get dist-upgrade
    • If you installed packages when running dist-upgrade, it's good practice to reboot the system.
      $ sudo reboot
  2. Install Apache:
    • We'll use Apache web server to serve our website. It's free and open-source.
    • Install it using:
      $ sudo apt-get install apache2
    • Now, you'll need to open up ports 80 and 443. If you use your provider firewall, do it in your UI, if you use ufw run this:
      sudo ufw allow in "Apache Full"
    • Then open the file using $ sudo nano /etc/apache2/apache2.conf, at the bottom add:
      ServerName IP_HERE
    • You can find your IP by running the command:
      $ curl http://icanhazip.com
    • Then, check for syntax errors by typing:
      $ sudo apache2ctl configtest
    • This file should say Syntax OK. Then, restart apache using:
      $ sudo service apache2 restart
    • Now, if you go to http://your_ip, you should see apache's default page.
  3. Install MySQL Database:
    • This is the recommended database by Nextcloud developers. But, you can also use Postgres Database(see next point). You'll only need one database, so install the one with which you're comfortable.
    • Run the following command:
      sudo apt-get install mysql-server mysql-client libmysqlclient-dev
    • Create a user, set password and create a database:
      sudo mysql
      CREATE DATABASE ncdb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
      GRANT ALL ON ncdb.* TO 'ncuser'@'localhost' IDENTIFIED BY '1234';
  4. Install Postgres: (optional)
    • Run the following commands:
      $ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
      $ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
      $ sudo apt-get update
      $ sudo apt-get install postgresql-common
      $ sudo apt-get install postgresql-9.6 libpq-dev
    • Create user and set password:
      $ sudo -u postgres createuser ncuser -s
      # above, change ncuser to any other name
      # set password
      $ sudo -u postgres psql
      postgres=# \password ncuser
    • Create Database: Don't forget to change ncuser to your user created above.
      postgres=# CREATE DATABASE ncdb TEMPLATE template0 ENCODING 'UTF8' OWNER ncuser;
    • You can quit Postgres commandline using \q command.
  5. Install PHP and some prerequisites modules nextcloud requires:
    • Install PHP using:
      $ sudo apt install php libapache2-mod-php
    • If you're using Postgres run this:
      $ sudo apt install php-ctype php-curl php-dom php-gd php-iconv php-json php-mbstring php-posix php-zip php-fileinfo php-bz2 php-intl php-ldap php-exif php-apcu php-imagick php-pgsql
    • If you're using MySQL run this:
      $ sudo apt install php-ctype php-curl php-dom php-gd php-iconv php-json php-mbstring php-posix php-zip php-fileinfo php-bz2 php-intl php-ldap php-exif php-apcu php-imagick php-mysql
  6. Postgresql and PHP:(Skip this if you're using MYSQL)
    • To make PHP recognize pdo_pgsql for Postgres DB, run: $ sudo nano /etc/postgresql/9.6/main/pg_hba.conf and change this line:
      # "local" is for Unix domain socket connections only
      local   all      all                                trust
    • Then, Change the $ sudo nano /etc/php/7.2/mods-available/pgsql.ini file:
      pgsql.allow_persistent = On
      pgsql.auto_reset_persistent = Off
      pgsql.max_persistent = -1
      pgsql.max_links = -1
      pgsql.ignore_notice = 0
      pgsql.log_notice = 0
    • Then restart Postgres:
      sudo service postgresql restart

Setting up domain and SSL

You must have a domain, and it must point to your cloud server IP.  You can see how to do this here.

In this example, your domain will be cloud.example.com. Follow the steps and remember to change cloud.example.com to your domain name everywhere:

  1. For testing SSL, we'll add a dummy index.html file for now.
    • Make folder and file:
      $ sudo mkdir -p /var/www/cloud.example.com/nextcloud/
      $ sudo touch /var/www/cloud.example.com/nextcloud/index.html
    • Edit the file: $ sudo nano /var/www/cloud.example.com/nextcloud/index.html:
              <title>Hello with SSL</title>
              <p>Hello with SSL</p>
  2. Next, we'll add virtual hosts for Apache.
    • Run sudo nano /etc/apache2/sites-available/cloud.example.com.conf and add:
      <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName cloud.example.com
        Alias / "/var/www/cloud.example.com/nextcloud/"
        <Directory /var/www/cloud.example.com/nextcloud/>
          Options +FollowSymlinks
          AllowOverride All
          <IfModule mod_dav.c>
            Dav off
          SetEnv HOME /var/www/cloud.example.com/nextcloud
          SetEnv HTTP_HOME /var/www/cloud.example.com/nextcloud
        <IfModule mod_headers.c>
          Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    • Close the file by pressing ctrl + X then Y.
    • Run the follwoing commands:
      sudo a2ensite cloud.example.com.conf
      sudo a2dissite 000-default.conf
      sudo service apache2 restart
  3. Let's add LetsEncrypt free SSL now:
    • Install Certbot:
      sudo add-apt-repository ppa:certbot/certbot
      # press ENTER if asked
      sudo apt install python-certbot-apache
    • Install SSL for our domain:
      sudo certbot --apache -d cloud.example.com
    • You will be asked "Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access." Press 2: Redirect all traffic to https.
  4. Now if you go to https://cloud.example.com, you should see your index.html file contents.

Nextcloud Installation

You've all the prerequisites installed, now is the time to install Nextcloud.

  1. Go to https://nextcloud.com/install/ and download the latest stable version of Nextcloud. While writing this tutorial, the stable version is 15.0.0.
    wget https://download.nextcloud.com/server/releases/nextcloud-15.0.0.zip
  2. Delete our dummy index.html file which we used for the testing purpose in the last step. Run:
    sudo rm -rf /var/www/cloud.example.com/nextcloud
  3. Unzip and copy this to our directory:
    sudo apt-get install unzip
    unzip nextcloud-15.0.0.zip
    sudo cp -r nextcloud /var/www/cloud.example.com
  4. This nextcloud directory needs proper permissions to work:
    sudo chown -R www-data:www-data /var/www/cloud.example.com/nextcloud/
  5. Ensure that you've all required modules enabled by running:
    sudo a2enmod rewrite
    sudo a2enmod headers
    sudo a2enmod env
    sudo a2enmod dir
    sudo a2enmod mime
  6. Finally, restart the Apache server:
    sudo service apache2 restart
  7. That's it, now go to https://cloud.example.com, create an admin user and fill the database credentials. Wait for around 45 seconds, and you're good to go.


Now, you can use Nextcloud anywhere by just logging in and also download apps from here. By default, Nextcloud comes with a Gallery, Video player and more. You can extend these capabilities with Apps available in App Store. Nextcloud also improves security by preventing brute-force login attacks, implementing Content Security Policy(CSP), SAML authentication, etc.

Some further steps to take:

  1. Connect external storage
  2. Enable End-to-End Encryption: Watch this video
  3. Setup Nextcloud Talk
  4. Manage your users in Settings > Users. Assign each user some quota's if you have limited space. Add External Storage support app to store your data elsewhere.
  5. Enable and Enforce two-factor authentication for all users.