Matrix備忘録
※Synapseのみで動作することに気づいたため修正した。
Matrix と Element を自分のサーバーで動かす
以下の2つの記事が参考になりました。ありがとうございます。


以下はすでに docker で caddy が動作している環境の話となっています。ホスト名もすでに存在している前提になっています。
利用ポート
連合のために8448
ポートが使われるのでポート開放しておくこと。
80
,443
ポートはCaddyで利用するために開放してあるものとする。
内部ポート8008
につなげるポートとして10088
を利用した。
compose.yml を作成する
ここでは matrix -> docker
というディレクトリ構成下に compose.yml
ファイルを作成した。
└── matrix
└── docker
└── compose.yml (これを編集する)
matrix/docker/compose.yml
services:
synapse:
restart: always
image: matrixdotorg/synapse:latest
volumes:
- ../data:/data
environment:
- SYNAPSE_SERVER_NAME=自分のMatrixサーバーのホスト名
- SYNAPSE_REPORT_STATS=yes
ports:
- "10088:8008"
networks:
- external_network
- internal_network
networks:
external_network:
internal_network:
internal: true
Synapse 設定ファイル群の生成
Synapseの設定ファイル群を生成するために compose.yml ファイルがあるディレクトリで
docker compose run --rm synapse generate
を実行する。
上記の compose.yml ではそのファイルがある上のディレクトリに設定ファイル・ディレクトリ群(data db redis)が生成される。(volumesのディレクトリ指定に注意)
設定ファイル編集のため一旦コンテナを停止する。docker compose down
homeserver.yamlの編集
次に上位ディレクトリに生成された設定ファイルを編集する。vi ../data/homeserver.yaml
└── matrix
├── data
│ └── homeserver.yaml (これを編集する)
└── docker
└── compose.yml
以下は生成直後のもの。“XXXX” には自動生成されたものが入る。
matrix/data/homeserver.yaml
# 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
server_name: "XXXX"
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/XXXX.log.config"
media_store_path: /data/media_store
registration_shared_secret: "XXXX"
report_stats: true
macaroon_secret_key: "XXXX"
form_secret: "XXXX"
signing_key_path: "/data/XXXX.signing.key"
trusted_key_servers:
- server_name: "matrix.org"
# vim:ft=yaml
このままではユーザー登録や連合時の検索、外部からのサーバー追加などが機能しないので以下を追記する。
enable_registration: true
enable_registration_without_verification: true
allow_public_rooms_over_federation: true
allow_public_rooms_without_auth: true
enable_room_list_search: true
ちなみにこの状態だと自由に登録し放題なので注意。enable_registration
を false
にすれば自由に登録はできなくなる。
その後 compose.yml
のあるディレクトリに戻り docker compose up -d
を実行する。docker ps
で正しく起動できているか確認。
チャットでリンクが貼られたときにリンクカードが作られるようにする
サンプルの記述は以下のとおり。rangeは自分の環境に合わせて変更する。blacklist関係は無駄な負荷を防ぐためのもの。
url_preview_enabled: true
url_preview_ip_range_blacklist:
- "127.0.0.0/8"
- "10.0.0.0/8"
- "172.16.0.0/12"
- "192.168.0.0/16"
- "100.64.0.0/10"
- "192.0.0.0/24"
- "169.254.0.0/16"
- "192.88.99.0/24"
- "198.18.0.0/15"
- "192.0.2.0/24"
- "198.51.100.0/24"
- "203.0.113.0/24"
- "224.0.0.0/4"
- "::1/128"
- "fe80::/10"
- "fc00::/7"
- "2001:db8::/32"
- "ff00::/8"
- "fec0::/10"
url_preview_url_blacklist:
# blacklist any URL with a username in its URI
- username: "*"
# blacklist all *.google.com URLs
- netloc: "google.com"
- netloc: "*.google.com"
# blacklist all plain HTTP URLs
- scheme: "http"
# blacklist http(s)://www.acme.com/foo
- netloc: "www.acme.com"
path: "/foo"
# blacklist any URL with a literal IPv4 address
- netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
Element の準備
自分のサーバー上でMatrixクライアントも動作させるために Element を設置する。
GithubのElementのreleasesページから tar.gz ファイルを取得する。
wget https://github.com/element-hq/element-web/releases/download/v1.11.91/element-v1.11.91.tar.gz
サーバー上のElementのルートとしたいディレクトリ上にtar.gzファイルを展開しその中身を設置する。ファイルの中にconfig.sample.json
があるのでそれを config.json
に名前を変更して自分のサーバー用に編集する。
{
"default_server_config": {
"m.homeserver": {
"base_url": "https://自分のMatrixサーバーのホスト名",
"server_name": "自分のMatrixサーバーのホスト名"
},
"m.identity_server": {
"base_url": "https://vector.im"
}
},
"disable_custom_urls": false,
"disable_guests": false,
"disable_login_language_selector": false,
"disable_3pid_login": false,
"force_verification": false,
"brand": "Element",
"integrations_ui_url": "https://scalar.vector.im/",
"integrations_rest_url": "https://scalar.vector.im/api",
"integrations_widgets_urls": [
"https://scalar.vector.im/_matrix/integrations/v1",
"https://scalar.vector.im/api",
"https://scalar-staging.vector.im/_matrix/integrations/v1",
"https://scalar-staging.vector.im/api",
"https://scalar-staging.riot.im/scalar/api"
],
"default_widget_container_height": 280,
"default_country_code": "JP",
"show_labs_settings": false,
"features": {},
"default_federate": true,
"default_theme": "light",
"room_directory": {
"servers": ["matrix.org"]
},
"enable_presence_by_hs_url": {
"https://matrix.org": false,
"https://matrix-client.matrix.org": false
},
"setting_defaults": {
"breadcrumbs": true
},
"jitsi": {
"preferred_domain": "meet.element.io"
},
"element_call": {
"url": "https://call.element.io",
"participant_limit": 8,
"brand": "Element Call"
},
"map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx"
}
Caddyfile の設定
現在のサーバーは Caddy が動作しているのでそこからリバースプロキシする設定を追記する。
webサーバーソフトごとのリバースプロキシ設定はドキュメントに書かれている。
自分のMatrixサーバーのホスト名 {
tls {
on_demand
}
reverse_proxy /_matrix/* localhost:10088
reverse_proxy /_synapse/client/* localhost:10088
file_server
root * 「Elementのマウント先ディレクトリ」
encode zstd gzip
}
自分のMatrixサーバーのホスト名:8448 {
tls {
on_demand
}
reverse_proxy /_matrix/* localhost:10088
}
Element のマウント先ディレクトリは compose.yaml
ファイルの方で設定した。
volumes:
- type: bind
source: Elementがあるディレクトリ(ローカル)
target: Elementのマウント先ディレクトリ(コンテナ内)
他の部分は省略
Caddy を再起動した後に自分のMatrixサーバのアドレスからElementが起動するか確認する。
最後に compose.yml
があるディレクトリに戻りdocker compose up -d
を実行して Synapse を起動する。
注意とまとめ
管理者用メールのためのSMTP設定などはしていない状態なのであくまでテスト用です。
Elementの使い方は割愛します。