# 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:
1. **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:
```bash
python3 -m venv path/to/venv
source path/to/venv/bin/activate
pip install package_name
```
1. **Install Synapse**:
You can install Synapse using pip. Open a terminal and run the following command:
```bash
pip install matrix-synapse
```
1. **Configure Synapse**:
Create a configuration file for Synapse. You can generate a basic configuration file using the following command:
```bash
python -m synapse.app.homeserver --server-name your.server.name --config-path /path/to/config
```
1. 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.
2. **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.
```bash
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -keyout server.key -out server.crt
```
1. Then, configure your Synapse server to use these certificates in your configuration file.
2. **Database Configuration**:
Configure the database settings in your Synapse configuration file. For Postgres follow these steps:
* Install PostgresSQL client library
```bash
sudo apt install postgresql postgresql-contrib
```
* To interact with database use 'postgres' user profile
```bash
sudo -u postgres bash
```
* Then create a postgres user with username 'synapse\_user'
```bash
createuser --pwprompt synapse_user
```
* Create a database for synapse home-server with database name 'synapse'
```bash
createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
```
* Exit from postgres
```bash
exit
```
* Setup postgres database for synapse home-server. Un-comment and edit `listen_address` variable in `postgresql.conf`
located at `/etc/postgresql//main/postgresql.conf`.
For version 15:
```bash
sudo nano /etc/postgresql/15/main/postgresql.conf
```
* Find the following line and edit as such:
```text
#------------------------------------------------------------------------------
# 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
```bash
sudo nano /etc/postgresql/15/main/pg_hba.conf
```
* Add the following line at the end of file
```text
local synapse synapse_user md5
```
* So that it looks like
```text
# 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
```text
database:
name: psycopg2
args:
user: synapse_user
password:
database: synapse
host: localhost
cp_min: 5
cp_max: 10
```
* Install `psycopg2` in Python virtual environment
```python
pip install psycopg2
pip install psycopg2-binary
```
1. **Start the Homeserver**:
To start the Synapse server, use the following command:
```bash
python -m synapse.app.homeserver --config-path /path/to/config
```
1. **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 the `listeners` section. For example, `http://127.0.0.1:8008`.
2. **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.
## References
* [Matrix homeserver setup guide](https://medium.com/@dassomnath/setup-matrix-synapse-home-server-ba54e20f8290)
* [Matrix homeserver configuration guide](https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html)