步骤/目录:
1.背景介绍
2.Matrix与element的安装
3.Matrix的设置与使用
    (1)初始化
    (2)注册管理员账号
    (3)关闭注册功能
    (4)其它

本文首发于个人博客https://lisper517.top/index.php/archives/225/,转载请注明出处。
本文的目的是介绍一款去中心化的私密聊天软件-Matrix。
本文写作日期为2024年1月1日。主要受up主 我不是咕咕鸽 的启发。他还分享了很多其它有趣的内容,讲解也很详细、基础,很适合小白学习。

1.背景介绍

在日常管理服务器的过程中,一些工具能大大减轻工作量。前几天在up主 我不是咕咕鸽 的空间看到了很多有趣的分享,也尝试自己搭建一下。

Matrix是一款支持端到端加密、去中心化的聊天服务器软件,比较适合小公司或者需要保护公司秘密、个人隐私时。你可以搭建自己的Matrix服务器,借助一些Matrix的客户端,在自己的服务器内,或者与其它的Matrix服务器通信。

2.Matrix与element的安装

在服务器上如下操作:

mkdir -p /docker/matrix/data/matrix
mkdir -p /docker/matrix/conf/element

接下来需要运行一次Matrix,生成默认的配置等文件(把域名改成自己为Matrix准备的域名):

sudo docker run -it --rm \
-v /docker/matrix/data/matrix:/data \
-e SYNAPSE_SERVER_NAME=matrix域名 \
-e SYNAPSE_REPORT_STATS=yes \
matrixdotorg/synapse:latest generate

修改一下homeserver.yaml,允许注册、允许无验证注册:

vim /docker/matrix/data/matrix/homeserver.yaml

在末尾加入以下两行(也可以不加,因为能通过其它方式创建用户):

enable_registration: true
enable_registration_without_verification: true

修改后该文件内容如下:

# Configuration file for Synapse.
#
# This is a YAML file: see [1] for a quick introduction. Note in particular
# that *indentation is important*: all the elements of a list or dictionary
# should have the same indentation.
#
# [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
#
# For more information on how to configure Synapse, including a complete accounting of
# each option, go to docs/usage/configuration/config_documentation.md or
# https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html
server_name: "你的matrix域名"
pid_file: /data/homeserver.pid
listeners:
  - port: 8008
    tls: false
    type: http
    x_forwarded: true
    resources:
      - names: [client, federation]
        compress: false
database:
  name: sqlite3
  args:
    database: /data/homeserver.db
log_config: "/data/你的matrix域名.log.config"
media_store_path: /data/media_store
registration_shared_secret: "一串密码"
report_stats: true
macaroon_secret_key: "一串密码"
form_secret: "一串密码"
signing_key_path: "/data/你的matrix域名.signing.key"
trusted_key_servers:
  - server_name: "matrix.org"

enable_registration: true
enable_registration_without_verification: true

# vim:ft=yaml

最后创建一下yml:

cd /docker/matrix
vim docker-compose.yml

写入以下内容:

version: "3.3"
services:
  synapse:
    image: "matrixdotorg/synapse:latest"
    container_name: "matrix"
    ports:
      - 8008:8008
    volumes:
      - "./data/matrix:/data"
    environment:
      VIRTUAL_HOST: "你的matrix域名"
      VIRTUAL_PORT: 8008
      LETSENCRYPT_HOST: "你的matrix域名"
      SYNAPSE_SERVER_NAME: "你的matrix域名"
      SYNAPSE_REPORT_STATS: "yes"
      restart: unless-stopped

  element-web:
    image: vectorim/element-web
    ports:
      - '80:80'
    #用好了element默认设置后,可以挂自己的设置
    #volumes:
      #- "./conf/element/config.json:/app/config.json"
    restart: unless-stopped

Element是Matrix应用较广的一个客户端,有web、PC、手机端(国内无法用到),这里顺便把element网页端也搭建了。两个分别用80、8008,有冲突的话改一下。
最后运行:

docker-compose config
docker-compose up

如果有NPM的话,可以给2个子域名,详见 可视化管理Nginx-Nginx Proxy Manager 。总之通过各种方式,使得两个域名都通过https访问。

3.Matrix的设置与使用

(1)初始化

访问你的matrix域名,能看到matrix界面。
访问你的element域名,可以切换,以你的matrix域名作为服务器,注册一个账号。但是现在先不要注册,因为还需要创建一个管理员。

(2)注册管理员账号

根据 官方文档 ,Matrix可以通过修改数据库来将已有的用户改为管理员,或者用 register_new_matrix_user 在新建用户时顺便赋予管理员权限。笔者用第二种方式演示:

docker exec -it matrix bash
#进入matrix容器
register_new_matrix_user --help
register_new_matrix_user -u 用户名 -p 密码 --admin -c "/data/homeserver.yaml"

换成自己需要的即可。如果要注册成普通用户,把 --admin 换成 --no-admin 即可。根据help信息的提示,usertypes一共有admin、guest、none三种,普通用户是none。

(3)关闭注册功能

/docker/matrix/data/matrix/homeserver.yaml 中的:

enable_registration: true
enable_registration_without_verification: true

注释掉,重新创建容器。

(4)其它

matrix更多的配置在 配置文档 。几个比较重要的有:

a.SMTP,邮件(见 此处)。不会用SMTP的可参考 这篇文章 介绍CommentNotifier一节。由于用户填写个人邮箱时要发确认邮件,SMTP还是有可能用到的;

b.电话服务,见 此处 。类似邮件,用户填个人电话时也要验证码;

c.通过token控制注册人数(控制时间、个数等),Registration Tokens;不过这个好像已经被废弃了,见 此处

标签: docker, matrix

添加新评论