Caddy + Nextcloud (fpm) + Collabora - individual containers and docker-compose

@francislavoie looks like I’ve finally managed to get EVERYTHING working locally.

Through Nginx only using HTTP for everything, with ports 80 and 9980 exposed on the host.

With our beloved Caddy, through HTTPS with a local certificate and only ports 80 and 443 exposed on the host (later I’ll do it on the VPS, with a valid certificate).

I intend to make a good documentation to add to the Wiki, for now I leave here only the necessary files/commands to reproduce locally what I did here. Just change domain/host/users/passwords to your liking.

Another important warning: always try to test on more than one distribution. I like using Arch, I learn a lot and I like its “lean” way. But there have been two or three situations where things didn’t work as they should on Arch and with the same files/settings it worked on another distribution (in this case, Ubuntu 22.04 but it could have been any other).

I begin with ~/docker/nextcloud$ docker-compose down -v to be assured that I will start from scratch. Then,

~/docker/nextcloud$ clear ; docker-compose up -d ; docker logs -f nextcloud-app

Wait till

Initializing finished
[08-May-2022 20:40:16] NOTICE: fpm is running, pid 1
[08-May-2022 20:40:16] NOTICE: ready to handle connections

Then CTRL-C and

~/docker/nextcloud$ sudo vi /var/lib/docker/volumes/nextcloud_nextcloud-app/_data/config/config.php

Add these lines

  'trusted_proxies' =>
  array (
    0 => 'nextcloud-web',
  ),
  'default_phone_region' => 'XX',

where 'XX' is your country code, this will fix the warning “Your installation has no default phone region set.” and also will add Caddy’s container (nextcloud-web) to the list of trusted proxies (fixing another warning/errors). I like to add just bellow 'trusted_domains', at line 44, because reasons.

Save the file and restart the stack.

~/docker/nextcloud$ clear ; docker-compose restart ; docker logs -f nextcloud-web

I recommend open two more tabs/windows to keep an eye on logs of nextcloud-app and nextcloud-collabora.

~/docker/nextcloud$ clear ; docker logs -f nextcloud-app

~/docker/nextcloud$ clear ; docker logs -f nextcloud-collabora

In your browser open your URL. I always save the bookmark as http://nextcloud.local.cites.aop just to see Caddy change everything to HTTPS for me! :smiley:

As at this moment I am testing locally, I must agree and accept the self-signed certificate.

Nextcloud login screen opens.

After login:

  • close welcome animation

  • click con the little bell and see that all icons are fine (one of the errors is do not see the icons)

  • dismiss all notifications and accept (or not) notifications in your browser

  • click on the “A” (top left corner) and go to “Apps”

  • choose “Office & text” on the left

  • “Download and enable” Collabora Online (third top to bottom)

  • click on the “A” (top left corner) and go to “Settings”

  • click on “Overview” (left column) and enjoy that the only “warning” is about module php-imagick; there is a controversy if it must be installed and enabled or not (something about security, I did not investigate); if you want to install and enable, the command (for this image version I choose) is docker exec nextcloud-app apk add php7-pecl-imagick

  • scroll down the left column and click on “Nextcloud Office”

  • click “Use your own server”, check “Disable certificate verification (insecure)” and enter the same URL you are using to access your Nextcloud; in my case it would be “https://nextcloud.local.cites.aop” and click “Save”

  • you must be greeted by a “Saved” on the top right corner and a green checkmarked circle “Collabora Online server is reachable”

That’s it! (easy to say now, were more than three weeks Googling, reading forums, asking for help, etc)

You have latest Nextcloud, latest Collabora, latest Postgres (may change to Mariadb, if you choose to) all “powered” by Caddy!

@francislavoie thank you, all times I asked for help you readily helped.

The next challenge is to put this “stack” (including nextcloud-web-caddy) behind the REAL reverse-proxy! Another Caddy! :wink:

Bellow,

  • docker-compose.yml

  • Caddyfile

  • nginx.conf

