Matrix: Difference between revisions

From CSCWiki
Jump to navigation Jump to search
Content deleted Content added
Vdnguyen (talk | contribs)
No edit summary
+
Line 1: Line 1:
We are currently setting up a test server for Matrix. We use Synapse. If everything goes well, we will set up a production Matrix server.
We are currently setting up a test server for Matrix. We use Synapse. If everything goes well, we will set up a production Matrix server.

Note: all steps are adapted from https://element-hq.github.io/synapse/latest/setup/installation.html.


== Server Setup ==
== Server Setup ==


We are currently running Matrix on a Proxmox VM on <code>citric-acid</code>. Ask Siracha for the credentials to access Proxmox and the VM.
We are currently running Matrix on a Proxmox LXE container on <code>citric-acid</code>. Ask Siracha for the credentials to access Proxmox and the VM.


== Matrix Installation ==
== Matrix Installation ==
<pre>
sudo apt install -y lsb-release wget apt-transport-https
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" |
sudo tee /etc/apt/sources.list.d/matrix-org.list
sudo apt update
sudo apt install matrix-synapse-py3
</pre>


We are using NixOS, so use the following config (might be a bit messy) to setup both Synapse, Nginx, and PostgreSQL:
== Database Setup ==
Install PostgreSQL and related packages.
<pre>
apt install postgresql libpq5
</pre>


'''Note''': We could have used <code>recommendedProxySettings = true</code> which sets most of the <code>X-Forwarded-For</code> headers correctly. However, this is a reverse proxy behind a reverse proxy (Nginx on citric acid -> Nginx on the container -> Synapse) so <code>X-Forwarded-Proto</code> has to be always set to https. I'm sure there is a better way to do this.
Authenticate as database user.
<pre>
su - postgres
# Or, if your system uses sudo to get administrative rights
sudo -u postgres bash
</pre>


Create a postgres user and a database.
<pre>
<pre>
{ pkgs, lib, config, ... }:
# this will prompt for a password for the new user
let
createuser --pwprompt synapse_user
fqdn = "matrix.${config.networking.domain}";
clientConfig = {
"m.homeserver".base_url = "https://${fqdn}";
"m.identity_server" = {};
};
serverConfig."m.server" = "${fqdn}:443";
mkWellKnown = data: ''
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
return 200 '${builtins.toJSON data}';
'';
extraCfg = pkgs.writeText "synapse-extra-config.yaml" ''
'';
in {
networking.firewall = {
enable = true;
allowedTCPPorts = [ 80 8008 ];
};
networking.domain = "csclub.uwaterloo.ca";


services.postgresql = {
createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
enable = true;
initialScript = pkgs.writeText "synapse-init.sql" ''
CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD 'synapse';
CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
TEMPLATE template0
LC_COLLATE = "C"
LC_CTYPE = "C";
'';
dataDir = "/data/postgresql";
};

services.nginx = {
enable = true;
# recommendedProxySettings = true;
virtualHosts = {
"csclub.uwaterloo.ca" = {
locations."/".extraConfig = ''
return 404;
'';
locations."= /.well-known/matrix/server".extraConfig = mkWellKnown serverConfig;
locations."= /.well-known/matrix/client".extraConfig = mkWellKnown clientConfig;
};
"${fqdn}" = {
locations."/".extraConfig = ''
return 404;
'';
locations."/_matrix".proxyPass = "http://[::1]:8008";
locations."/_matrix".extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
'';
locations."/_synapse/client".proxyPass = "http://[::1]:8008";
locations."/_synapse/client".extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
'';
};
};
};

services.matrix-synapse = {
enable = true;
extraConfigFiles = [ extraCfg ];
dataDir = "/data/matrix-synapse";
settings = {
server_name = config.networking.domain;
public_baseurl = "https://matrix.csclub.uwaterloo.ca/";
listeners = [
{
port = 8008;
bind_addresses = [ "::1" ];
type = "http";
tls = false;
x_forwarded = true;
resources = [
{
names = [ "client" ]; # no federation
compress = true;
}
];
}
];

oidc_providers = [
{
idp_id = "keycloak";
idp_name = "CSC";
issuer = "https://keycloak.csclub.uwaterloo.ca/realms/csc";
client_id = "synapse";
client_secret = "xxxx"; # fill the client secret from keycloak here
scopes = [ "openid" "profile" ];
user_mapping_provider.config = {
localpart_template = "{{ user.preferred_username }}";
display_name_template = "{{ user.name }}";
};

}
];
registration_shared_secret = "xxxxx";
enable_registration = true;
enable_registration_captcha = false;
registration_requires_token = true;
};
};
}
</pre>
</pre>


