Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

这次用 docker 重装了服务,存档里有不用 docker 安装的详细笔记;记录了看到的(半)自动更新 element-web 的方法

目录结构

目录设定按照的这个结构,提前 mkdir 各个文件夹

matrix
   ├── docker-compose.yml
   ├── synapse
   │   ├── data
   │   │    └──homeserver.yaml (自动生成,需要修改)
   │   ├── db
   │   └── redis
   └── web
        ├── current
        └── archive

配置和安装 Synapse (docker)

生成 homeserver.yaml

docker run -it --rm \
-v ~/matrix/synapse/data:/data \
-e SYNAPSE_SERVER_NAME=<your_server_name> \
-e SYNAPSE_REPORT_STATS=no \
matrixdotorg/synapse:latest generate
  • ~/matrix/synapse/data是我选择的data文件的实际位置

修改 homeserver.yaml

在上一步结束后,会提示生成了 homeserver.yamlserver_name等内容已经自动为你填好了。sudo nano ~/matrix/synapse/data/homeserver.yaml ,别的需要手动修改的内容有:

serve_server_wellknown 设置

serve_server_wellknown: true
  • 如果你的前端和后端域名没有分开,一定要添加这一句上去

redis 配置

redis:
  enabled: true
  host: redis   # 这个和后面的 docker-compose.yml 是对应的,不要随便改
  port: 6379

数据库设置

database:
#  name: sqlite3
#  args:
#    database: /data/homeserver.db
#↑注释掉使用 sqlite3 的配置
  name: psycopg2
  txn_limit: 10000
  args:
    user: synapse
    password: <和docker-compose.yml里写的密码一样>
    database: synapse
    host: db
    port: 5432
    cp_min: 5
    cp_max: 10
  • 这里把默认的 sqlite3 数据库,换成了 Postgresql

设置服务器不对外注册

测试完服务器上线后,我又添加了这一部分

enable_registration: false
registration_shared_secret: "xxxxxxxx" # 配置了这个的话,实例不开放注册时,可由管理员创建新帐号

写 docker-compose.yml

matrix文件夹的位置上,sudo nano docker-compose.yml

#来自糖喵的配置文件,只有一点点个人改动
version: "3.4"

services:
    synapse:
        hostname: matrix
        image: matrixdotorg/synapse:latest
        restart: unless-stopped
        container_name: matrix_server
        depends_on:
           - db
           - redis
        ports:
           - "127.0.0.1:<your_port>:8008" # 修改端口
        volumes:
           - ./synapse/data:/data
        networks:
           - synapse_network
           - external_network
        healthcheck:
           test: ["CMD-SHELL", "curl -s localhost:8008/health || exit 1"]
    db:
        image: postgres:14.4-alpine
        restart: unless-stopped
        container_name: matrix_db
        volumes:
           - ./synapse/db:/var/lib/postgresql/data
        environment:
           POSTGRES_USER: synapse
           POSTGRES_PASSWORD: <设置你自己的数据库密码>
           POSTGRES_DB: synapse
           POSTGRES_INITDB_ARGS: "--encoding='UTF8' --lc-collate='C' --lc-ctype='C'"
        networks:
           - synapse_network
        healthcheck:
           test: ["CMD", "pg_isready", "-U", "synapse"]
    redis:
        image: redis:6.0-alpine
        restart: unless-stopped
        container_name: matrix_redis
        volumes:
           - ./synapse/redis:/data
        networks:
           - synapse_network
        healthcheck:
           test: ["CMD", "redis-cli", "ping"]
networks:
     external_network:
     synapse_network:
        internal: true
  • docker-compose up -d 之后,使用 docker ps -a会看到新增三个容器:matrix_server,matrix_db,matrix_redis

安装和自动更新 Element Web

按照这个教程做的:Install Element - Create a Chat Server Using Matrix Synapse and Element on Debian 11

安装 jq

sudo apt install jq

sudo nano element-update.sh写入更新脚本

#!/bin/sh
set -e

install_location="/home/usrname/matrix/web"  #你的存放目录
latest="$(curl -s https://api.github.com/repos/vector-im/element-web/releases/latest | jq -r .tag_name)"

cd "$install_location"

[ ! -d "archive" ] && mkdir -p "archive"
[ -d "archive/element-${latest}" ] && rm -r "archive/element-${latest}"
[ -f "archive/element-${latest}.tar.gz" ] && rm "archive/element-${latest}.tar.gz"

wget "https://github.com/vector-im/element-web/releases/download/${latest}/element-${latest}.tar.gz" -P "archive"
tar xf "archive/element-${latest}.tar.gz" -C "archive"

[ -L "${install_location}/current" ] && rm "${install_location}/current"
ln -sf "${install_location}/archive/element-${latest}" "${install_location}/current"
ln -sf "${install_location}/config.json" "${install_location}/current/config.json"

赋予执行权限

sudo chmod +x /path/to/element-update.sh

执行脚本安装/更新Element

sudo /path/to/element-update.sh

如果是首次安装,还需要修改 config.json

sudo cp ~/matrix/web/current/config.sample.json ~/matrix/web/config.json
sudo nano ~/matrix/web/config.json

修改默认 homeserver address

"m.homeserver": {
    "base_url": "https://<your_server_name>",
    "server_name": "<your_server_name>"
},

设置 nginx

新增 DNS 记录之后,按照这里 写入 nginx 配置文件并申请证书。然后按照搭建Matrix即时通信服务里的 nginx 配置修改,最后的样子是:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name  <your_server_name>;

    ssl_certificate /etc/letsencrypt/live/<your_server_name>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<your_server_name>z/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    root /home/username/matrix/web/current; # 放置 web 界面的地方,安装方式在存档里有写

    location ~ ^(/_matrix|/_synapse/client) {
        proxy_pass http://127.0.0.1:<your_port>; # 这里要根据服务器修改
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        # 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 500M;
    }

    location /.well-known/matrix/client {
        return 200 '{"m.homeserver": {"base_url": "<your_server_name>"}}'; # 这里要根据服务器修改
        default_type application/json;
        add_header Access-Control-Allow-Origin *;
    }

    location /.well-known/matrix/server {
        return 200 '{"m.server": "<your_server_name>:443"}';   # 这里要根据服务器修改
        default_type application/json;
        add_header Access-Control-Allow-Origin *;
    }

}

注册新用户

切换到容器内部

docker container exec -it matrix_server bash

注册用户

cd data
register_new_matrix_user -c homeserver.yaml http://localhost:8008 
exit

安装 turn 服务器

这部分看这两个就好:

再次修改 homeserver.yaml的时候,小心别切错目录了

参考内容

  1. 使用 Docker 安装 Matrix - 7 months ago
  2. 搭建Matrix即时通信服务 - 11 months ago
  3. 使用docker搭建Synapse[Matrix] - 2 years ago
  4. 联合部署 Mastodon 与 Synapse - 2 years ago
  5. Create a Chat Server Using Matrix Synapse and Element on Debian 11
  6. Synapse | Configuring a Turn Server

评论