selfhosted-apps-docker/nextcloud/readme.md

352 lines
12 KiB
Markdown
Raw Normal View History

2020-04-09 22:52:11 +00:00
# Nextcloud in docker
2020-05-18 22:49:18 +00:00
###### guide-by-example
2020-04-09 22:52:11 +00:00
2020-04-16 22:05:54 +00:00
![logo](https://i.imgur.com/VXSovC9.png)
2020-04-09 22:52:11 +00:00
2020-05-08 09:50:52 +00:00
# Purpose & Overview
2020-04-09 22:52:11 +00:00
2020-04-10 23:51:47 +00:00
File share & sync.
2020-04-09 22:52:11 +00:00
2020-04-10 23:51:47 +00:00
* [Official site](https://nextcloud.com/)
* [Github](https://github.com/nextcloud/server)
* [DockerHub](https://hub.docker.com/_/nextcloud/)
2020-04-09 22:52:11 +00:00
2023-02-18 09:11:26 +00:00
Nextcloud is an open source software for sharing files, calendar,
and general office collaboration stuff. Most people know it and use it
as an alternative to onedrive/google drive.
2020-05-08 09:50:52 +00:00
The Nextcloud server is written in PHP and JavaScript.
For remote access it employs sabre/dav, an open-source WebDAV server.
2023-02-18 09:11:26 +00:00
It is designed to work with most of the databases.
2020-05-08 09:50:52 +00:00
There are many ways to deploy Nextcloud, this setup is going with the most goodies.</br>
2020-05-08 11:03:56 +00:00
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
2020-05-08 09:50:52 +00:00
[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).
2020-04-18 09:07:00 +00:00
# Files and directory structure
2020-04-09 22:52:11 +00:00
2020-04-25 23:31:16 +00:00
```
2020-05-01 09:38:43 +00:00
/home/
└── ~/
└── docker/
└── nextcloud/
2023-02-18 09:11:26 +00:00
├── 🗁 nextcloud_data/
├── 🗁 nextcloud_db_data/
2023-02-17 21:52:55 +00:00
├── 🗋 .env
├── 🗋 docker-compose.yml
├── 🗋 nginx.conf
└── 🗋 nextcloud-backup-script.sh
2020-04-25 23:31:16 +00:00
```
2020-04-09 22:52:11 +00:00
2023-02-18 09:11:26 +00:00
* `nextcloud_data/` - users actual data and web app data
* `nextcloud_db_data/` - database data - users and files metadata, configuration
2020-05-22 16:05:03 +00:00
* `.env` - a file containing environment variables for docker compose
2020-05-22 16:22:45 +00:00
* `docker-compose.yml` - a docker compose file, telling docker how to run the containers
2020-05-08 09:50:52 +00:00
* `nginx.conf` - nginx web server configuration file
2023-02-17 21:52:55 +00:00
* `nextcloud-backup-script.sh` - a backup script, to be run daily
2020-05-08 09:50:52 +00:00
You only need to provide the files.</br>
The directories are created by docker compose on the first run.
2020-04-18 09:07:00 +00:00
# docker-compose
2020-04-10 23:51:47 +00:00
Official examples [here](https://github.com/nextcloud/docker/tree/master/.examples/docker-compose)
2020-05-08 11:03:56 +00:00
Five containers to spin up
2020-04-27 21:01:48 +00:00
2020-05-08 09:50:52 +00:00
* **nextcloud-app** - nextcloud backend app that stores the files and facilitate
2023-02-18 09:11:26 +00:00
the sync and runs the apps(calendar, notes, phonetrack,...)
* **nextcloud-db** - mariadb database storing files-metadata and users-metadata
2020-05-09 11:26:29 +00:00
* **nextcloud-web** - nginx web server with fastCGI PHP-FPM support
2023-02-18 09:11:26 +00:00
* **nextcloud-redis** - in memory file caching and more reliable transactional
file locking
2020-05-09 11:26:29 +00:00
* **nextcloud-cron** - for periodic maintenance in the background
2020-04-10 23:51:47 +00:00
2020-04-25 23:31:16 +00:00
`docker-compose.yml`
```yml
version: '3'
services:
nextcloud-db:
image: mariadb
container_name: nextcloud-db
hostname: nextcloud-db
2021-12-31 00:35:58 +00:00
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb_read_only_compressed=OFF
2020-04-25 23:31:16 +00:00
restart: unless-stopped
2020-05-20 18:03:11 +00:00
env_file: .env
2020-04-25 23:31:16 +00:00
volumes:
2023-02-17 21:52:55 +00:00
- ./nextcloud_data_db:/var/lib/mysql
2020-04-25 23:31:16 +00:00
nextcloud-redis:
2023-02-17 21:52:55 +00:00
image: redis:alpine
2020-04-25 23:31:16 +00:00
container_name: nextcloud-redis
hostname: nextcloud-redis
restart: unless-stopped
2020-05-08 09:50:52 +00:00
nextcloud-app:
image: nextcloud:fpm-alpine
container_name: nextcloud-app
hostname: nextcloud-app
2020-04-25 23:31:16 +00:00
restart: unless-stopped
2020-05-20 18:03:11 +00:00
env_file: .env
2020-04-25 23:31:16 +00:00
depends_on:
- nextcloud-db
- nextcloud-redis
volumes:
2023-02-17 21:52:55 +00:00
- ./nextcloud_data/:/var/www/html
2020-04-25 23:31:16 +00:00
2020-05-08 09:50:52 +00:00
nextcloud-web:
image: nginx:alpine
container_name: nextcloud-web
hostname: nextcloud-web
restart: unless-stopped
volumes:
2023-02-17 21:52:55 +00:00
- ./nextcloud_data/:/var/www/html:ro
2020-05-08 09:50:52 +00:00
- ./nginx.conf:/etc/nginx/nginx.conf:ro
2023-02-17 21:52:55 +00:00
expose:
- 80:80
2020-05-08 09:50:52 +00:00
2020-04-25 23:31:16 +00:00
nextcloud-cron:
2020-05-08 09:50:52 +00:00
image: nextcloud:fpm-alpine
2020-04-25 23:31:16 +00:00
container_name: nextcloud-cron
hostname: nextcloud-cron
restart: unless-stopped
volumes:
2023-02-17 21:52:55 +00:00
- ./nextcloud_data/:/var/www/html
2020-04-25 23:31:16 +00:00
entrypoint: /cron.sh
depends_on:
- nextcloud-db
- nextcloud-redis
networks:
default:
2023-02-17 21:52:55 +00:00
name: $DOCKER_MY_NETWORK
external: true
2020-04-25 23:31:16 +00:00
```
`.env`
```bash
# GENERAL
2020-05-20 18:29:12 +00:00
DOCKER_MY_NETWORK=caddy_net
2020-05-02 20:48:23 +00:00
TZ=Europe/Bratislava
2020-04-25 23:31:16 +00:00
# NEXTCLOUD-MARIADB
MYSQL_ROOT_PASSWORD=nextcloud
2023-02-17 21:52:55 +00:00
MARIADB_AUTO_UPGRADE=1
MARIADB_DISABLE_UPGRADE_BACKUP=1
2020-04-25 23:31:16 +00:00
MYSQL_PASSWORD=nextcloud
MYSQL_DATABASE=nextcloud
MYSQL_USER=nextcloud
2023-02-17 21:52:55 +00:00
# NEXTCLOUD-APP
2020-04-25 23:31:16 +00:00
MYSQL_HOST=nextcloud-db
REDIS_HOST=nextcloud-redis
2023-02-17 21:52:55 +00:00
OVERWRITEPROTOCOL=https
TRUSTED_PROXIES=caddy
NC_default_phone_region=SK # CHANGE TO YOUR COUNTRY CODE
2020-04-25 23:31:16 +00:00
2023-02-17 21:52:55 +00:00
# USING SENDINBLUE FOR SENDING EMAILS
2020-05-16 13:18:21 +00:00
MAIL_DOMAIN=example.com
2020-04-25 23:31:16 +00:00
MAIL_FROM_ADDRESS=nextcloud
2023-02-17 21:52:55 +00:00
SMTP_SECURE=tls
SMTP_HOST=smtp-relay.sendinblue.com
SMTP_PORT=587
SMTP_NAME=<registration-email@gmail.com>
SMTP_PASSWORD=<smtp-key-goes-here>
2020-04-25 23:31:16 +00:00
```
2020-05-08 09:50:52 +00:00
`nginx.conf`
2020-05-09 11:26:29 +00:00
```
2023-02-17 21:52:55 +00:00
Not be pasted here, too long.
It is included in this github repo.
2020-05-09 11:26:29 +00:00
```
2020-05-08 09:50:52 +00:00
2023-02-18 09:11:26 +00:00
[nginx.conf](https://raw.githubusercontent.com/DoTheEvo/selfhosted-apps-docker/master/nextcloud/nginx.conf)<br>
2020-05-08 09:50:52 +00:00
This is nginx web server configuration file, specifically setup
2023-02-18 09:11:26 +00:00
to support fastCGI PHP-FPM.<br>
From [this official nextcloud example
setup](https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/insecure/mariadb/fpm/web)
2020-05-09 11:26:29 +00:00
and has one thing changed in it - the upstream hostname from `app` to `nextcloud-app`
2020-05-08 09:50:52 +00:00
```
upstream php-handler {
server nextcloud-app:9000;
}
```
---
2020-04-25 23:31:16 +00:00
**All containers must be on the same network**.</br>
2020-05-08 09:50:52 +00:00
Which is named in the `.env` file.</br>
2020-04-25 23:31:16 +00:00
If one does not exist yet: `docker network create caddy_net`
2020-04-09 22:52:11 +00:00
2020-04-18 09:07:00 +00:00
# Reverse proxy
2020-04-09 22:52:11 +00:00
2020-04-25 23:31:16 +00:00
[Nextcloud official documentation](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/reverse_proxy_configuration.html)
2020-04-27 21:01:48 +00:00
regarding reverse proxy.
2020-05-01 09:51:20 +00:00
Caddy v2 is used, details
[here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2).</br>
2020-04-25 23:31:16 +00:00
There are few extra directives here to fix some nextcloud warnings.
`Caddyfile`
2023-02-17 21:52:55 +00:00
```php
2020-04-25 23:31:16 +00:00
nextcloud.{$MY_DOMAIN} {
header Strict-Transport-Security max-age=31536000;
redir /.well-known/carddav /remote.php/carddav 301
redir /.well-known/caldav /remote.php/caldav 301
2023-02-17 21:52:55 +00:00
redir /.well-known/webfinger /index.php/.well-known/webfinger 301
redir /.well-known/nodeinfo /index.php/.well-known/nodeinfo 301
reverse_proxy nextcloud-web:80
2020-04-25 23:31:16 +00:00
}
```
2020-04-09 22:52:11 +00:00
2020-04-18 09:07:00 +00:00
# First run
2020-04-09 22:52:11 +00:00
2020-05-20 18:03:11 +00:00
Nextcloud needs few moments to start, then there is the initial configuration,
creating admin account.</br>
2023-02-17 21:52:55 +00:00
If database env variables were not used then also the database info
would be required here.
2020-04-09 22:52:11 +00:00
2020-05-20 18:03:11 +00:00
![first-run-pic](https://i.imgur.com/lv1x9GF.png)
2020-04-09 22:52:11 +00:00
2020-04-18 20:03:47 +00:00
The domain or IP you access nextcloud on this first run is added
to `trusted_domains` in `config.php`.
2020-05-08 11:03:56 +00:00
Changing the domain later on will throw *"Access through untrusted domain"* error.</br>
2023-02-17 21:52:55 +00:00
Editing `nextcloud_data/config/config.php` and adding the new domain will fix it.
2020-04-18 20:03:47 +00:00
2020-04-18 09:07:00 +00:00
# Security & setup warnings
2020-04-09 22:52:11 +00:00
2020-04-27 21:01:48 +00:00
Nextcloud has a status check in *Settings > Administration > Overview*</br>
2023-02-18 09:11:26 +00:00
There could be some warnings there, but if following this guide, it should be
all good. As `Caddyfile` and `.env` file should take care of it.
2020-04-10 23:51:47 +00:00
2023-02-18 09:11:26 +00:00
[Here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/a86c8498dc8ebc59546660701a54b839bf417516/nextcloud#security--setup-warnings)
is a link to an older commit that talks in more detail on possible stuff here.<br>
But fuck writing on that noise when nextcloud is now doing phone number area
code notification there.
2020-04-25 23:31:16 +00:00
2023-02-18 09:11:26 +00:00
![status-pic](https://i.imgur.com/0nltwrn.png)
2020-04-10 23:51:47 +00:00
2020-05-10 00:32:57 +00:00
# Troubleshooting
2023-02-18 09:11:26 +00:00
* *old stuff that was here is not applicable anymore*
2020-04-09 22:52:11 +00:00
2020-04-18 09:07:00 +00:00
# Extra info
2020-04-09 22:52:11 +00:00
2020-05-08 11:03:56 +00:00
#### check if redis container works
2020-04-09 22:52:11 +00:00
2020-05-08 11:03:56 +00:00
At `https://<nexcloud url>/ocs/v2.php/apps/serverinfo/api/v1/info`</br>
2023-02-18 09:11:26 +00:00
ctrl+f for `redis`, if it's present it means nexcloud is set to use it.
2020-04-09 22:52:11 +00:00
2020-05-08 11:03:56 +00:00
You can also exec in to redis container:
2020-05-09 11:26:29 +00:00
- `docker exec -it nextcloud-redis /bin/sh`
2020-05-08 11:03:56 +00:00
- 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>
2020-04-13 15:39:40 +00:00
2020-04-18 09:07:00 +00:00
# Update
2020-04-13 15:39:40 +00:00
2020-05-08 11:03:56 +00:00
Manual image update:
- `docker-compose pull`</br>
- `docker-compose up -d`</br>
- `docker image prune`
2020-04-13 15:39:40 +00:00
2020-04-18 09:07:00 +00:00
# Backup and restore
2020-04-13 15:39:40 +00:00
2020-05-08 11:03:56 +00:00
#### Backup
2023-02-18 09:11:26 +00:00
Using [kopia](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/kopia_backup)
or [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup)
to make daily snapshot of the entire docker directory.
2020-05-08 11:03:56 +00:00
#### Restore
2023-02-18 09:11:26 +00:00
* down the containers `docker-compose down`</br>
* delete/move/rename the entire project directory</br>
* from the backups copy back the entire project directory</br>
2020-05-08 11:03:56 +00:00
* start the containers `docker-compose up -d`
2020-04-13 15:39:40 +00:00
2020-04-18 09:07:00 +00:00
# Backup of just user data
2020-04-13 15:39:40 +00:00
2023-02-18 09:11:26 +00:00
User's data daily export going by the
2020-05-08 11:03:56 +00:00
[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>
2020-04-16 22:05:54 +00:00
2023-02-18 09:11:26 +00:00
Daily kopia/borg backup run takes care of backing up the directories.
So only database dump is needed and done with the script.</br>
2020-04-13 15:39:40 +00:00
2020-04-25 23:31:16 +00:00
#### Create a backup script
2023-02-18 09:11:26 +00:00
Placed inside `nextcloud` directory on the host.
2020-04-25 23:31:16 +00:00
`nextcloud-backup-script.sh`
```bash
#!/bin/bash
2020-04-13 15:39:40 +00:00
2020-04-25 23:31:16 +00:00
# MAINTENANCE MODE ON
2020-05-08 11:03:56 +00:00
docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --on
2020-04-13 15:39:40 +00:00
2020-04-25 23:31:16 +00:00
# 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'
2020-04-13 15:39:40 +00:00
2020-04-25 23:31:16 +00:00
# MAINTENANCE MODE OFF
2020-05-08 11:03:56 +00:00
docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --off
2020-04-25 23:31:16 +00:00
```
2020-04-13 15:39:40 +00:00
2020-05-09 11:26:29 +00:00
The script must be **executable** - `chmod +x nextcloud-backup-script.sh`
2020-05-09 11:47:07 +00:00
Test run the script `sudo ./nextcloud-backup-script.sh`</br>
2020-05-08 11:03:56 +00:00
The resulting database dump is in
2023-02-17 21:52:55 +00:00
`nextcloud/nextcloud_data_db/BACKUP.nextcloud.database.sql`
2020-05-08 11:03:56 +00:00
#### Cronjob
Running on the host, so that the script will be periodically run.
2020-04-13 15:39:40 +00:00
2020-05-08 11:03:56 +00:00
* `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
2020-04-13 15:39:40 +00:00
2020-04-18 09:07:00 +00:00
# Restore the user data
2020-04-13 15:39:40 +00:00
2023-02-18 09:11:26 +00:00
[The official docs.](https://docs.nextcloud.com/server/latest/admin_manual/maintenance/restore.html)
2020-05-08 11:03:56 +00:00
Assuming clean start.
2020-04-25 23:31:16 +00:00
* start the containers: `docker-compose up -d`</br>
2020-05-09 11:33:24 +00:00
let them run so they create the file structure
2020-05-08 11:03:56 +00:00
* down the containers: `docker-compose down`
2020-07-20 23:19:33 +00:00
* delete the directories `config`, `data`, `themes` in the freshly created
2023-02-17 21:52:55 +00:00
`nextcloud/nextcloud_data/`
2023-02-18 09:11:26 +00:00
* from the backup of `/nextcloud/nextcloud_data/`, copy the directories
`configs`, `data`, `themes` in to the new `/nextcloud/nextcloud_data/`
2023-02-17 21:52:55 +00:00
* from the backup of `/nextcloud/nextcloud_data_db/`, copy the backup database
named `BACKUP.nextcloud.database.sql` in to the new `/nextcloud/nextcloud_data_db/`
2020-04-25 23:31:16 +00:00
* start the containers: `docker-compose up -d`
* set the correct user ownership of the directories copied:</br>
2020-05-08 11:03:56 +00:00
`docker exec --workdir /var/www/html nextcloud-app chown -R www-data:www-data config data themes`
2020-04-25 23:31:16 +00:00
* 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>
2020-05-08 11:03:56 +00:00
`docker container exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:mode --off`
2020-04-25 23:31:16 +00:00
* update the systems data-fingerprint:</br>
2020-05-08 11:03:56 +00:00
`docker exec --user www-data --workdir /var/www/html nextcloud-app php occ maintenance:data-fingerprint`
2020-04-25 23:31:16 +00:00
* restart the containers: `docker-compose restart`
* log in