matrix-synapse をインストール (Ubuntu 20.04 LTS / Apache2)

検索して調べても nginx 環境で構築する記事のほうが多い為、apache2からの乗り換えも考えましたが、時間をかけて色々試行錯誤してみた所、Federation TesterでNo SRV Recordsのメッセージが出てしまうのは消すことが出来ていないものの、概ね成功したようなのでとりあえずの導入の部分だけですが思え書きとして書いておきます。Googleから調べた断片情報のニコイチ的なやり方でインストールしているので無駄な所も間違いなくあります。

sudo uname --all の実行結果 : Linux ***** 5.8.0-45-generic #51~20.04.1-Ubuntu SMP Tue Feb 23 13:46:31 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

とても役に立った|ヒントになった参考元ページ

導入で躓いた点|注意点|留意事項

  • 素の名前の"matrix-synapse"ではなく"matrix-synapse-py3"の方をインストールする /

    -py3と末尾につくほうが新しいバージョンなようです。

  • インストール直後にsystemctl status matrix-synapse と確認しても動作しておらず、journalctl -u などで調べても警告が出まくっているが、気にせずに一旦サービスを停止しセットアップ作業を続ける

    多くのインストール方法の紹介ではsynapseインストール直後にてsystemctl statusでサービスの動作確認をしているが、検証環境によってその時点でサービスが動作出来ていない場合があり、この時点でどう解決すれば良いのか悩んでしまう。

    • このような状態の場合、matrix-synapseのsystemdサービスの登録の設定がプロセス終了した場合に無条件に再起動するようになっていて、無限に起動ログを吐き続けるため、ディスクの領域負担的にも明示的にsystemctl stop matrix-synapse とコマンド実行して一旦停止させてから作業を続行すること
    • 他の要因もあるが当方の環境ではsynapseサービスのセキュリティキーファイルのアクセスの為のパーミションの設定の問題が主だった。certbotが取得したセキュリティキーのファイルにのpermissionにsynapseやapache2サービスの実行ユーザーがアクセスできるようにする
  • サービスの起動がfailする場合 systemctl statusやjouranalログを調べるのではなく/var/log/matrix-synapse/homeserver.logを見たほうが明確な原因を発見できる

構成

PC_2を動作確認用などで使用する構成の想定です

f:id:SriVidyut:20210427161511j:plain



インストール作業

  1. python-certbot-apacheとの他のインストール
     sudo su
     apt update
     apt upgrade
     apt install python3-certbot-apache lsb-release wget apt-transport-https
    
  2. apache2の追加モジュールの有効化
     a2enmod proxy
     a2enmod proxy_http
     a2enmod proxy_balancer
     a2enmod ssl
     a2enmod headers
     a2enmod lbmethod_byrequests
    
  3. synapse サーバーのインストール

    一行目の実行(wget)がうまく行かなかったら一旦ブラウザからダウンロードして /usr/share/keyrings に移動させる

     wget -qO /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" | tee /etc/apt/sources.list.d/matrix-org.list
     apt update
     apt install matrix-synapse-py3
    

    運が良ければインストール直後の時点での systemctl status matrix-synapse コマンドで緑色の●になっています。しかしインストールを試す環境によっては白丸で動かていない・・というか無限再起動な挙動っぽいのでとりえずサービスを明示的に停止して作業を続行します。

  4. ufwの設定

    動作させる場合に利用ポートを開ける設定をする。synapseの動作するバックエンド側がローカルネット上の別ホストの場合では少なくともsynapseの動作側で8008のポートは開ける必要がある

    sudo su
    ufw status
    ufw allow ssh
    ufw allow http
    ufw allow https
    ufw allow 8448/tcp
    ufw status numbered
    
  5. おまじないも含めてPCを再起動

