Learn How To Install Nextcloud on Ubuntu 17.04

March 21, 2020

Table of Contents

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 = 127.0.0.1:9000

Now restart PHP7.1-fpm.

service php7.1-fpm restart

Caddy

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 https://getcaddy.com | 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:

your-domain-here.com {
root   /var/www/nextcloud
log    /var/log/nextcloud_access.log
errors /var/log/nextcloud_errors.log
fastcgi / 127.0.0.1:9000 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 {
    /.htacces
    /data
    /config
    /db_structure
    /.xml
    /README
}
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.

[Unit]
Description=Caddy HTTP/2 web server %I
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target
Wants=systemd-networkd-wait-online.service
[Service]
; run user and group for caddy
User=root
Group=root
ExecStart=/usr/local/bin/caddy -agree=true -conf=/etc/caddy/Caddyfile
Restart=on-failure
; create a private temp folder that is not shared with other processes
PrivateTmp=true
; limit the number of file descriptors, see `man systemd.exec` for more limit settings
LimitNOFILE=8192
[Install]
WantedBy=multi-user.target

Enable and run the Caddy server.

systemctl enable caddy.service
systemctl start caddy.service

MariaDB

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://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,i386] http://ftp.utexas.edu/mariadb/repo/10.2/ubuntu 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
CREATE DATABASE nextcloud;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'exampleUser'@'localhost' IDENTIFIED BY 'securepassword';
FLUSH PRIVILEGES;
exit

NextCloud

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

cd /var/www/nextcloud
wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.zip
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.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!