Deploy Matrix Synapse and Element Web on Docker: Self-Hosted E2EE Chat for HomeLab

HomeLab tutorial - IT technology blog
HomeLab tutorial - IT technology blog

Every alert, family photo, and late-night project thread you send through Discord or Telegram sits on someone else’s servers. Running your own Matrix homeserver with Synapse and Element Web puts that metadata and message history back on hardware you own.

You still get threads, voice calls, file sharing, and mobile push notifications—just with end-to-end encryption (E2EE) and zero vendor lock-in. I’ve run this exact stack on an Intel N100 mini PC with 8 GB of RAM for over a year, serving 12 active users across 30+ rooms without a hiccup.

Quick Start: Get Matrix Synapse Running in 5 Minutes

Got Docker and Docker Compose installed on a Linux host? You can have Synapse and Element Web talking to each other in under five minutes. First, create a project folder and generate your initial Synapse config and signing key.

mkdir -p ~/matrix/{synapse,element,postgres}
cd ~/matrix

docker run -it --rm \
  -v $(pwd)/synapse:/data \
  -e SYNAPSE_SERVER_NAME=matrix.yourdomain.com \
  -e SYNAPSE_REPORT_STATS=no \
  matrixdotorg/synapse:latest generate

That single command drops three things into ~/matrix/synapse: homeserver.yaml, a logging config, and your cryptographic signing key. Next, point Element Web at your own domain instead of the public matrix.org server by creating element/config.json.

cat <<'EOF' > element/config.json
{
  "default_server_config": {
    "m.homeserver": {
      "base_url": "https://matrix.yourdomain.com",
      "server_name": "matrix.yourdomain.com"
    }
  },
  "disable_custom_urls": false,
  "default_theme": "dark"
}
EOF

With both configs ready, wire Synapse and Element Web together in docker-compose.yml:

services:
  synapse:
    image: matrixdotorg/synapse:latest
    container_name: matrix-synapse
    restart: unless-stopped
    volumes:
      - ./synapse:/data
    ports:
      - "8008:8008"
    environment:
      - SYNAPSE_CONFIG_PATH=/data/homeserver.yaml

  element:
    image: vectorim/element-web:latest
    container_name: matrix-element
    restart: unless-stopped
    volumes:
      - ./element/config.json:/app/config.json:ro
    ports:
      - "8080:80"

Spin up the containers and create your first admin user from the terminal:

docker compose up -d
docker exec -it matrix-synapse register_new_matrix_user -c /data/homeserver.yaml http://localhost:8008

Pick a username, enter a strong password, and type yes when asked about admin privileges. Your local homeserver is now live on port 8008, and Element Web is listening on port 8080.

Deep Dive: PostgreSQL, Reverse Proxy, and Federation

Synapse ships with SQLite enabled by default. That’s fine for a 10-minute test, but SQLite starts throwing database lock errors as soon as you join three or four busy federated rooms. Switch to PostgreSQL before inviting anyone else—migrating a populated SQLite database later is a headache you don’t need.

Switching Synapse from SQLite to PostgreSQL

Drop a postgres service into docker-compose.yml. Pay close attention to POSTGRES_INITDB_ARGS: Synapse refuses to start unless the database uses UTF8 encoding and the C locale for deterministic room sorting.

  postgres:
    image: postgres:16-alpine
    container_name: matrix-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=synapse
      - POSTGRES_PASSWORD=StrongRandomPasswordHere
      - POSTGRES_DB=synapse
      - POSTGRES_INITDB_ARGS=--encoding=UTF8 --lc-collate=C --lc-ctype=C
    volumes:
      - ./postgres:/var/lib/postgresql/data

Open synapse/homeserver.yaml and swap out the default SQLite database: section for your PostgreSQL credentials:

database:
  name: psycopg2
  args:
    user: synapse
    password: StrongRandomPasswordHere
    database: synapse
    host: postgres
    cp_min: 5
    cp_max: 10

Add depends_on: [postgres] under the synapse service in docker-compose.yml so Postgres boots first. If you’re starting fresh after testing SQLite, delete ./synapse/homeserver.db before running docker compose up -d.

Configuring Reverse Proxy and Well-Known Delegation