version: '2'
services:

  nextcloud-db:
    image: postgres:14.2-alpine
    container_name: nextcloud-db
    restart: unless-stopped
    volumes:
      - nextcloud-db:/var/lib/postgresql/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      POSTGRES_USER: nextcloud
      POSTGRES_PASSWORD: password

  nextcloud-cache:
    image: redis:7.0.0-alpine
    container_name: nextcloud-cache
    restart: unless-stopped
    mem_limit: 2048m
    mem_reservation: 512m
    command: redis-server --requirepass password

  nextcloud-app:
    image: nextcloud:24.0.0-fpm-alpine
    container_name: nextcloud-app
    restart: unless-stopped
    #ports:
    #  - 9000:9000
    volumes:
      - nextcloud-app:/var/www/html
    depends_on:
      - nextcloud-db
      - nextcloud-cache
    environment:
      POSTGRES_DB: nextcloud
      POSTGRES_USER: nextcloud
      POSTGRES_PASSWORD: password
      POSTGRES_HOST: nextcloud-db
      NEXTCLOUD_ADMIN_USER: admin
      NEXTCLOUD_ADMIN_PASSWORD: password
      NEXTCLOUD_TRUSTED_DOMAINS: nextcloud.local.cites.aop
      REDIS_HOST: nextcloud-cache
      REDIS_HOST_PASSWORD: password
      SMTP_HOST: mail.local.cites.aop
      SMTP_SECURE: tls
      SMTP_PORT: 587
      SMTP_AUTHTYPE: LOGIN
      SMTP_NAME: mailer@local.cites.aop
      SMTP_PASSWORD: password
      MAIL_FROM_ADDRESS: no-reply
      MAIL_DOMAIN: local.cites.aop

  # to use Nginx instead Caddy, uncomment this "nextcloud-web",
  # comment the next and use the supplied nginx.conf
  # 
  #nextcloud-web:
  #  image: nginx:1.21.6-alpine
  #  container_name: nextcloud-web
  #  restart: unless-stopped
  #  ports:
  #    - 80:80
  #  volumes:
  #    - ./nginx.conf:/etc/nginx/nginx.conf:ro
  #  volumes_from:
  #    - nextcloud-app

  nextcloud-web:
    image: caddy:2.5.1-alpine
    container_name: nextcloud-web
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - nextcloud-caddy-data:/data
      - nextcloud-caddy-config:/config
    volumes_from:
      - nextcloud-app

  nextcloud-collabora:
    image: collabora/code:21.11.4.2.1
    container_name: nextcloud-collabora
    restart: unless-stopped
    #ports:
    #  - 9980:9980
    volumes:
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      - nextcloud-web
    environment:
      - username=admin
      - password=password
      - dictionaries=en_US es_ES pt_BR
     # out of desperation, I was going to start using permutations...
     #- extra_params=--o:ssl.enable=true
     #- extra_params=--o:ssl.enable=false
     #- extra_params=--o:ssl.enable=true  --o:ssl.termination=false
     #- extra_params=--o:ssl.enable=true  --o:ssl.termination=true
     #- extra_params=--o:ssl.enable=false --o:ssl.termination=true
     #- extra_params=--o:ssl.enable=false --o:ssl.termination=false

volumes:
  nextcloud-app:
  nextcloud-db:
  nextcloud-caddy-data:
  nextcloud-caddy-config:
{
	#debug
}