== Testing ==
Edit Synapse config in <code>/etc/matrix-synapse/homeserver.yaml</code>.

<pre>
Go to https://app.cinny.in/login/matrix.csclub.uwaterloo.ca, and click "Continue with CSC".
database:
name: psycopg2
args:
user: synapse_user
password: <pass>
dbname: synapse
host: localhost
cp_min: 5
cp_max: 10
</pre>

Revision as of 21:50, 17 September 2025

We are currently setting up a test server for Matrix. We use Synapse. If everything goes well, we will set up a production Matrix server.

Server Setup

We are currently running Matrix on a Proxmox LXE container on citric-acid. Ask Siracha for the credentials to access Proxmox and the VM.

Matrix Installation

We are using NixOS, so use the following config (might be a bit messy) to setup both Synapse, Nginx, and PostgreSQL:

Note: We could have used recommendedProxySettings = true which sets most of the X-Forwarded-For headers correctly. However, this is a reverse proxy behind a reverse proxy (Nginx on citric acid -> Nginx on the container -> Synapse) so X-Forwarded-Proto has to be always set to https. I'm sure there is a better way to do this.

{ pkgs, lib, config, ... }:
let
  fqdn = "matrix.${config.networking.domain}";
  clientConfig = {
    "m.homeserver".base_url = "https://${fqdn}";
    "m.identity_server" = {};
  };
  serverConfig."m.server" = "${fqdn}:443";
  mkWellKnown = data: ''
    add_header Content-Type application/json;
    add_header Access-Control-Allow-Origin *;
    return 200 '${builtins.toJSON data}';
  '';
  extraCfg = pkgs.writeText "synapse-extra-config.yaml" ''
  '';
in {
  networking.firewall = {
    enable = true;
    allowedTCPPorts = [ 80 8008 ];
  };
  networking.domain = "csclub.uwaterloo.ca";

  services.postgresql = {
    enable = true;
    initialScript = pkgs.writeText "synapse-init.sql" ''
      CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD 'synapse';
      CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
        TEMPLATE template0
        LC_COLLATE = "C"
        LC_CTYPE = "C";
    '';
    dataDir = "/data/postgresql";
  };

  services.nginx = {
    enable = true;
    # recommendedProxySettings = true;
    virtualHosts = {
      "csclub.uwaterloo.ca" = {
        locations."/".extraConfig = ''
          return 404;
        '';
        locations."= /.well-known/matrix/server".extraConfig = mkWellKnown serverConfig;
        locations."= /.well-known/matrix/client".extraConfig = mkWellKnown clientConfig;
      };
      "${fqdn}" = {
        locations."/".extraConfig = ''
          return 404;
        '';
        locations."/_matrix".proxyPass = "http://[::1]:8008";
        locations."/_matrix".extraConfig = ''
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto https;
    proxy_set_header        X-Forwarded-Host $host;
    proxy_set_header        X-Forwarded-Server $host;
        '';
        locations."/_synapse/client".proxyPass = "http://[::1]:8008";
        locations."/_synapse/client".extraConfig = ''
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto https;
    proxy_set_header        X-Forwarded-Host $host;
    proxy_set_header        X-Forwarded-Server $host;
        '';
      };
    };
  };

  services.matrix-synapse = {
    enable = true;
    extraConfigFiles = [ extraCfg ];
    dataDir = "/data/matrix-synapse";
    settings = {
      server_name = config.networking.domain;
      public_baseurl = "https://matrix.csclub.uwaterloo.ca/";
      listeners = [
        {
          port = 8008;
          bind_addresses = [ "::1" ];
          type = "http";
          tls = false;
          x_forwarded = true;
          resources = [
            {
                names = [ "client" ]; # no federation
                compress = true;
            }
          ];
        }
      ];

      oidc_providers = [
        {
           idp_id = "keycloak";
           idp_name = "CSC";
           issuer = "https://keycloak.csclub.uwaterloo.ca/realms/csc";
           client_id = "synapse";
           client_secret = "xxxx"; # fill the client secret from keycloak here
           scopes = [ "openid" "profile" ];
           user_mapping_provider.config = {
                localpart_template = "{{ user.preferred_username }}";
                display_name_template = "{{ user.name }}";
           };

        }
      ];
      registration_shared_secret = "xxxxx";
      enable_registration = true;
      enable_registration_captcha = false;
      registration_requires_token = true;
    };
  };
}

Testing

Go to https://app.cinny.in/login/matrix.csclub.uwaterloo.ca, and click "Continue with CSC".