# Synapse Matrix Server mit Docker
https://lstu.stemy.me/gpn20mxs
## Vorbereitung
### Domains
Im Beispiel werden die folgenden zwei Domains verwendet:
- `chaos.boutique`
- `matrix.chaos.boutique`
- Optional: `chat.chaos.boutique`
`chaos.boutique` ist der Teil, der später auch in den Benutzernamen etc. vorkommt.
Zum Beispiel `@jutta:chaos.boutique`.
### Server
- Beispiel: Debian 11
- [Docker (Anleitung)](https://docs.docker.com/engine/install/)
## Docker Netzwerk
```
docker network create matrix
```
## Datenbank
```
docker run -d \
--name postgres \
--network matrix \
-e POSTGRES_PASSWORD=j93l521xEZjKgO \
-e POSTGRES_INITDB_ARGS='--encoding=UTF-8 --lc-collate=C --lc-ctype=C' \
postgres
```
## Synapse
Erstmal die Konfig erstellen
```
docker run -it --rm \
--mount type=volume,src=synapse-data,dst=/data \
-e SYNAPSE_SERVER_NAME=chaos.boutique \
-e SYNAPSE_REPORT_STATS=no \
matrixdotorg/synapse:latest generate
ls /var/lib/docker/volumes/synapse-data/_data/
vi /var/lib/docker/volumes/synapse-data/_data/homeserver.yaml
```
DB konfigurieren
```
database:
name: psycopg2
args:
user: postgres
password: j93l521xEZjKgO
database: postgres
host: postgres
```
```
docker run -d --name synapse \
--mount type=volume,src=synapse-data,dst=/data \
--network matrix \
-p 8008:8008 \
matrixdotorg/synapse:latest
```
Mal in die Logs gucken:
```
docker logs synapse
```
## Zertifikate besorgen
```
apt install \
certbot \
python3-certbot-nginx
certbot certonly \
--standalone \
-d matrix.chaos.boutique \
-d chat.chaos.boutique \
-d chaos.boutique
```
## NGINX
```
apt install nginx
```
https://matrix-org.github.io/synapse/latest/reverse_proxy.html
`/etc/nginx/sites-enabled/chaos.boutique`
```
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name chaos.boutique;
ssl_certificate /etc/letsencrypt/live/matrix.chaos.boutique/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.chaos.boutique/privkey.pem;
set $CWK '';
set $CWK '${CWK}{';
set $CWK '${CWK} "m.homeserver": {';
set $CWK '${CWK} "base_url": "https://matrix.chaos.boutique"';
set $CWK '${CWK} }';
set $CWK '${CWK}}';
set $SWK '';
set $SWK '${SWK}{';
set $SWK '${SWK} "m.server": "matrix.chaos.boutique:443"';
set $SWK '${SWK}}';
location /.well-known/matrix/client {
return 200 "$CWK";
default_type application/json;
add_header Access-Control-Allow-Origin *;
}
location /.well-known/matrix/server {
return 200 "$SWK";
default_type application/json;
add_header Access-Control-Allow-Origin *;
}
}
```
`/etc/nginx/sites-enabled/matrix.chaos.boutique`
```
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
# For the federation port
listen 8448 ssl http2 default_server;
listen [::]:8448 ssl http2 default_server;
server_name matrix.chaos.boutique;
ssl_certificate /etc/letsencrypt/live/matrix.chaos.boutique/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.chaos.boutique/privkey.pem;
location ~ ^(/_matrix|/_synapse/client) {
# note: do not add a path (even a single /) after the port in `proxy_pass`,
# otherwise nginx will canonicalise the URI and cause signature verification
# errors.
proxy_pass http://localhost:8008;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
# Nginx by default only allows file uploads up to 1M in size
# Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
client_max_body_size 50M;
}
}
```
Test:
- https://matrix.chaos.boutique/_matrix/federation/v1/version
- https://chaos.boutique/.well-known/matrix/client
- https://chaos.boutique/.well-known/matrix/server
- https://federationtester.matrix.org/
### Benutzer anlegen
(nur falls Registrierung deaktiviert ist)
```
docker exec -it synapse \
register_new_matrix_user \
http://localhost:8008 \
-c /data/homeserver.yaml
```
## Element
```
mkdir -p /etc/element
curl -o /etc/element/config.json https://raw.githubusercontent.com/vector-im/element-web/develop/config.sample.json
vi /etc/element/config.json
docker run -d --name element \
-v /etc/element/config.json:/app/config.json \
-p 8080:80 \
vectorim/element-web
```
### Reverse Proxy
`/etc/nginx/sites-enabled/chat.chaos.boutique`
```
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name chat.chaos.boutique;
ssl_certificate /etc/letsencrypt/live/matrix.chaos.boutique/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.chaos.boutique/privkey.pem;
location / {
proxy_pass http://localhost:8080;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "frame-ancestors 'none'";
}
}
```
Testen: https://chat.chaos.boutique/
## TURN
Kein Docker; macht Probleme
Coturn installieren
```
apt install coturn
vi /etc/default/coturn
```
Hier `TURNSERVER_ENABLED=1` setzen.
```
vi /etc/turnserver.conf
```
Hier z.B.
- `use-auth-secret` einkommentieren
- Und `static-auth-secret` setzen
```
systemctl start coturn
systemctl status coturn
```
Synapse Konfig anpassen
```
vi /var/lib/docker/volumes/synapse-data/_data/homeserver.yaml
```
Nach `turn_uris` suchen.
Beispiel
```
turn_uris:
- turn:chaos.boutique:3478?transport=udp
- turn:chaos.boutique:3478?transport=tcp
turn_shared_secret: geheim
```
Synapse neu starten
```
docker restart synapse
```
Einen 1:1 Anruf ausprobieren (aus zwei Netzwerken).
## Backup
Strategie:
- Server muss nicht gestoppt werden
- Datenbank-Backup starten
- Läuft in einer Transaktion; ist also in sich konsistent
- Danach das komplette Synapse-Datenverzeichnis
Restore:
- Synapse installieren; nicht starten
- Oder stoppen und DB + Daten komplett durch das Backup ersetzen
- Datenbank-Dump einspielen
- Synapse-Datenverzeichnis wiederherstellen
- Synapse starten
Beispiel Skript:
```
PGPASSWORD="geheim" pg_dump \
--host=postgres \
--dbname=postgres \
--username=postgres \
> /backup/db.sql
# rsync
# borg create \
# ssh:// \
# /backup /synapse
```
Ausführen mit (Cron oder Timer)
```
docker run --rm \
--network matrix \
--mount type=volume,src=synapse-data,dst=/data,readonly \
-v ./backup.sh:/backup.sh \
mweimann/borg-backup
/backup.sh
```