ubuntu22.04 安装synapse服务端

ubuntu22.04 安装synapse服务端

# Synapse

synapse 是一个开源的 Matrix 服务器, 它实现了 Matrix 协议.这次用官方推荐的Matrix.org packages的方式来安装.

github链接: https://github.com/element-hq/synapse

安装文档: https://element-hq.github.io/synapse/latest/setup/installation.html#debianubuntu

简单安装可以参考这篇文章: https://www.hostzealot.com/blog/how-to/step-by-step-guide-to-installing-synapse-matrix-server-on-ubuntu-2204

# 安装步骤

# 安装synapse deb包

1
2
3
4
5
sudo wget -O /usr/share/keyrings/matrix-org-archive-keyring.gpg https://packages.matrix.org/debian/matrix-org-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/matrix-org-archive-keyring.gpg] https://packages.matrix.org/debian/ $(lsb_release -cs) main prerelease" |
    sudo tee /etc/apt/sources.list.d/matrix-org.list
sudo apt update
sudo apt install matrix-synapse-py3

# 安装postgresql

1
2
3
sudo apt install build-essential python3-dev libffi-dev python3-pip python3-setuptools sqlite3 libssl-dev virtualenv libjpeg-dev libxslt1-dev libicu-dev
sudo apt install libpq5
sudo apt install postgresql postgresql-contrib

安装完成后进入pg shell:

1
2
sudo -i -u postgres
psql

创建用户和数据库(在pg shell中操作):

1
2
3
4
# this will prompt for a password for the new user
createuser --pwprompt synapse_user

createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse

# 修改synapse配置文件

vi /etc/matrix-synapse/homeserver.yaml:

 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
# 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://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html
#
# This is set in /etc/matrix-synapse/conf.d/server_name.yaml for Debian installations.
# server_name: "SERVERNAME"
pid_file: "/var/run/matrix-synapse.pid"
listeners:
  - port: 8008
    tls: false
    type: http
    x_forwarded: true
    bind_addresses: ['::1', '127.0.0.1']
    resources:
      - names: [client, federation]
        compress: false
enable_registration: false
log_config: "/etc/matrix-synapse/log.yaml"
media_store_path: /var/lib/matrix-synapse/media
signing_key_path: "/etc/matrix-synapse/homeserver.signing.key"
trusted_key_servers:
  - server_name: "matrix.org"

vi /etc/matrix-synapse/conf.d/data_base.yaml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
database:
  name: psycopg2
  args:
    user: *******
    password: **********
    dbname: synapse
    host: localhost
    cp_min: 5
    cp_max: 10
    # seconds of inactivity after which TCP should send a keepalive message to the server
    keepalives_idle: 10

    # the number of seconds after which a TCP keepalive message that is not
    # acknowledged by the server should be retransmitted
    keepalives_interval: 10

    # the number of TCP keepalives that can be lost before the client's connection
    # to the server is considered dead
    keepalives_count: 3

vi /etc/matrix-synapse/conf.d/server_name.yaml:

1
2
3
4
5
6
7
8
9
# This file is autogenerated, and will be recreated on upgrade if it is deleted.
# Any changes you make will be preserved.

# The domain name of the server, with optional explicit port.
# This is used by remote servers to connect to this server,
# e.g. matrix.org, localhost:8080, etc.
# This is also the last part of your UserID.
#
server_name: yourdomain.com

# 注册一个用户

执行: matrix-synapse register_new_matrix_user -c /etc/matrix-synapse/homeserver.yaml http://localhost:8008

1
2
3
4
5
6
7
# matrix-synapse register_new_matrix_user -c /etc/matrix-synapse/homeserver.yaml http://localhost:8008
New user localpart [xfeng]: afan
Password:
Confirm password:
Make admin [no]:
Sending registration request...
Success!

出现Success!就表示成功了.

# 配置caddy反向代理

  1. 修改/etc/matrix-synapse/homeserver.yaml, 确认listeners有下面两个子项:
1
2
x_forwarded: true
request_id_header: true
  1. 修改Caddyfile(我这里用了8088端口):

vi /etc/caddy/Caddyfile

1
2
3
4
5
6
7
8
matrix.example.com:8088 {
  reverse_proxy /_matrix/* localhost:8008
  reverse_proxy /_synapse/client/* localhost:8008
}

example.com:8448 {
  reverse_proxy /_matrix/* localhost:8008
}

重启服务sudo systemctl restart matrix-synapse.service之后就可以在element 客户端填入服务器地址yourdomain.com:8088连接到这个服务器了.

# 配置email(可选)

修改/etc/matrix-synapse/homeserver.yaml

 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
email:
  smtp_host: mail.server
  smtp_port: 587
  smtp_user: "exampleusername"
  smtp_pass: "examplepassword"
  force_tls: true
  require_transport_security: true
  enable_tls: false
  notif_from: "Your Friendly %(app)s homeserver <noreply@example.com>"
  app_name: my_branded_matrix_server
  enable_notifs: true
  notif_for_new_users: false
  client_base_url: "http://localhost/riot"
  validation_token_lifetime: 15m
  invite_client_location: https://app.element.io

  subjects:
    message_from_person_in_room: "[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room..."
    message_from_person: "[%(app)s] You have a message on %(app)s from %(person)s..."
    messages_from_person: "[%(app)s] You have messages on %(app)s from %(person)s..."
    messages_in_room: "[%(app)s] You have messages on %(app)s in the %(room)s room..."
    messages_in_room_and_others: "[%(app)s] You have messages on %(app)s in the %(room)s room and others..."
    messages_from_person_and_others: "[%(app)s] You have messages on %(app)s from %(person)s and others..."
    invite_from_person_to_room: "[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s..."
    invite_from_person: "[%(app)s] %(person)s has invited you to chat on %(app)s..."
    password_reset: "[%(server_name)s] Password reset"
    email_validation: "[%(server_name)s] Validate your email"