Content
Matrix homeserver setup on local machine using Python
Setup of Matrix homeserver
Running a Matrix homeserver (like Synapse) on localhost without DNS can be done for testing and development purposes.
Here's a general outline of the steps that needs to be followed:
-
Prerequisites:
-
Install Python: Synapse is written in Python, so make sure you have Python installed on your system.
-
Install pip: This is the Python package manager, which you'll use to install Synapse.
-
Configure Python virtual environment for all subsequent installations like this:
-
python3 -m venv path/to/venv
source path/to/venv/bin/activate
pip install package_name
-
Install Synapse:
You can install Synapse using pip. Open a terminal and run the following command:
pip install matrix-synapse
-
Configure Synapse:
Create a configuration file for Synapse. You can generate a basic configuration file using the following command:
python -m synapse.app.homeserver --server-name your.server.name --config-path /path/to/config
-
Replace
your.server.name
with a unique server name. Modify the generated configuration file (/path/to/config
) to your requirements.In your configuration file, you may want to set the
listeners
section to bind the server to your localhost IP (e.g., "127.0.0.1") on a specific port (e.g., 8008) to ensure that it only listens on your local machine. -
Create a Self-Signed Certificate:
For testing purposes, you can create a self-signed SSL certificate. This step is optional but recommended if you want to use HTTPS.You can create a self-signed certificate using tools like OpenSSL.
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -keyout server.key -out server.crt
-
Then, configure your Synapse server to use these certificates in your configuration file.
-
Database Configuration:
Configure the database settings in your Synapse configuration file. For Postgres follow these steps:- Install PostgresSQL client library
sudo apt install postgresql postgresql-contrib
- To interact with database use 'postgres' user profile
sudo -u postgres bash
- Then create a postgres user with username 'synapse_user'
createuser --pwprompt synapse_user
- Create a database for synapse home-server with database name 'synapse'
createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
- Exit from postgres
exit
-
Setup postgres database for synapse home-server. Un-comment and edit
listen_address
variable inpostgresql.conf
located at
/etc/postgresql/<postgres_version>/main/postgresql.conf
.
For version 15:
sudo nano /etc/postgresql/15/main/postgresql.conf
- Find the following line and edit as such:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = 'localhost' # what IP address(es) to listen on;
-
Save and exit.
-
Then edit
pg_hba.conf
file and add a line to enable password authentication so that 'synapse_user' can connect to
'synapse' database
sudo nano /etc/postgresql/15/main/pg_hba.conf
- Add the following line at the end of file
local synapse synapse_user md5
- So that it looks like
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
local synapse synapse_user md5
- Add the 'synapse config' of the above database to
config.yaml
, so it looks something like this
database:
name: psycopg2
args:
user: synapse_user
password: <pass>
database: synapse
host: localhost
cp_min: 5
cp_max: 10
- Install
psycopg2
in Python virtual environment
pip install psycopg2
pip install psycopg2-binary
-
Start the Homeserver:
To start the Synapse server, use the following command:
python -m synapse.app.homeserver --config-path /path/to/config
-
Access your Homeserver:
You can access your Matrix homeserver by using a Matrix client such as Element. Point your client to your localhost IP and the port you configured in thelisteners
section. For example,http://127.0.0.1:8008
. -
User Registration:
Register a new user on your homeserver using your Matrix client.
Remember that running a homeserver without DNS means you won't be able to access it from other devices on your network or the internet. It's suitable for local testing and development purposes. If you want to make your homeserver accessible outside your localhost, you'll need to configure DNS and network settings accordingly.