User Tools

Site Tools


computing:synapse

This is an old revision of the document!



  • synapse
  • Jonathan Haack
  • Haack's Networking
  • webmaster@haacksnetworking.org

synapse


This tutorial is for users of Debian GNU/Linux who want to create their own synapse/matrix instance. The official documentation was solid and the Hack Liberty tutorial had many great insights. I have everything up and running now with nginx except for the MTA/email functionality. Although I originally intended to use apache, I could not get the ProxyPass for .well-known to work and it could not federate, although everything else worked fine. I will solve that issue later and post an addenda here when I do. For now, let me step you through what I did. First, the prerequisites are to set up A records for your domains, and adjust to your needs. I established gnulinux.club, matrix.gnulinux.club, element.gnulinux.club, and jitsi.gnulinux.club. After those cached, I then shelled into my VM and established ufw rules for ssh, http, https, matrix, and jitsi:

sudo ufw allow 22                
sudo ufw allow 80               
sudo ufw allow 443                               
sudo ufw allow 8448              
sudo ufw allow 10000/udp
sudo ufw allow 3478/udp
sudo ufw allow 5349/tcp  

You should also make sure that you have fail2ban installed and configured before proceeding. I would also recommend hardening your sshd_config file. Once those prerequisites are done, let's install nginx web server:

sudo apt install nginx
cd /etc/nginx/sites-enabled
cp default gnulinux.club
cp default matrix.gnulinux.club
cp default element.gnulinux.club

You will notice that I did not create a server block for jitsi becuase it's installer script will take care of this automatically later on. Once these are created, delete everything in the block file except the basic http block and make some small edits:

nano /etc/nginx/sites-enabled/gnulinux.club
<server_name gnulinux.club;>
<root /var/www/gnulinux.club/;>

The matrix.gnulinux.club block needs to have the proxy_pass directive set. To limit confusion, I have just provided the config directly. Also, note that web root is irrelevant for this block because it will not be serving any files directly but is merely sending requests back to matrix/localhost on port 8008.

nano /etc/nginx/sites-enabled/matrix.gnulinux.club

matrix.gnulinux.club

As for the element.gnulinux.club block (adjusted for your domain, of course), make sure to add a directory to the end of the block directory itself. This will help later when upgrades to Element come out, because you can just re-symlink the new update. We will do the symlink later when installing the package, for now:

nano /etc/nginx/sites-enabled/element.gnulinux.club
<server_name element.gnulinux.club;>
<root /var/www/element.gnulinux.club/element/;>
systemctl restart nginx

Conduct a url request on gnulinux.club (adjusted for you) to ensure that your domain resolves and nginx is functioning properly. Once that's confirmed, we can now set up Let's Encrypt certs:

sudo apt install python3-certbot-nginx
certbot --nginx -d gnulinux.club -d matrix.gnulinux.club -d element.gnulinux.club

Conduct another url request on gnulinux.club (adjusted for you) to ensure that your domain now has the TLS lock and redirected properly. Once that's setup, it is now time to install the matrix-synapse-py3 server. Always check the official documentation page for this, but as of this installation, that was done as follows:

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
<name of the server: gnulinux.club>
<report statistics yes>

Verify that the matrix-synapse server is running with ps -aux | grep synapse and then move on to federating the server. To federate the server, you can either create a srv DNS record, or leverage .well-known. I chose the latter, and did the following:

mkdir -p /var/www/gnulinux.club/.well-known/matrix
cd /var/www/gnulinux.club/.well-known/matrix/
nano server
<{ "m.server": "matrix.gnulinux.club:443" }>

Once that's created, run curl and make sure the output matches what you specified in the server file:

curl -L https://gnulinux.club/.well-known/matrix/server
{ "m.server": "matrix.gnulinux.club:443" }

