What you will set up
Matrix is an open chat network. Like email, anyone can run a server, and people on different servers talk to each other as if they were on the same one. Here, your server is two Compose services under a user of its own called matrix:
- Synapse, the reference Matrix server,
- PostgreSQL, its database.
Caddy, set up in the Docker guide, handles HTTPS in front of it. Addresses look like @anna:example.com, while the server itself runs at matrix.example.com, so example.com can stay your website.
The app-specific results below come from the tested Podman guide on a Melonslab server with Debian 13. Matrix's federation tester passed over IPv4 and IPv6, a user on the test server joined a public room on matrix.org and received its messages, and the server came back after a reboot. Through a test SMTP server on port 587 with STARTTLS, Synapse sent the link that confirms an added address, a password reset that worked, and an email about a missed message. A backup was restored into empty volumes, with the account, its messages and an uploaded file intact. With that room joined, Synapse and PostgreSQL used under 200 MB of memory. Voice and video calls need more, set up in the calls guide.
The Docker version below follows the tested rootless Docker base guide. The app-specific results above come from the tested Podman guide on a Melonslab server; they were not measured under Docker.
Before you start
You need:
- a server set up as in the Docker guide, with Caddy running;
- a domain, such as
example.com, with an A record and an AAAA record formatrix.example.compointing at your server; - a way to publish two small files on
https://example.com, either from this server's Caddy or from wherever your website runs (step 6).
The examples use example.com. Replace it with your own throughout.
1. Create the user
As root:
useradd -m -s /bin/bash matrix
loginctl enable-linger matrix
uid=$(id -u matrix)
Everything up to step 6 runs as matrix.
2. Generate Synapse's configuration
docker run --rm -v /home/matrix/matrix/synapse-data:/data \
-e SYNAPSE_SERVER_NAME=example.com -e SYNAPSE_REPORT_STATS=no \
docker.io/matrixdotorg/synapse:latest generate
This creates the synapse-data directory, holding Synapse's configuration, its secrets and the server's signing key, which is its identity on the Matrix network. The server name becomes part of every address and cannot be changed later, so choose it carefully.
3. Create the database password
The passwords are generated with the Compose file in the next step and kept in a 0600 file under the app directory.
The database password goes into PostgreSQL's 0600 env file, and the database section of Synapse's configuration into a separate 0600 YAML file that Compose mounts as a secret. The password is not written into the Compose file.
4. Define the Compose project
Create a Compose file in the app's directory. It uses the same images, ports, volumes, environment and restart policy as the Podman guide:
mkdir -p /home/matrix/matrix
cat > /home/matrix/matrix/compose.yaml <<'YAML'
services:
db:
image: docker.io/library/postgres:18-alpine
volumes:
- db:/var/lib/postgresql
environment:
POSTGRES_USER: synapse
POSTGRES_DB: synapse
POSTGRES_INITDB_ARGS: --encoding=UTF-8 --lc-collate=C --lc-ctype=C
env_file: matrix.env
restart: always
synapse:
image: docker.io/matrixdotorg/synapse:latest
command: run --config-path /data/homeserver.yaml --config-path /config/synapse.yaml --config-path /config/database.yaml
ports:
- "127.0.0.1:8082:8008"
volumes:
- /home/matrix/matrix/synapse-data:/data
- /home/matrix/matrix/synapse.yaml:/config/synapse.yaml:ro
secrets:
- source: database
target: /config/database.yaml
restart: always
secrets:
database:
file: ./database.yaml
volumes:
db:
YAML
umask 077
pw=$(openssl rand -hex 24)
printf 'POSTGRES_PASSWORD=%s\n' "$pw" > /home/matrix/matrix/matrix.env
printf 'database:\n name: psycopg2\n args:\n user: synapse\n password: "%s"\n dbname: synapse\n host: 127.0.0.1\n' "$pw" > /home/matrix/matrix/database.yaml
unset pw
chmod 600 /home/matrix/matrix/matrix.env /home/matrix/matrix/database.yaml
chown -R matrix:matrix /home/matrix/matrix
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash -lc "cd /home/matrix/matrix && docker compose up -d"
Use the weekly docker-update.service/docker-update.timer from the rootless Docker guide instead of Podman's auto-update timer. It runs docker compose pull and docker compose up -d in the app directory each Sunday. Set it up once for matrix as shown there; it covers every Compose app in that user's home.
When the log says Synapse now listening on TCP port 8008, press Ctrl+C. A warning just before it, that Synapse failed to listen on 0.0.0.0, is harmless: it listens on [::], which covers IPv4 too.
6. Put Caddy in front
Go back to root with exit, and switch to Caddy's user:
runuser -u caddy -- /bin/bash
Add this block to the end of ~/Caddyfile:
matrix.example.com {
reverse_proxy /_matrix/* 127.0.0.1:8082
reverse_proxy /_synapse/client/* 127.0.0.1:8082
}
Only the paths Matrix apps and other servers use are passed on, so Synapse's admin interface stays reachable from the server only.
Then the two files on https://example.com. They tell other servers and apps that the Matrix server for example.com is matrix.example.com. If example.com points at this server, add:
example.com {
header /.well-known/matrix/* Content-Type application/json
header /.well-known/matrix/* Access-Control-Allow-Origin *
respond /.well-known/matrix/server `{"m.server": "matrix.example.com:443"}`
respond /.well-known/matrix/client `{"m.homeserver": {"base_url": "https://matrix.example.com"}}`
}
If example.com already has a block in the Caddyfile, put the four lines inside that block instead: Caddy does not accept two blocks for the same address. If your website runs somewhere else, publish the same two files there, at /.well-known/matrix/server and /.well-known/matrix/client, with the same two headers.
Restart Caddy, which briefly interrupts every site it serves:
runuser -u caddy -- env HOME=/home/caddy XDG_RUNTIME_DIR=/run/user/$(id -u caddy) DOCKER_HOST=unix:///run/user/$(id -u caddy)/docker.sock PATH=/usr/bin:/bin bash -lc 'cd /home/caddy/caddy && docker compose restart caddy'
7. Create your account
Go back to root with exit, then run as matrix with the Compose commands from step 1:
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash <<'SH'
cd /home/matrix/matrix
docker compose exec synapse register_new_matrix_user -c /data/homeserver.yaml http://localhost:8008
SH
It asks for a user name and password, and whether the account is an admin. Nobody can sign up on the server by themselves, so create accounts for others the same way.
Log in with a Matrix app, such as Element from element.io. Element's sign-in page shows matrix.org as the server: choose Edit next to it, enter example.com under Other account provider and choose Continue. The app finds matrix.example.com by itself. In Element, private chats are end-to-end encrypted by default.
8. Email
Synapse sends email to confirm an address a user adds to their account, for password resets, and about messages a user has missed. Without email, adding an address in the app's settings fails with "Adding an email to your account is disabled on this server", and resetting a forgotten password fails with "Email-based password resets have been disabled on this server". Accounts, chat and federation work as before.
Send through your email provider's SMTP server. Like the database settings, the email settings go into a secret that Synapse reads as a file, so the password is not in any file. As matrix, with your own server, account and sender address:
read -rsp 'SMTP password: ' pw; echo
printf 'email:\n smtp_host: smtp.example.com\n smtp_port: 587\n require_transport_security: true\n smtp_user: matrix@example.com\n smtp_pass: "%s"\n notif_from: "Matrix <matrix@example.com>"\n enable_notifs: true\n' "$pw" > /home/matrix/matrix/email.yaml
chmod 600 /home/matrix/matrix/email.yaml
unset pw
The command asks for the SMTP password without showing it. If the password contains " or \, type a \ in front of each. Port 587 with require_transport_security uses STARTTLS, and Synapse refuses to send without it. For port 465, which uses TLS from the start, change the port and replace require_transport_security: true with force_tls: true.
Add a second secret to compose.yaml, under synapse.secrets:
- source: email
target: /config/email.yaml
and at the top level:
email:
file: ./email.yaml
Add --config-path /config/email.yaml to the end of synapse.command. Then restart Synapse:
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash -lc "cd /home/matrix/matrix && docker compose up -d --force-recreate synapse"
Each user adds their address in the app's settings and confirms it with the link Synapse sends. Then Forgot password? on the sign-in page works for them. Email about missed messages is sent once a user switches on email notifications in the app, about 10 minutes after a message they have not read.
9. Check federation
Open https://federationtester.matrix.org, enter example.com and check that it reports success. Then, from your app, join a public room on another server. The first join of a large room can take a few minutes, as your server fetches the room's history.
10. Keep it up to date
Use the weekly docker-update.service/docker-update.timer from the rootless Docker guide for this user; it covers every Compose app in the user's home.
11. Back up
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash <<'SH'
cd /home/matrix/matrix
umask 077
mkdir -p ~/backup
docker compose exec -T db pg_dump -U synapse synapse > ~/backup/synapse-db.sql
tar -cf ~/backup/matrix-data.tar -C /home/matrix/matrix synapse-data
SH
This saves the database, and the configuration, signing key and uploaded files in one archive. umask 077 makes new files readable by matrix only. Copy ~/backup to another machine afterwards. Keep it private: the signing key lets anyone who has it speak as your server.
To restore on a new server, do steps 1, 3 and 4, and step 8 if you use email, but not step 2: the archive holds the configuration and the signing key. Then, as matrix, with the backup in ~/backup:
mkdir -p /home/matrix/matrix/synapse-data
tar -xf ~/backup/matrix-data.tar -C /home/matrix/matrix
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash -lc "cd /home/matrix/matrix && docker compose up -d db"
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash -lc "cd /home/matrix/matrix && docker compose stop synapse"
until docker compose exec -T db pg_isready -q -h 127.0.0.1; do sleep 2; done
docker compose exec -T db dropdb -U synapse synapse
docker compose exec -T db createdb -U synapse -E UTF8 --lc-collate=C --lc-ctype=C -T template0 synapse
docker compose exec -T db psql -q -U synapse -d synapse < ~/backup/synapse-db.sql > /dev/null
uid=$(id -u matrix)
runuser -u matrix -- env HOME=/home/matrix XDG_RUNTIME_DIR=/run/user/$uid \
DOCKER_HOST=unix:///run/user/$uid/docker.sock PATH=/usr/bin:/bin \
bash -lc "cd /home/matrix/matrix && docker compose up -d synapse"
Starting the database starts the whole pod, and Synapse fills the new, empty database straight away, so the lines stop Synapse, wait until PostgreSQL is ready, and replace the database with an empty one with the C collation before loading the backup. Then go on with step 6. In our test, the account, its rooms and messages, and an uploaded file were all back.
Troubleshooting
The federation tester reports a problem with .well-known. Open https://example.com/.well-known/matrix/server in a browser; it should show the JSON from step 6.
Synapse stops at start with a message about the database collation. The database was created without the C collation, and POSTGRES_INITDB_ARGS only takes effect on an empty volume. On a new server, stop the pod, delete the database with docker compose down -v and start again.
Search does not find a word with å, ä or ö typed in another case. The C collation that Synapse requires makes PostgreSQL's search ignore case only for the letters A to Z. A search for älvsjö does not find a message with Älvsjö, while stockholm finds Stockholm. Search with the case used in the message. This is server-side search, which covers rooms that are not end-to-end encrypted.
Synapse says its config file does not exist. Step 2 has not been run as this user, or the directory in step 2 and the bind mount in compose.yaml have different paths.
Calls, and more
Voice and video calls need a call service of their own. The calls guide adds one to this server, for Element's apps and for older ones. Hermes Agent can join it as an AI assistant you chat with.
With your Matrix server on a Melonslab server, your accounts, rooms and messages are stored in Sweden, under Swedish and EU law, on a server you control.