nextcloud.local.cites.aop {

	acme_server
	tls internal

	encode gzip

	redir /.well-known/carddav /remote.php/dav 301
	redir /.well-known/caldav /remote.php/dav 301

	header {
		Strict-Transport-Security max-age=31536000;
	}

	@collabora {
		path /browser/*
		path /hosting/discovery
		path /hosting/capabilities
		path /cool/*
	}

	reverse_proxy @collabora https://nextcloud-collabora:9980 {
		header_up Host "nextcloud.local.cites.aop"
		transport http {
			tls_insecure_skip_verify
		}
	}

	# .htaccess / data / config / ... shouldn't be accessible from outside
	@forbidden {
		path /.htaccess
		path /data/*
		path /config/*
		path /db_structure
		path /.xml
		path /README
		path /3rdparty/*
		path /lib/*
		path /templates/*
		path /occ
		path /console.php
	}
	respond @forbidden 404

	root * /var/www/html

	php_fastcgi nextcloud-app:9000

	file_server
}
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    # Prevent nginx HTTP Server Detection
    server_tokens   off;

    keepalive_timeout  65;

    #gzip  on;

    upstream php-handler {
        server nextcloud-app:9000;
    }

    server {
        listen 80;

        # HSTS settings
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;

        # set max upload size
        client_max_body_size 512M;
        fastcgi_buffers 64 4K;

        # Enable gzip but do not remove ETag headers
        gzip on;
        gzip_vary on;
        gzip_comp_level 4;
        gzip_min_length 256;
        gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
        gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

        # Pagespeed is not supported by Nextcloud, so if your server is built
        # with the `ngx_pagespeed` module, uncomment this line to disable it.
        #pagespeed off;

        # HTTP response headers borrowed from Nextcloud `.htaccess`
        add_header Referrer-Policy                      "no-referrer"   always;
        add_header X-Content-Type-Options               "nosniff"       always;
        add_header X-Download-Options                   "noopen"        always;
        add_header X-Frame-Options                      "SAMEORIGIN"    always;
        add_header X-Permitted-Cross-Domain-Policies    "none"          always;
        add_header X-Robots-Tag                         "none"          always;
        add_header X-XSS-Protection                     "1; mode=block" always;

        # Remove X-Powered-By, which is an information leak
        fastcgi_hide_header X-Powered-By;

        # Path to the root of your installation
        root /var/www/html;

        # Specify how to handle directories -- specifying `/index.php$request_uri`
        # here as the fallback means that Nginx always exhibits the desired behaviour
        # when a client requests a path that corresponds to a directory that exists
        # on the server. In particular, if that directory contains an index.php file,
        # that file is correctly served; if it doesn't, then the request is passed to
        # the front-end controller. This consistent behaviour means that we don't need
        # to specify custom rules for certain paths (e.g. images and other assets,
        # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus
        # `try_files $uri $uri/ /index.php$request_uri`
        # always provides the desired behaviour.
        index index.php index.html /index.php$request_uri;

        # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
        location = / {
            if ( $http_user_agent ~ ^DavClnt ) {
                return 302 /remote.php/webdav/$is_args$args;
            }
        }

        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }

        # Make a regex exception for `/.well-known` so that clients can still
        # access it despite the existence of the regex rule
        # `location ~ /(\.|autotest|...)` which would otherwise handle requests
        # for `/.well-known`.
        location ^~ /.well-known {
            # The rules in this block are an adaptation of the rules
            # in `.htaccess` that concern `/.well-known`.

            location = /.well-known/carddav { return 301 /remote.php/dav/; }
            location = /.well-known/caldav  { return 301 /remote.php/dav/; }

            location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
            location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

            # Let Nextcloud's API for `/.well-known` URIs handle all other
            # requests by passing them to the front-end controller.
            return 301 /index.php$request_uri;
        }

        # Rules borrowed from `.htaccess` to hide certain paths from clients
        location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
        location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)                { return 404; }

        # Ensure this block, which passes PHP files to the PHP process, is above the blocks
        # which handle static assets (as seen below). If this block is not declared first,
        # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
        # to the URI, resulting in a HTTP 500 error response.
        location ~ \.php(?:$|/) {
            # Required for legacy support
            rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;

            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            set $path_info $fastcgi_path_info;

            try_files $fastcgi_script_name =404;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $path_info;
            #fastcgi_param HTTPS on;

            fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
            fastcgi_param front_controller_active true;     # Enable pretty urls
            fastcgi_pass php-handler;

            fastcgi_intercept_errors on;
            fastcgi_request_buffering off;
        }

        location ~ \.(?:css|js|svg|gif)$ {
            try_files $uri /index.php$request_uri;
            expires 6M;         # Cache-Control policy borrowed from `.htaccess`
            access_log off;     # Optional: Don't log access to assets
        }

        location ~ \.woff2?$ {
            try_files $uri /index.php$request_uri;
            expires 7d;         # Cache-Control policy borrowed from `.htaccess`
            access_log off;     # Optional: Don't log access to assets
        }

        # Rule borrowed from `.htaccess`
        location /remote {
            return 301 /remote.php$request_uri;
        }

        location / {
            try_files $uri $uri/ /index.php$request_uri;
        }
    }
}
2 Likes