Federation traffic defaults to port 8448 unless you tell other servers otherwise. Serving two tiny JSON endpoints at /.well-known/matrix/ lets your existing Nginx, Caddy, or Traefik proxy handle everything cleanly over standard HTTPS port 443.

server {
    listen 443 ssl http2;
    server_name matrix.yourdomain.com;

    location ~ ^(/_matrix|/_synapse/client) {
        proxy_pass http://127.0.0.1:8008;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        client_max_body_size 50M;
    }

    location /.well-known/matrix/server {
        default_type application/json;
        return 200 '{"m.server": "matrix.yourdomain.com:443"}';
    }

    location /.well-known/matrix/client {
        default_type application/json;
        add_header Access-Control-Allow-Origin *;
        return 200 '{"m.homeserver": {"base_url": "https://matrix.yourdomain.com"}}';
    }
}

Route a second subdomain like chat.yourdomain.com to port 8080 for Element Web. Match client_max_body_size with max_upload_size in homeserver.yaml—50 MB handles screenshots, log bundles, and short screen recordings without bogging down your disk.

Advanced Usage: Voice Calls, Bridges, and Resource Tuning

Text and E2EE are only half the story. With a few extra containers, your HomeLab server can replace voice channels and pull in messages from friends who refuse to leave other platforms.

Adding Coturn for WebRTC Voice and Video

Direct peer-to-peer WebRTC calls frequently stall behind HomeLab NATs or strict carrier firewalls. Running a lightweight coturn container on ports 3478 (TCP/UDP) and a small UDP relay range (such as 49152-49252) solves that. Generate a 64-character secret with openssl rand -hex 32, then add it to both turnserver.conf and homeserver.yaml:

turn_uris:
  - "turn:turn.yourdomain.com:3478?transport=udp"
  - "turn:turn.yourdomain.com:3478?transport=tcp"
turn_shared_secret: "YOUR_GENERATED_HEX_SECRET"
turn_user_lifetime: 1h
turn_allow_guests: true

Bridging Telegram or Discord into Your HomeLab

You don’t need to convince your entire group chat to install Element on day one. Bridges like mautrix-telegram and mautrix-discord run alongside Synapse as Docker containers using roughly 80–150 MB of RAM each. Drop each bridge’s generated registration.yaml into ./synapse/, list it under app_service_config_files in homeserver.yaml, and reply to external chats right inside Element.

Memory Tuning on Low-Power Hosts

Synapse caches room state aggressively in RAM. With the default cache factor of 1.0, joining a few large public rooms can push memory past 1.2 GB. On a 4 GB Raspberry Pi or 8 GB mini PC, add SYNAPSE_CACHE_FACTOR=0.4 to Synapse’s environment in docker-compose.yml. Memory usage stays around 450–650 MB, and message delivery still feels instant for a small household or team.

Practical Tips from Running Matrix in a HomeLab

Five habits make the difference between a rock-solid homeserver and one that breaks after an update:

  • Lock down open registration immediately: Confirm enable_registration: false is set in homeserver.yaml. When friends want to join, issue single-use registration tokens via the Synapse Admin API instead of inviting spam bots.
  • Set up E2EE key backup on day one: Enable Secure Backup in Element Web and save the recovery key in your password manager. Encryption keys stay on your devices—clearing browser storage without a backup or second signed-in session permanently locks you out of older encrypted messages.
  • Automate media cache pruning: Federated rooms cache remote avatars, stickers, and attachments in ./synapse/media_store, easily eating 20–40 GB over a few months. Run a monthly cron job calling POST /_synapse/admin/v1/purge_media_cache?before_ts=<epoch_ms> with your admin token to purge remote files older than 30 days.
  • Back up the signing key alongside PostgreSQL: That *.signing.key file in ./synapse/ is your server’s cryptographic identity. Always pair it with a nightly pg_dump of the synapse database so a dead SSD never forces you to rebuild your domain identity.
  • Test federation early: Run your domain through the official Matrix Federation Tester right after configuring Nginx or Caddy. Catching a missing Access-Control-Allow-Origin header or broken TLS chain takes 30 seconds there instead of an hour of tailing logs.
Share: