April 26, 2021

Nextcloud Snap, Ubuntu 20.04 & Caddy V2

Nextcloud is suitable for any home or office server, Raspberry Pi or VPS!

This guide covers installing and configuring the Snap implementation of Nextcloud with Collabora Online on Ubuntu 20.04 and Caddy V2 as reverse proxy.

We assume you have:

  • Basic Linux / sysadmin skills
  • Ubuntu 20.04 with sudo
  • Access to a domain name
  • Nothing occupying port 80 / 443

Note: if you're not using Ubuntu 20.04, the exact steps required to get everything working may vary from what's shown here.

Install Nextcloud & Collabora Online

The first and only step you need to get a minimal working Nextcloud instance at localhost:80:

sudo snap install nextcloud

Next, let's tell Nextcloud to use another port so we can have Caddy serve it up over HTTPS at your domain. Just about any vacant port will do - here we'll use 8111 :

sudo snap set nextcloud ports.http=8111

A memory limit of at least 512M is recommended:

sudo snap set nextcloud php.memory-limit=512M

Now let's create your very first admin user:

sudo nextcloud.manual-install admin password

Collabora Online takes Nextcloud to the next level with a Google-sheets like experience! Let's install Collabora Online, optionally with some other useful apps like Calendar, Contacts & Notes:

sudo nextcloud.occ app:enable richdocuments
sudo nextcloud.occ app:enable richdocumentscode
sudo nextcloud.occ app:enable calendar
sudo nextcloud.occ app:enable contacts
sudo nextcloud.occ app:enable notes

Here we set trusted_domains to ensure your Nextcloud instance will be reachable at your domain. Enter whatever domain name you own where you want Nextcloud to be reachable, it can be any valid domain like yourdomain.com or subdomain like cloud.yourdomain.com:

sudo nextcloud.occ config:system:set trusted_domains 1 --value=cloud.yourdomain.com

Now head over to the Nextcloud configuration file:

sudo nano /var/snap/nextcloud/current/nextcloud/config/config.php

Add the following line below 'version' => '20.0.9.1',:

'overwriteprotocol' => 'https',

Next, let's learn how to customize the data directory location with Nextcloud snap!

Change Nextcloud data location

By default, the Nextcloud snap stores your data locally in /var/snap/nextcloud/common/nextcloud/data. This might be fine for light users.

Follow these steps if you prefer to have Nextcloud store your data elsewhere, like an external hard drive, ZFS pool or NAS storage!

Note: for this to work, your drive/pool/storage must be mounted in /mnt or /media.

Create the new Nextcloud data directory if you haven't already:

sudo mkdir -p /media/YourDrive/Nextcloud/data

Install the removable-media plugin:

sudo snap connect nextcloud:removable-media

Stop the Nextcloud snap before updating the configuration:

sudo snap stop nextcloud

Edit the configuration file and set its datadirectory to your /media location:

sudo nano /var/snap/nextcloud/current/nextcloud/config/config.php
'datadirectory' => '/media/YourDrive/Nextcloud/data',

Copy the old data directory over to your new /media location:

sudo cp -r /var/snap/nextcloud/common/nextcloud/data /media/YourDrive/Nextcloud

Start the Nextcloud snap:

sudo snap start nextcloud

In the next section, we'll set up Caddy to serve up Nextcloud from your domain! This is a good time to log into your domain registrar and make sure your domain name is pointing to your server's IP address.

Install & Configure Caddy

We chose Caddy because it's relatively easy to use, and completely automates the process of obtaining, configuring and renewing HTTPS certificates!

Use the following commands to install Caddy:

sudo apt install curl
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee -a /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

Next, edit the Caddyfile:

cd /etc/caddy/
sudo nano Caddyfile

Paste this into the Caddyfile, change the domain name and port number as needed:

cloud.yourdomain.com {
    reverse_proxy localhost:8111
    file_server
}

Important: to apply the changes, do caddy reload. You should see something like this to indicate that your configuration changes have been loaded:

2021/04/24 03:15:27:081 INFO   using adjacent Caddyfile

Congratulations, you have successfully set up Nextcloud, Collabora and Caddy! Your Nextcloud instance is now available at https://cloud.yourdomain.com. From here you can go ahead and enjoy your new self-hosted cloud, or read on to the finishing touches for some extras.

Finishing Touches

Now that you've got everything working, it wouldn't hurt to become familiar with how to export and import your Nextcloud instance including configuration, database, apps and data - just in case!

To export or import your entire Nextcloud instance, it's as simple as:

sudo nextcloud.export
sudo nextcloud.import

See more details and some caveats on migrations at the nextcloud-snap Github.

To keep your Nextcloud snap up to date:

sudo snap refresh nextcloud

Update all your Nextcloud apps in one fell swoop:

sudo nextcloud.occ app:update --all

We hope you found this guide helpful!  Please contact us if you got stuck anywhere or have any feedback. Thank you for reading.