写在前面

本文档记录了在Ubuntu 22.04环境下,利用Docker部署Matrix服务的过程。由于某些工具可能在此之前已经安装过,本指南可能不包含所有步骤。请根据实际情况调整。

参考链接(建议别参考):

支持端到端加密!打造自己专属的去中心化即时聊天室!|自由畅聊,我用 Matrix!匿名、安全、功能丰富 | 我不是咕咕鸽 (laoda.de)

Installing Matrix Server - Synapse with Docker (and Cloudflare). | FOSS Engineer

https://matrix-org.github.io/synapse


安装前准备

确保Docker已正确安装

在开始之前,请确保您的系统已安装Docker。可以通过运行docker --version来验证Docker是否已正确安装。

创建安装目录

首先,在用户的home目录下创建一个名为matrix的文件夹:

1
mkdir ~/matrix

拉取Docker镜像并创建配置目录

执行以下命令以拉取Matrix的Docker镜像并生成初始配置文件:

1
2
3
4
5
sudo docker run -it --rm \
-v /home/ubuntu/matrix/data:/data \
-e SYNAPSE_SERVER_NAME=yourdomain.com \
-e SYNAPSE_REPORT_STATS=yes \
matrixdotorg/synapse:latest generate

注意

  • 确保挂载卷的宿主机路径(/home/ubuntu/matrix)与前面创建的路径~/matrix一致。
  • yourdomain.com替换为您计划使用的服务器域名。

配置homeserver.yaml

生成配置文件后,检查并修改homeserver.yaml以满足您的需求。特别是,如果您希望启用用户注册,请添加以下配置:

1
2
enable_registration: true
enable_registration_without_verification: true

创建docker-compose.yml

~/matrix目录下创建docker-compose.yml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: "3.3"

services:
synapse:
image: "matrixdotorg/synapse:latest"
container_name: "matrix_synapse"
ports:
- 9999:8008
volumes:
- "./data:/data"
environment:
VIRTUAL_HOST: "yourdomain.com"
VIRTUAL_PORT: 8008
LETSENCRYPT_HOST: "yourdomain.com"
SYNAPSE_SERVER_NAME: "yourdomain.com"
SYNAPSE_REPORT_STATS: "yes"

注意:请替换yourdomain.com为您的实际域名。


反向代理 配置证书 联邦认证

服务器防火墙设置

根据需要,放行9999端口(或您配置的其他端口),以及8448端口以支持Matrix的联邦特性。

反向代理和SSL证书配置

配置Nginx作为反向代理,并为您的Matrix服务配置SSL证书。确保已正确配置8448端口,以便支持Matrix联邦。

打开nginx.conf,进行下面的设置

有关配置需参照:
有关联邦的说明 - https://matrix-org.github.io/synapse/latest/federate.html
有关配置反代的说明 - Configuring a Reverse Proxy - Synapse (matrix-org.github.io)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#################
# Matrix Server #
#################
server {
listen 80;
server_name matrix.yours.com;
rewrite ^(.*) https://$server_name:443$1 permanent;
}

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.yours.com;

charset utf-8;

ssl_certificate /ssl/cert/matrix.pem;
ssl_certificate_key /ssl/cert/matrix.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

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:9999;
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;

# Synapse responses may be chunked, which is an HTTP/1.1 feature.
proxy_http_version 1.1;
}
}

启动服务

使用以下命令启动Matrix服务:

1
docker-compose up

建议初次启动时不要使用-d选项,这样可以在控制台查看日志和潜在的错误信息。配置完成并确认一切正常后,可以使用-d选项以守护进程模式运行。

测试服务

  • 通过访问http://yourdomain.com/_matrix/static/确认服务是否正常运行。
1
2
3
4
5
6
It works! Synapse is running
Your Synapse server is listening on this port and is ready for messages.

To use this server you'll need a Matrix client.

Welcome to the Matrix universe :)
  • 使用federation tester测试您的服务器的联邦功能。
  • 尝试注册用户或使用已有的Matrix客户端连接到您的服务器进行进一步的测试。