{"slug":"self-host-matrix","title":"QQ微信不够私密？自建自己的聊天服务器！","description":"通过自建Synapse，用户可以通过Element等软件来直接在你的服务器上聊天","coverImage":"https://b.acofork.com/img/self-host-matrix-historical-cover.webp","coverWidth":545,"coverHeight":505,"date":"2025-08-01","tags":["服务器"],"pinned":false,"wordCount":1009,"html":"<h2 id=\"前置环境准备\">前置环境准备</h2><p>由于Synapse、Matrix（下文简称”矩阵“）手搓部署非常麻烦。所以请安装 <strong>1Panel面板</strong></p>\n<h2 id=\"部署postgresql\">部署PostgreSQL</h2><p>安装并创建名为 <code>synapse</code> 用户名也为 <code>synapse</code> 的数据库</p>\n<p>前往应用商店安装 <code>PGAdmin4</code></p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-24-58-image.webp\" alt=\"\" width=\"803\" height=\"198\" loading=\"eager\" decoding=\"async\" /></p>\n<p>接着点击添加服务器</p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-27-10-image.webp\" alt=\"\" width=\"1865\" height=\"926\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>相关信息可以在连接信息看到</p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-27-53-image.webp\" alt=\"\" width=\"1865\" height=\"926\" loading=\"lazy\" decoding=\"async\" /></p>\n<p><strong>删除</strong>刚刚创建的 <code>synapse</code> 这个数据库</p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-28-49-image.webp\" alt=\"\" width=\"364\" height=\"198\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>重新创建同名数据库</p>\n<p>设置所有者（即用户名）为 <code>synapse</code> </p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-29-36-image.webp\" alt=\"\" width=\"703\" height=\"280\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>将 <code>排序规则</code> 和 <code>字符类型</code> 都改为 <code>C</code></p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-30-34-image.webp\" alt=\"\" width=\"706\" height=\"554\" loading=\"lazy\" decoding=\"async\" /></p>\n<h2 id=\"部署synapse\">部署Synapse</h2><p>首先参照1Panel官方的教程去创建一个存储卷，否则安装 <code>synapse</code> 会失败</p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-32-00-image.webp\" alt=\"\" width=\"937\" height=\"700\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>安装 <code>synapse</code> </p>\n<p>导航到文件管理： <code>/var/lib/docker/volumes/synapse-data/_data</code></p>\n<p>你应该可以看到</p>\n<p><img src=\"https://b.acofork.com/img/self-host-matrix-2025-08-02-17-33-50-image.webp\" alt=\"\" width=\"707\" height=\"454\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>编辑 <code>homeserver.yaml</code> ，并按需配置</p>\n<div class=\"markdown-code\" data-language=\"yaml\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>yaml</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-yaml\">{<span class=\"hljs-string\">%</span> <span class=\"hljs-string\">raw</span> <span class=\"hljs-string\">%</span>}\n<span class=\"hljs-attr\">server_name:</span> <span class=\"hljs-string\">\"家服务器名称，比如：m.2x.nz\"</span>\n<span class=\"hljs-attr\">public_baseurl:</span> <span class=\"hljs-string\">\"公共URL，比如：https://m.2x.nz\"</span>\n<span class=\"hljs-attr\">pid_file:</span> <span class=\"hljs-string\">/data/homeserver.pid</span>\n\n<span class=\"hljs-attr\">serve_server_wellknown:</span> <span class=\"hljs-literal\">true</span> <span class=\"hljs-comment\"># 启用联邦</span>\n\n<span class=\"hljs-attr\">listeners:</span>\n  <span class=\"hljs-bullet\">-</span> <span class=\"hljs-attr\">port:</span> <span class=\"hljs-number\">8008</span>\n    <span class=\"hljs-attr\">tls:</span> <span class=\"hljs-literal\">false</span>\n    <span class=\"hljs-attr\">type:</span> <span class=\"hljs-string\">http</span>\n    <span class=\"hljs-attr\">x_forwarded:</span> <span class=\"hljs-literal\">true</span>\n    <span class=\"hljs-attr\">resources:</span>\n      <span class=\"hljs-bullet\">-</span> <span class=\"hljs-attr\">names:</span> [<span class=\"hljs-string\">client</span>, <span class=\"hljs-string\">federation</span>]\n        <span class=\"hljs-attr\">compress:</span> <span class=\"hljs-literal\">false</span>\n    <span class=\"hljs-attr\">response_headers:</span>\n      <span class=\"hljs-attr\">Access-Control-Allow-Origin:</span> <span class=\"hljs-string\">\"https://app.element.io\"</span>\n      <span class=\"hljs-attr\">Access-Control-Allow-Methods:</span> <span class=\"hljs-string\">\"GET, POST, OPTIONS\"</span>\n      <span class=\"hljs-attr\">Access-Control-Allow-Headers:</span> <span class=\"hljs-string\">\"Content-Type, Authorization\"</span>\n\n<span class=\"hljs-attr\">database:</span>\n  <span class=\"hljs-attr\">name:</span> <span class=\"hljs-string\">psycopg2</span>\n  <span class=\"hljs-attr\">args:</span>\n    <span class=\"hljs-attr\">user:</span> <span class=\"hljs-string\">synapse</span>\n    <span class=\"hljs-attr\">password:</span> <span class=\"hljs-string\">你的数据库密码</span>\n    <span class=\"hljs-attr\">dbname:</span> <span class=\"hljs-string\">synapse</span>\n    <span class=\"hljs-attr\">host:</span> <span class=\"hljs-string\">你的PostgreSQL的容器名称</span>\n    <span class=\"hljs-attr\">cp_min:</span> <span class=\"hljs-number\">5</span>\n    <span class=\"hljs-attr\">cp_max:</span> <span class=\"hljs-number\">10</span>\n\n<span class=\"hljs-attr\">log_config:</span> <span class=\"hljs-string\">\"/data/my.matrix.host.log.config\"</span>\n<span class=\"hljs-attr\">media_store_path:</span> <span class=\"hljs-string\">/data/media_store</span>\n\n<span class=\"hljs-attr\">registration_shared_secret:</span> <span class=\"hljs-string\">\"这里的东西是随机生成的，每个人都不一样，请忽略这一块，并使用你的配置\"</span>\n<span class=\"hljs-attr\">macaroon_secret_key:</span> <span class=\"hljs-string\">\"这里的东西是随机生成的，每个人都不一样，请忽略这一块，并使用你的配置\"</span>\n<span class=\"hljs-attr\">form_secret:</span> <span class=\"hljs-string\">\"这里的东西是随机生成的，每个人都不一样，请忽略这一块，并使用你的配置\"</span>\n<span class=\"hljs-attr\">signing_key_path:</span> <span class=\"hljs-string\">\"/data/my.matrix.host.signing.key\"</span>\n\n<span class=\"hljs-attr\">report_stats:</span> <span class=\"hljs-literal\">false</span>\n\n<span class=\"hljs-attr\">trusted_key_servers:</span>\n  <span class=\"hljs-bullet\">-</span> <span class=\"hljs-attr\">server_name:</span> <span class=\"hljs-string\">\"matrix.org\"</span>\n\n<span class=\"hljs-comment\"># Github OAuth</span>\n<span class=\"hljs-attr\">oidc_providers:</span>\n  <span class=\"hljs-bullet\">-</span> <span class=\"hljs-attr\">idp_id:</span> <span class=\"hljs-string\">github</span>\n    <span class=\"hljs-attr\">idp_name:</span> <span class=\"hljs-string\">Github</span>\n    <span class=\"hljs-attr\">idp_brand:</span> <span class=\"hljs-string\">\"github\"</span>  <span class=\"hljs-comment\"># optional: styling hint for clients</span>\n    <span class=\"hljs-attr\">discover:</span> <span class=\"hljs-literal\">false</span>\n    <span class=\"hljs-attr\">issuer:</span> <span class=\"hljs-string\">\"https://github.com/\"</span>\n    <span class=\"hljs-attr\">client_id:</span> <span class=\"hljs-string\">\"Ov23liaHxxYHybb0jRoZ\"</span> <span class=\"hljs-comment\"># TO BE FILLED</span>\n    <span class=\"hljs-attr\">client_secret:</span> <span class=\"hljs-string\">\"e937f214ea7c132924ab34c76d83f4b7099d696e\"</span> <span class=\"hljs-comment\"># TO BE FILLED</span>\n    <span class=\"hljs-attr\">authorization_endpoint:</span> <span class=\"hljs-string\">\"https://github.com/login/oauth/authorize\"</span>\n    <span class=\"hljs-attr\">token_endpoint:</span> <span class=\"hljs-string\">\"https://github.com/login/oauth/access_token\"</span>\n    <span class=\"hljs-attr\">userinfo_endpoint:</span> <span class=\"hljs-string\">\"https://api.github.com/user\"</span>\n    <span class=\"hljs-attr\">scopes:</span> [<span class=\"hljs-string\">\"read:user\"</span>]\n    <span class=\"hljs-attr\">user_mapping_provider:</span>\n      <span class=\"hljs-attr\">config:</span>\n        <span class=\"hljs-attr\">subject_claim:</span> <span class=\"hljs-string\">\"id\"</span>\n        <span class=\"hljs-attr\">localpart_template:</span> <span class=\"hljs-string\">\"<span class=\"hljs-template-variable\">{{ user.login }}</span>\"</span>\n        <span class=\"hljs-attr\">display_name_template:</span> <span class=\"hljs-string\">\"<span class=\"hljs-template-variable\">{{ user.name }}</span>\"</span>\n\n<span class=\"hljs-comment\">### ✅ 邮件配置（确保SMTP验证正常）</span>\n<span class=\"hljs-attr\">email:</span>\n  <span class=\"hljs-attr\">smtp_host:</span> <span class=\"hljs-string\">\"你的SMTP发件服务器\"</span>\n  <span class=\"hljs-attr\">smtp_port:</span> <span class=\"hljs-number\">465</span>\n  <span class=\"hljs-attr\">smtp_user:</span> <span class=\"hljs-string\">\"你的发件邮箱\"</span>\n  <span class=\"hljs-attr\">smtp_pass:</span> <span class=\"hljs-string\">\"你的SMTP密码\"</span>\n  <span class=\"hljs-attr\">force_tls:</span> <span class=\"hljs-literal\">true</span>\n  <span class=\"hljs-attr\">notif_from:</span> <span class=\"hljs-string\">\"Matrix &lt;你的发件邮箱&gt;\"</span>\n  <span class=\"hljs-attr\">validation_token_lifetime:</span> <span class=\"hljs-string\">\"5m\"</span>\n\n<span class=\"hljs-comment\">### ✅ 启用注册 + 邮箱验证 + 密码找回</span>\n<span class=\"hljs-attr\">enable_registration:</span> <span class=\"hljs-literal\">true</span>\n<span class=\"hljs-attr\">registrations_require_3pid:</span>\n  <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">email</span>\n<span class=\"hljs-attr\">registration_requires_token:</span> <span class=\"hljs-literal\">false</span>   <span class=\"hljs-comment\"># 确保不强制邀请码注册（默认关闭）</span>\n<span class=\"hljs-attr\">password_config:</span>\n  <span class=\"hljs-attr\">enabled:</span> <span class=\"hljs-literal\">true</span>\n\n<span class=\"hljs-comment\">### ✅ 允许邮箱登录</span>\n<span class=\"hljs-attr\">login_via_existing_session:</span>\n  <span class=\"hljs-attr\">enabled:</span> <span class=\"hljs-literal\">true</span>\n\n<span class=\"hljs-attr\">rc_registration:</span>\n  <span class=\"hljs-attr\">per_second:</span> <span class=\"hljs-number\">0.003</span>  <span class=\"hljs-comment\"># 每秒允许的注册请求（例如：0.003 ≈ 每5分钟一次）</span>\n  <span class=\"hljs-attr\">burst_count:</span> <span class=\"hljs-number\">1</span>     <span class=\"hljs-comment\"># 同一IP地址的最大注册突发数</span>\n\n  <span class=\"hljs-comment\"># 消息发送速率限制</span>\n<span class=\"hljs-attr\">rc_message:</span>\n  <span class=\"hljs-attr\">per_second:</span> <span class=\"hljs-number\">0.2</span>    <span class=\"hljs-comment\"># 每秒允许发送的消息数</span>\n  <span class=\"hljs-attr\">burst_count:</span> <span class=\"hljs-number\">10</span>    <span class=\"hljs-comment\"># 突发消息缓冲区大小</span>\n\n<span class=\"hljs-comment\"># 房间加入速率限制</span>\n<span class=\"hljs-attr\">rc_joins:</span>\n  <span class=\"hljs-attr\">local:</span>\n    <span class=\"hljs-attr\">per_second:</span> <span class=\"hljs-number\">0.1</span>   <span class=\"hljs-comment\"># 本地用户加入房间的速率</span>\n    <span class=\"hljs-attr\">burst_count:</span> <span class=\"hljs-number\">10</span>\n  <span class=\"hljs-attr\">remote:</span>\n    <span class=\"hljs-attr\">per_second:</span> <span class=\"hljs-number\">0.01</span>  <span class=\"hljs-comment\"># 远程用户加入房间的速率</span>\n    <span class=\"hljs-attr\">burst_count:</span> <span class=\"hljs-number\">10</span>\n\n<span class=\"hljs-comment\"># 媒体保留设置</span>\n<span class=\"hljs-attr\">media_retention:</span>\n  <span class=\"hljs-comment\"># 本地媒体文件的保留时间</span>\n  <span class=\"hljs-attr\">local_media_lifetime:</span> <span class=\"hljs-string\">90d</span>\n  \n  <span class=\"hljs-comment\"># 远程媒体文件的保留时间（来自其他homeserver的媒体）</span>\n  <span class=\"hljs-attr\">remote_media_lifetime:</span> <span class=\"hljs-string\">14d</span>\n\n<span class=\"hljs-comment\"># 删除陈旧设备的时间</span>\n<span class=\"hljs-attr\">delete_stale_devices_after:</span> <span class=\"hljs-string\">1y</span>\n\n<span class=\"hljs-attr\">auto_join_rooms:</span>\n  <span class=\"hljs-bullet\">-</span> <span class=\"hljs-string\">\"#XXX:你的家服务器URL\"</span> <span class=\"hljs-comment\"># 需要自动加入的房间</span></code></pre></div><p>{% endraw %}</p>\n<p>按需配置，更多高级配置参阅： <a href=\"https://element-hq.github.io/synapse/latest/usage/configuration/homeserver_sample_config.html\" target=\"_blank\" rel=\"noopener noreferrer\">Homeserver Sample Config File - Synapse</a></p>\n<h2 id=\"创建管理员账号\">创建管理员账号</h2><p>连接上容器的终端然后输入这串命令创建管理员账号</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">register_new_matrix_user  http://localhost:8008 -c /data/homeserver.yaml  -a -u 管理员用户名 -p 密码</code></pre></div><h2 id=\"开始聊天\">开始聊天</h2><p>前往 <a href=\"https://app.element.io\" target=\"_blank\" rel=\"noopener noreferrer\">https://app.element.io</a> 将家服务器改为你的（必须为HTTPS）</p>\n<p>通过刚刚创建的管理员账号登录</p>\n<p>其他人可以通过邮箱注册</p>\n","toc":[{"id":"前置环境准备","text":"前置环境准备","depth":2},{"id":"部署postgresql","text":"部署PostgreSQL","depth":2},{"id":"部署synapse","text":"部署Synapse","depth":2},{"id":"创建管理员账号","text":"创建管理员账号","depth":2},{"id":"开始聊天","text":"开始聊天","depth":2}]}