Synapse matrix homeserver with systemd

I run a Matrix homeserver and until recently I was using the virtualenv + synctl method to start the service, which they describe in the README. What I really wanted was to put it into systemd so that I can start and stop it like any other system service.

There is a basic synapse.service file in the synapse repository for Arch Linux. It needs tweaking a bit to work with a virtualenv installation. In my case I have a user called synapse and all the homeserver files are in /home/synapse.

I created a file /etc/systemd/system/synapse.service:

[Unit]
Description=Synapse Matrix homeserver

[Service]
Type=simple
User=synapse
Group=synapse
WorkingDirectory=/home/synapse
ExecStart=/home/synapse/.synapse/bin/python -m synapse.app.homeserver --config-path=/home/synapse/homeserver.yaml
ExecStop=/home/synapse/.synapse/bin/synctl stop /home/synapse/homeserver.yaml

[Install]
WantedBy=multi-user.target

The main trick is to use the python associated with the virtual environment so that it can find the homeserver and all its dependencies. By running python directly systemd is able to track that the process is running – if you use synctl start it forks off into the background shortly after and systemd thinks it failed.

Then it’s just matter of:

# systemctl enable synapse
# systemctl start synapse