Setting up Nextcloud

Introduction

This tutorial will explain how to set up a Nextcloud server with Docker.

Environment

Hardware amd64 VPS
OS Debian 10 Buster

Dependencies

  1. Docker
  2. docker-compose
  3. Internet Domain

Step by Step Instructions

1. Add DNS entry to your domain.

A domain of your choice needs to point to the address of your server.

2. Change to root user

sudo -s

3. Installation of additional Dependencies

apt update
apt install -y certbot nginx

4. Create nextcloud user and add persistence

useradd --system --create-home --shell /bin/bash --password <your-password-here> nextcloud
mkdir /home/nextcloud/nextcloud /home/nextcloud/apps /home/nextcloud/config /home/nextcloud/data /home/nextcloud/theme
chown -R nextcloud:nextcloud /home/nextcloud/nextcloud /home/nextcloud/apps /home/nextcloud/config /home/nextcloud/data /home/nextcloud/theme

5. Configure NGINX server

Create and open vhost file:

nano /etc/nginx/sites-available/nextcloud.domain.tld.vhost

Paste the following configuration into the file:

server {
  listen 80;
  server_name nextcloud.domain.tld;
  client_max_body_size 0;

  if ($host != "nextcloud.domain.tld") {
        return 444;
  }

  location ~* {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect default;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

  location ~ /.well-known/acme-challenge/ {
    allow all;
  }

}

Activate vhost file:

ln -s /etc/nginx/sites-available/nextcloud.domain.tld.vhost /etc/nginx/sites-enabled/nextcloud.domain.tld.vhost

Restart NGINX:

systemctl restart nginx

6. Create Let's Encrypt certificate for nextcloud.domain.tld

https://certbot.eff.org/lets-encrypt/debianbuster-nginx

Restart NGINX:

systemctl restart nginx

7. Run server

Create and open docker-compose.yaml:

cd /home/nextcloud
nano docker-compose.yaml

Paste the following configuration into the file:

version: '3.3'

services:
  nextcloud_db:
    image: postgres
    container_name: nextcloud_db
    command: --transaction-isolation="read committed"
    restart: unless-stopped
    volumes:
      - /home/nextcloud/db:/var/lib/postgresql/data
    environment:
      - 'POSTGRES_DB=nextcloud'
      - 'POSTGRES_USER=nextcloud'
      - 'POSTGRES_PASSWORD=your-database-password'

  nextcloud:
    image: nextcloud
    container_name: nextcloud
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - /home/nextcloud/nextcloud:/var/www/html
      - /home/nextcloud/apps:/var/www/html/custom_apps
      - /home/nextcloud/config:/var/www/html/config
      - /home/nextcloud/data:/var/www/html/data
      #- /home/nextcloud/theme:/var/www/html/themes/<YOUR_CUSTOM_THEME>
    environment:
      - 'POSTGRES_HOST=db'
      - 'POSTGRES_DB=nextcloud'
      - 'POSTGRES_USER=nextcloud'
      - 'POSTGRES_PASSWORD=your-database-password'
      - 'NEXTCLOUD_ADMIN_USER=your-nextcloud-admin-username'
      - 'NEXTCLOUD_ADMIN_PASSWORD=your-nextcloud-admin-password'
      - 'NEXTCLOUD_TRUSTED_DOMAINS=https://nextcloud.domain.tld nextcloud.domain.tld domain.tld'
    restart: unless-stopped

Run server:

docker-compose up -d

8. Set up server

Visit your website: https://nextcloud.domain.tld

Follow the on-screen instructions.

Sources