How to Install Nextcloud on Ubuntu 17.04

Posted on

In this tutorial, you will learn to install the NextCloud software. We will be using Caddy for our web server needs, as it allows easier configuration and simple issuing of SSL certificates. Instructions in this guide were written for NextCloud 12, but may also work for newer versions.

Before we begin, make sure your server is up-to-date, and install the required PHP 7.1 modules.

apt-get update
apt-get upgrade -y
apt-get install software-properties-common unzip

PHP 7.1

Install and configure PHP 7.1.

apt-get install -y python-software-properties
add-apt-repository -y ppa:ondrej/php
apt-get update -y
apt-get install -y php7.1 php7.1-fpm php7.1-cli php7.1-json php7.1-curl php7.1-imap php7.1-gd php7.1-mysql php7.1-xml php7.1-zip php7.1-intl php7.1-mcrypt php-imagick php7.1-mbstring

For Caddy, we will use TCP sockets instead of Unix sockets, as they are more optimized.

Edit the file /etc/php/7.1/fpm/pool.d/www.conf at around line 36. Look for the listen = /run/php/php7.1-fpm.sock. Comment it out and add the TCP socket instead, as shown below:

;listen = /run/php/php7.1-fpm.sock
listen =

Now restart PHP7.1-fpm.

service php7.1-fpm restart


Next, we have to install Caddy.

Running the Caddy setup script will automatically download the correct version for your system (32/64bit) and install it.

curl | bash -s personal

Create the root directory where we will serve files from:

mkdir -p /var/www/nextcloud

Create the folder /etc/caddy and the file “Caddyfile“.

mkdir -p /etc/caddy
nano /etc/caddy/Caddyfile

Now, paste the following contents: {

root   /var/www/nextcloud
log    /var/log/nextcloud_access.log
errors /var/log/nextcloud_errors.log

fastcgi / php {
    env PATH /bin

rewrite {
    r ^/index.php/.*$
    to /index.php?{query}

# client support (e.g. os x calendar / contacts)
redir /.well-known/carddav /remote.php/carddav 301
redir /.well-known/caldav /remote.php/caldav 301

# remove trailing / as it causes errors with php-fpm
rewrite {
    r ^/remote.php/(webdav|caldav|carddav|dav)(//?)$
    to /remote.php/{1}

rewrite {
    r ^/remote.php/(webdav|caldav|carddav|dav)/(.+?)(//?)$
    to /remote.php/{1}/{2}

rewrite {
    r ^/public.php/(.+?)(//?)$
    to /public.php/(.+?)(//?)$

# .htaccess / data / config / ... shouldn't be accessible from outside
status 403 {

header / Strict-Transport-Security "max-age=31536000;"


Let’s also turn Caddy into a service. Paste these lines to the file /etc/systemd/system/caddy.service.

Description=Caddy HTTP/2 web server %I

; run user and group for caddy
ExecStart=/usr/local/bin/caddy -agree=true -conf=/etc/caddy/Caddyfile

; create a private temp folder that is not shared with other processes

; limit the number of file descriptors, see `man systemd.exec` for more limit settings


Enable and run the Caddy server.

systemctl enable caddy.service
systemctl start caddy.service


A database is required for the NextCloud setup.

Note: Save and remember the root password you chose during MariaDB installation!

Run the following commands to install MariaDB. You may want to replace the MariaDB repository with another one of your choosing.

apt-key adv --recv-keys --keyserver hkp:// 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,i386] zesty main'
apt update
apt install mariadb-server -y

With MariaDB installed, we have to create a user and database for the NextCloud installation.

Run this command sequence:

mysql -u root -p
GRANT ALL PRIVILEGES ON nextcloud.* TO 'exampleUser'@'localhost' IDENTIFIED BY 'securepassword';


Change into the root directory for where the installation will reside and execute the following commands.

cd /var/www/nextcloud
unzip nextcloud-*
mv nextcloud/* .
chown -R www-data:www-data /var/www/nextcloud
rm -rf nextcloud-*.zip

Now visit your domain name or IP address and you will be greeted by the installation screen.
Follow the installation instructions on the screen and fill in the desired admin account details, database user, password and name.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article

Leave a Reply

Your email address will not be published. Required fields are marked *