You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
12 KiB
Markdown

4 years ago
# Nextcloud in docker
4 years ago
###### guide-by-example
4 years ago
4 years ago
![logo](https://i.imgur.com/VXSovC9.png)
4 years ago
4 years ago
# Purpose & Overview
4 years ago
4 years ago
File share & sync.
4 years ago
4 years ago
* [Official site](https://nextcloud.com/)
* [Github](https://github.com/nextcloud/server)
* [DockerHub](https://hub.docker.com/_/nextcloud/)
4 years ago
4 years ago
Nextcloud is an open source suite of client-server software for creating
and using file hosting services with wide cross platform support.
The Nextcloud server is written in PHP and JavaScript.
For remote access it employs sabre/dav, an open-source WebDAV server.
It is designed to work with several database management systems,
including SQLite, MariaDB, MySQL, PostgreSQL.
There are many ways to deploy Nextcloud, this setup is going with the most goodies.</br>
4 years ago
Using [PHP-FPM](https://www.cloudways.com/blog/php-fpm-on-cloud/)
for better performance and using [Redis](https://aws.amazon.com/redis/)
for more reliable
4 years ago
[transactional file locking](https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/files_locking_transactional.html)
and for [memory file caching](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/caching_configuration.html).
4 years ago
# Files and directory structure
4 years ago
4 years ago
```
4 years ago
/home/
└── ~/
└── docker/
└── nextcloud/
├── nextcloud-data/
├── nextcloud-db-data/
├── .env
├── docker-compose.yml
4 years ago
├── nginx.conf
4 years ago
└── nextcloud-backup-script.sh
4 years ago
```
4 years ago
4 years ago
* `nextcloud-data/` - a directory where nextcloud will store users data and web app data
* `nextcloud-db-data/` - a directory where nextcloud will store its database data
4 years ago
* `.env` - a file containing environment variables for docker compose
4 years ago
* `docker-compose.yml` - a docker compose file, telling docker how to run the containers
4 years ago
* `nginx.conf` - nginx web server configuration file
* `nextcloud-backup-script.sh` - a backup script if you want it
You only need to provide the files.</br>
The directories are created by docker compose on the first run.
4 years ago
# docker-compose
4 years ago
Official examples [here](https://github.com/nextcloud/docker/tree/master/.examples/docker-compose)
4 years ago
Five containers to spin up
4 years ago
4 years ago
* **nextcloud-app** - nextcloud backend app that stores the files and facilitate
4 years ago
the sync and runs the apps
4 years ago
* **nextcloud-db** - mariadb database where files-metadata and users-metadata are stored
4 years ago
* **nextcloud-web** - nginx web server with fastCGI PHP-FPM support
4 years ago
* **nextcloud-redis** - in memory file caching
and more reliable transactional file locking
4 years ago
* **nextcloud-cron** - for periodic maintenance in the background
4 years ago
4 years ago
`docker-compose.yml`
```yml
version: '3'
services:
nextcloud-db:
image: mariadb
container_name: nextcloud-db
hostname: nextcloud-db
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: unless-stopped
4 years ago
env_file: .env
4 years ago
volumes:
- ./nextcloud-data-db:/var/lib/mysql
nextcloud-redis:
image: redis:alpine
container_name: nextcloud-redis
hostname: nextcloud-redis
restart: unless-stopped
4 years ago
nextcloud-app:
image: nextcloud:fpm-alpine
container_name: nextcloud-app
hostname: nextcloud-app
4 years ago
restart: unless-stopped
4 years ago
env_file: .env
4 years ago
depends_on:
- nextcloud-db
- nextcloud-redis
volumes:
- ./nextcloud-data/:/var/www/html
4 years ago
nextcloud-web:
image: nginx:alpine
container_name: nextcloud-web
hostname: nextcloud-web
restart: unless-stopped
volumes:
- ./nextcloud-data/:/var/www/html:ro
- ./nginx.conf:/etc/nginx/nginx.conf:ro
4 years ago
nextcloud-cron:
4 years ago
image: nextcloud:fpm-alpine
4 years ago
container_name: nextcloud-cron
hostname: nextcloud-cron
restart: unless-stopped
volumes:
- ./nextcloud-data/:/var/www/html
entrypoint: /cron.sh
depends_on:
- nextcloud-db
- nextcloud-redis
networks:
default:
external:
4 years ago
name: $DOCKER_MY_NETWORK
4 years ago
```
`.env`
```bash
# GENERAL
4 years ago
MY_DOMAIN=example.com
4 years ago
DOCKER_MY_NETWORK=caddy_net
4 years ago
TZ=Europe/Bratislava
4 years ago
# NEXTCLOUD-MARIADB
MYSQL_ROOT_PASSWORD=nextcloud
MYSQL_PASSWORD=nextcloud
MYSQL_DATABASE=nextcloud
MYSQL_USER=nextcloud
# NEXTCLOUD
MYSQL_HOST=nextcloud-db
REDIS_HOST=nextcloud-redis
# USING SENDGRID FOR SENDING EMAILS
4 years ago
MAIL_DOMAIN=example.com
4 years ago
MAIL_FROM_ADDRESS=nextcloud
SMTP_SECURE=ssl
SMTP_HOST=smtp.sendgrid.net
SMTP_PORT=465
SMTP_NAME=apikey
4 years ago
SMTP_PASSWORD=<sendgrid-api-key-goes-here>
4 years ago
```
4 years ago
`nginx.conf`
4 years ago
```
I wont be pasting it here
in full text,
4 years ago
but it is included in this github repo.
4 years ago
```
4 years ago
4 years ago
[nginx.conf](https://raw.githubusercontent.com/DoTheEvo/selfhosted-apps-docker/master/nextcloud/nginx.conf)
4 years ago
This is nginx web server configuration file, specifically setup
to support fastCGI PHP-FPM.
Taken from [this official nextcloud example
setup](https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/insecure/mariadb-cron-redis/fpm/web)
4 years ago
and has one thing changed in it - the upstream hostname from `app` to `nextcloud-app`
4 years ago
```
upstream php-handler {
server nextcloud-app:9000;
}
```
---
4 years ago
**All containers must be on the same network**.</br>
4 years ago
Which is named in the `.env` file.</br>
4 years ago
If one does not exist yet: `docker network create caddy_net`
4 years ago
4 years ago
# Reverse proxy
4 years ago
4 years ago
[Nextcloud official documentation](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/reverse_proxy_configuration.html)
4 years ago
regarding reverse proxy.
4 years ago
Caddy v2 is used, details
[here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2).</br>
4 years ago
There are few extra directives here to fix some nextcloud warnings.
`Caddyfile`
```
nextcloud.{$MY_DOMAIN} {
4 years ago
reverse_proxy nextcloud-web:80
4 years ago
header Strict-Transport-Security max-age=31536000;
redir /.well-known/carddav /remote.php/carddav 301
redir /.well-known/caldav /remote.php/caldav 301
}
```
4 years ago
4 years ago
# First run
4 years ago
4 years ago
Nextcloud needs few moments to start, then there is the initial configuration,
creating admin account.</br>
4 years ago
If database env variables were not passed in to nextcloud-app
then also the database info would be required here.
4 years ago
4 years ago
![first-run-pic](https://i.imgur.com/lv1x9GF.png)
4 years ago
4 years ago
The domain or IP you access nextcloud on this first run is added
to `trusted_domains` in `config.php`.
4 years ago
Changing the domain later on will throw *"Access through untrusted domain"* error.</br>
Editing `nextcloud-data/config/config.php` and adding the new domain will fix it.
4 years ago
4 years ago
# Security & setup warnings
4 years ago
4 years ago
Nextcloud has a status check in *Settings > Administration > Overview*</br>
4 years ago
There are likely several warnings on a freshly spun containers.
4 years ago
4 years ago
##### The database is missing some indexes
4 years ago
On the docker host execute:</br>
4 years ago
`docker exec --user www-data --workdir /var/www/html nextcloud-app php occ db:add-missing-indices`
4 years ago
4 years ago
##### Some columns in the database are missing a conversion to big int
4 years ago
4 years ago
On the docker host execute:</br>
4 years ago
`docker exec --user www-data --workdir /var/www/html nextcloud-app php occ db:convert-filecache-bigint`
4 years ago
4 years ago
##### The "Strict-Transport-Security" HTTP header is not set to at least "15552000" seconds.
4 years ago
Helps to know what is [HSTS](https://www.youtube.com/watch?v=kYhMnw4aJTw).</br>
This warning is already fixed in the reverse proxy section in the caddy config,</br>
the line: `header Strict-Transport-Security max-age=31536000;`
4 years ago
##### Your web server is not properly set up to resolve "/.well-known/caldav" and Your web server is not properly set up to resolve "/.well-known/carddav".
4 years ago
This warning is already fixed in the reverse proxy section in the caddy config,</br>
The lines:</br>
`redir /.well-known/carddav /remote.php/carddav 301`</br>
`redir /.well-known/caldav /remote.php/caldav 301`
4 years ago
4 years ago
![status-pic](https://i.imgur.com/wjjd5CJ.png)
4 years ago
4 years ago
# Troubleshooting
If there is a problem accesing nextcloud from a mobile app,
*"Please log in before granting access"*,
and being stuck after logging in with the circle animation:
Edit `nextcloud-data/config/config.php`</br>
adding as the last line: `'overwriteprotocol' => 'https',`
4 years ago
4 years ago
# Extra info
4 years ago
4 years ago
#### check if redis container works
4 years ago
4 years ago
At `https://<nexcloud url>/ocs/v2.php/apps/serverinfo/api/v1/info`</br>
ctrl+f for `redis`, should be in memcache.distributed and memcache.locking
4 years ago
4 years ago
You can also exec in to redis container:
4 years ago
- `docker exec -it nextcloud-redis /bin/sh`
4 years ago
- start monitoring: `redis-cli MONITOR`
- start browsing files on the nextcloud
- there should be activity in the monitoring
#### check if cron container works
- after letting Nextcloud run for a while
- in *settings > administration > basic settings*</br>
- background jobs should be set to Cron</br>
- the last job info should never be older than 10 minutes</br>
4 years ago
4 years ago
# Update
4 years ago
4 years ago
[Watchtower](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/watchtower)
updates the image automatically.
4 years ago
4 years ago
Manual image update:
- `docker-compose pull`</br>
- `docker-compose up -d`</br>
- `docker image prune`
4 years ago
4 years ago
# Backup and restore
4 years ago
4 years ago
#### Backup
Using [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup)
that makes daily snapshot of the entire directory.
#### Restore
* down the nextcloud containers `docker-compose down`</br>
* delete the entire nextcloud directory</br>
* from the backup copy back the nextcloud directory</br>
* start the containers `docker-compose up -d`
4 years ago
4 years ago
# Backup of just user data
4 years ago
4 years ago
User data daily export using the
[official procedure.](https://docs.nextcloud.com/server/latest/admin_manual/maintenance/backup.html)</br>
For nextcloud it means entering the maintenance mode, doing a database dump
and backing up several directories containing data, configs, themes.</br>
4 years ago
For the script it just means database dump as borg backup and its deduplication
4 years ago
will deal with the directories, especially useful in the case of nextcloud where
4 years ago
hundreds gigabytes can be stored.
4 years ago
4 years ago
#### Create a backup script
4 years ago
Placed inside `~/docker/nextcloud/` directory on the host.
4 years ago
`nextcloud-backup-script.sh`
```bash
#!/bin/bash
4 years ago
4 years ago
# MAINTENANCE MODE ON
4 years ago
docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --on
4 years ago
4 years ago
# CREATE DATABASE DUMP, bash -c '...' IS USED OTHERWISE OUTPUT > WOULD TRY TO GO TO THE HOST
docker container exec nextcloud-db bash -c 'mysqldump --single-transaction -h nextcloud-db -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > /var/lib/mysql/BACKUP.nextcloud.database.sql'
4 years ago
4 years ago
# MAINTENANCE MODE OFF
4 years ago
docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --off
4 years ago
```
4 years ago
4 years ago
The script must be **executable** - `chmod +x nextcloud-backup-script.sh`
4 years ago
Test run the script `sudo ./nextcloud-backup-script.sh`</br>
4 years ago
The resulting database dump is in
`nextcloud/nextcloud-data-db/BACKUP.nextcloud.database.sql`
#### Cronjob
Running on the host, so that the script will be periodically run.
4 years ago
4 years ago
* `su` - switch to root
* `crontab -e` - add new cron job</br>
* `0 23 * * * /home/bastard/docker/nextcloud/nextcloud-backup-script.sh`</br>
runs it every day [at 23:00](https://crontab.guru/#0_23_*_*_*)
* `crontab -l` - list cronjobs to check
4 years ago
4 years ago
# Restore the user data
4 years ago
4 years ago
Assuming clean start.
4 years ago
* start the containers: `docker-compose up -d`</br>
4 years ago
let them run so they create the file structure
4 years ago
* down the containers: `docker-compose down`
4 years ago
* delete the directories `configs`, `data`, `themes` in the freshly created
4 years ago
`nextcloud/nextcloud-data/`
4 years ago
* from the backup of `/nextcloud/nextcloud-data/`, copy the directories
4 years ago
`configs`, `data`, `themes` in to the new `/nextcloud/nextcloud-data/`
4 years ago
* from the backup of `/nextcloud/nextcloud-data-db/`, copy the backup database
4 years ago
named `BACKUP.nextcloud.database.sql` in to the new `/nextcloud/nextcloud-data-db/`
4 years ago
* start the containers: `docker-compose up -d`
* set the correct user ownership of the directories copied:</br>
4 years ago
`docker exec --workdir /var/www/html nextcloud-app chown -R www-data:www-data config data themes`
4 years ago
* restore the database</br>
`docker exec --workdir /var/lib/mysql nextcloud-db bash -c 'mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE < BACKUP.nextcloud.database.sql'`
* turn off the maintenance mode:</br>
4 years ago
`docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --off`
4 years ago
* update the systems data-fingerprint:</br>
4 years ago
`docker exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:data-fingerprint`
4 years ago
* restart the containers: `docker-compose restart`
* log in