設定作業

  1. SRV Recordの登録をしておく / ※SRV Recordの設定の出来るダイナミックDNSサービスもあります。ここでは挙げませんが探してみてください。
    SRVレコードの登録確認
    dig -t srv _matrix._tcp.example.com
    

    上記のコマンドの実行結果

    ;; ANSWER SECTION:
    _matrix._tcp.example.com. 60 IN	SRV	15 5 8448 matrix1.example.com.
    _matrix._tcp.example.com. 60 IN	SRV	10 5 8448 matrix2.example.com.
    
  2. letsencryptサービスを利用したSSL証明書の取得コマンドを実行 / python-certbot-apacheの実行
    certbot --apache -d www.example.com,matrix1.example.com,matrix2.example.com
    

    SSL側のページへリダイレクトするかの質問に一応しないでおいた

  3. インストールされたファイル関連のパーミッションやユーザーのグループへの付加 certbotの実行で生成されたファイルのうち必要な2つ
    /etc/letsencrypt/live/www.example.com/fullchain.pem
    /etc/letsencrypt/live/www.example.com/privkey.pem
    

    apache2(www-data)とmatrix-synapse 両systemdの実行ユーザーがアクセスできるようにしておくか別フォルダfullchain.pemと privkey.pemファイルを別途用意した運用用のディレクトリにコピーするなどしてそこをパーミッション設定する (このパスは控えておく)

  4. /etc/apache2/sites-available/000-default-le-ssl.conf (certbotの実行により追加生成されたapache2の設定ファイル)の編集
    • matrix-synapseを別ホストで稼働させる場合はhttp://127.0.0.1から変更することと/etc/apache2/ports.confの編集の必要がある。
    • apache2の古いバージョンの場合はNameVirtualHostの宣言を追加しておく。
    • DocumentRootはAPI上のやりとりでのURLのパスの衝突を避けるためじゃないかと思いますが別途に用意したパスを指定したほうが望ましいかもしれないです。
    <IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName matrix1.example.com
        ServerAdmin admin@example.com
        DocumentRoot /path_to_docroot
    
        SSLEngine on
    
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
        AllowEncodedSlashes NoDecode
        ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
        ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
        ProxyPass /_synapse/client http://127.0.0.1:8008/_synapse/client nocanon
        ProxyPassReverse /_synapse/client http://127.0.0.1:8008/_synapse/client
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    
        <IfModule security2_module>
            SecRuleEngine off
        </IfModule>
    </VirtualHost>
    
    <VirtualHost *:8448>
        ServerName matrix1.example.com
        ServerAdmin admin@example.com
        DocumentRoot /path_to_docroot
    
        SSLEngine on
    
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
        AllowEncodedSlashes NoDecode
        ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
        ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    
        <IfModule security2_module>
            SecRuleEngine off
        </IfModule>
    </VirtualHost>
    </IfModule>
    
    ※synapseサービスをローカルネット上の別ホスト・バックエンドで動かす場合(アドレスを192.168.0.5とする。追加のapache2モジュールの有効化が必要かもしれません。)
    <IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName matrix2.example.com
        ServerAdmin admin@example.com
        SSLEngine on
    
        ProxyRequests on
        ProxyPreserveHost off
        ProxyVia off
        AddDefaultCharset off
    
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
        AllowEncodedSlashes NoDecode
    
        ProxyPass /_matrix http://192.168.0.5:8008/_matrix nocanon
        ProxyPassReverse /_matrix http://192.168.0.5:8008/_matrix
        ProxyPass /_synapse/client http://192.168.0.5:8008/_synapse/client nocanon
        ProxyPassReverse /_synapse/client http://192.168.0.5:8008/_synapse/client
    
        <IfModule security2_module>
            SecRuleEngine off
        </IfModule>
        
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    </VirtualHost>
    
    <VirtualHost *:8448>
        ServerName matrix2.example.com
        ServerAdmin admin@example.com
    
        SSLEngine on
        ProxyRequests off
        ProxyPreserveHost off
        ProxyVia off
        AddDefaultCharset off
    
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
    
        AllowEncodedSlashes NoDecode
        ProxyPass /_matrix http://192.168.0.5:8008/_matrix nocanon
        ProxyPassReverse /_matrix http://192.168.0.5:8008/_matrix
        
        <IfModule security2_module>
            SecRuleEngine off
        </IfModule>
        
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    
        <IfModule security2_module>
  5. /etc/apache2/ports.confの設定 / リスンポート8448を追加
    <IfModule ssl_module>
    	Listen 443
    	Listen 8448
    </IfModule>
    

    apache2ctl configtest ※設定ファイルのチェックコマンド

  6. /etc/matrix-synapse/homeserver.yamlの編集

    以下のコマンドを端末から実行して乱数文字列を別に控えておく

    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
    1240行辺り コメントアウトされた行を有効して上で表示された文字列を設定
    registration_shared_secret: "乱数文字列"
    apache2の動作PCとは別体で利用する場合(この記事でいう構成のPC_2)はそのPCのローカルネット上のNICのアドレス(ここでの例は192.168.0.5)を割り当てる、element web等を利用する場合は webclient を追加する
      - port: 8008
        tls: false
        type: http
        x_forwarded: true    
    #   bind_addresses: ['::1', '127.0.0.1']
        bind_addresses: ['::1', '127.0.0.1','192.168.0.5']
    
        resources:
    #     - names: [client, federation]
          - names: [client, federation, webclient]
            compress: false
    
    510行以降辺り / certbotが取得したファイル
    tls_certificate_path: "/etc/letsencrypt/live/www.example.com/fullchain.pem"
    tls_private_key_path: "/etc/letsencrypt/live/www.example.com/privkey.pem"
    
  7. /path_to_docroot/.well-known/へのファイル追加 dynamic DNSサービスを利用しているなどの理由由来でDNSSRVレコードの登録ができない場合はこの設定をする必要があるようです。 apache2で設定した各ヴァーチャルホストのディレクティブ中で設定したDocumentRoot以下にフォルダを作ってファイルを編集する。
    mkdir -p /path_to_docroot/.well-known/matrix
    chmod -R www-data /path_to_docroot/.well-known/matrix
    nano /path_to_docroot/.well-known/matrix/server
    
    ファイル: serverの内容 (※ matrix2.example.comの場合はmatrix2.example.com) / (アクセス元の変数云々でサーバー名の出力を切り替えるCGIに変更しても良いかもしれない。 )
    {
    "m.server": "matrix1.example.com:443"
    }
    
  8. サービスの再起動と動作確認、管理ユーザーの登録
  9. 詳細な設定について /

    この記事ではとりあえずFederationの確認迄になっていますが、実運用ではsqliteではなくpostgresqlサーバー、capchaのセキュリティでは不十分なようで登録ユーザーへの送信用のメールアドレスの設定も行ったほうが良いらしいです