// Matrix Chat Server

Host your own end-to-end encrypted chat server

Why Matrix?

Matrix is an open protocol for real-time communication. It provides:

What you'll build: A self-hosted Synapse server with Element web client, ready for production use.

Prerequisites

Step 1: Set Up DNS

Before anything else, point your domain at your server:

Wait 5-10 minutes for DNS to propagate.

Step 2: Install Docker

curl -fsSL https://get.docker.com | sh
systemctl enable docker
systemctl start docker

Step 3: Create Docker Compose File

mkdir -p ~/matrix
cd ~/matrix
cat > docker-compose.yml << 'EOF'
version: '3.8'

services:
  synapse:
    image: matrixdotorg/synapse:latest
    container_name: synapse
    restart: unless-stopped
    ports:
      - "8008:8008"
    volumes:
      - ./data:/data
    environment:
      - SYNAPSE_SERVER_NAME=matrix.yourdomain.com
      - SYNAPSE_REPORT_STATS=no

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

volumes:
  data:
EOF

Step 4: Generate Synapse Config

# Create config directory
mkdir -p ~/matrix/data

# Generate config (replace with your domain)
docker run -it --rm -v ~/matrix/data:/data -e SYNAPSE_SERVER_NAME=matrix.yourdomain.com matrixdotorg/synapse generate

Step 5: Set Up SSL with Certbot

# Install certbot
apt install certbot python3-certbot-nginx

# Get certificate (stop nginx first if running)
certbot certonly --standalone -d matrix.yourdomain.com

# Copy certificates
cp /etc/letsencrypt/live-matrix.yourdomain.com/fullchain.pem ~/matrix/data/
cp /etc/letsencrypt/live-matrix.yourdomain.com/privkey.pem ~/matrix/data/

Step 6: Configure Element

cat > ~/matrix/element-config.json << 'EOF'
{
  "default_server_name": "matrix.yourdomain.com",
  "default_server_url": "https://matrix.yourdomain.com"
}
EOF

Step 7: Configure Nginx Reverse Proxy

cat > /etc/nginx/sites-available/matrix << 'EOF'
server {
    listen 443 ssl http2;
    server_name matrix.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live-matrix.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live-matrix.yourdomain.com/privkey.pem;

    location / {
        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;
    }
}

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

    ssl_certificate /etc/letsencrypt/live-matrix.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live-matrix.yourdomain.com/privkey.pem;

    location / {
        proxy_pass http://localhost:8080;
    }
}
EOF

ln -s /etc/nginx/sites-available/matrix /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

Step 8: Start Everything

cd ~/matrix
docker-compose up -d
docker-compose logs -f

Step 9: Create Admin User

docker exec -it synapse register_new_matrix_user -u admin -p YourPassword -a http://localhost:8008

Step 10: Connect Your Clients

Access Element web client at https://element.yourdomain.com

Log in with your admin credentials or create new users.

For mobile, download Element from F-Droid or App Store and enter your server URL.

Bridging to Other Platforms

Matrix can bridge to other chat platforms. Here are popular bridges:

Security notes:

Next Steps

"The right to privacy is essential to the right to freedom of speech. Matrix gives you that privacy by default." — The Rebel