As long as that functions, you can now move on to installing Element. That involves making a web root directory of the server block, creating a dedicated user that owns and downloads the latest package (always check official release page), symlinking the current package to the location specified in the server block, and lastly configuring the config.json file for your domain and particular instance. Here goes:

mkdir /var/www/element.gnulinux.club/
cd /var/www/elemenet.gnulinux.club/
sudo useradd -m elementweb
<password>
sudo --user elementweb wget https://github.com/vector-im/element-web/releases/download/v1.10.12/element-v1.10.12.tar.gz
sudo --user elementweb tar -xvf element-v1.10.12.tar.gz
rm element-v1.10.12.tar.gz
cd /var/www/elemenet.gnulinux.club/
ln -s element-v1.10.12/ element
cd /var/www/elemenet.gnulinux.club/element
cp config-sample.json config.jason
nano config.json
<"base_url": "https://matrix.gnulinux.club",>
<"server_name": "gnulinux.club">

Once Element is installed, you need to enable registration. Matrix no longer allows unchallenged / non-tokened registration without an explicit override, so after seeing how involved token auth was, I added a Google Captcha v2 instead.

sudo nano /etc/matrix-synapse/homserver.yaml
enable_registration: true [needs to be disabled when creating the db users from command line]
enable_registration_captcha: true
recaptcha_public_key: "yourmomspublickey"
recaptcha_private_key: "yourdadsprivatekey" #On Google, Turn verify origin off
registration_shared_secret: "yourcousinssharedsecret"
federation_client_minimum_tls_version: 1.2

At this stage, you could theoretically add a user through the web GUI, however the instance is still using sqlite3 and I preferred something more robust. For that reason, I waited and first created a proper database and dedicated database user, both for scaling and hardening purposes:

sudo apt install postgresql
sudo -u postgres bash
createuser --pwprompt synapseusr
createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapseusr synapsedb
exit

After recording the password you specified, make sure to configure homeserver.yaml to use the database instead of the default sqlite3 one.

sudo nano /etc/matrix-synapse/homeserver.yaml
<database:>
<name: psycopg2>
<txn_limit: 10000>
<args:>
  <user: synapseusr>
  <password: mommalovesU>
  <database: synapsedb>
  <host: localhost>
  <port: 5432>
  <cp_min: 5>
  <cp_max: 10>

After creating the database, inform synapse of how to reach it in the pg_hba.conf file as follows:

nano /etc/postgresql/13/main/pg_hba.conf
<host    synapsedb     synapseusr    ::1/128     md5>
sudo systemctl reload postgresql

Check that the configuration you established is functioning by restarting the service with systemctl restart matrix-synapse. As long as everything is functioning, you can now create an admin user. Temporarily comment out user registration on /etc/matrix-synapse/homeserver.yaml and restart the service again systemctl restart matrix-synapse in order to create the user. Note: the user creation will fail if you do not do this.

sudo -u postgres bash
register_new_matrix_user -c /etc/matrix-synapse/homeserver.yaml http://localhost:8008

Record the password you chose in a safe location, and then uncomment the user registration on /etc/matrix-synapse/homeserver.yaml again and once again restart the service systemctl restart matrix-synapse. This is required because you cannot manually create users while the enable_registration: true parameter is active. Likely, you will now want to also create a non-admin user for yourself, and the webgui is very convenient for this so just visit element.gnulinux.club and then register. Once you log in, make sure that you can access federated instances, spaces, and rooms. So long as everything is working, let's carry on with setting up jitsi:

Between these two debugging tools, you should be in good shape to get everything up and running! Okay, that took a lot to document but it's worth it. Also, if you are serving others in a public facing community, make sure you have appropriate backup scripts for this VM. Utilize rsync or similar to ensure you have those backups offsite as well as on site, and also provision a hot-spare in case your self-hosted or co-located hardware fails.

oemb1905 2022/11/20 01:58

computing/synapse.1669056022.txt.gz · Last modified: 2022/11/21 18:40 by oemb1905