Preduvjeti
Instalirajte razvojne alate
Instalirajte Synapse
Instalacija i konfiguracija PostgreSQL-a
Konfiguriranje Synapse
Postavljanje Let's Encrypt certifikata
Postavite Nginx uz Let's Encrypt
Postavljanje Systemd usluge
Korištenje Riot
Postavite Riot na vlastiti poslužitelj.
Matrix je otvoreni standardni komunikacijski protokol za decentraliziranu komunikaciju u stvarnom vremenu. Matrix je implementiran kao kućni poslužitelji koji se distribuiraju putem interneta; stoga ne postoji jedinstvena točka kontrole ili neuspjeha. Matrix pruža RESTful HTTP API za kreiranje i upravljanje distribuiranim poslužiteljima za razgovor koji uključuje slanje i primanje poruka, pozivanje i upravljanje članovima sobe za razgovor, održavanje korisničkih računa i pružanje naprednih značajki chata kao što su VoIP i video pozivi, itd. Matrix također uspostavlja sigurna sinkronizacija između kućnih poslužitelja koji su raspoređeni diljem svijeta.
Synapse je implementacija Matrix kućnog poslužitelja koju je napisao Matrix tim. Matrix ekosustav sastoji se od mreže mnogih federalnih kućnih poslužitelja raspoređenih diljem svijeta. Korisnik Matrixa koristi chat klijent za povezivanje s kućnim poslužiteljem, koji se zauzvrat povezuje na Matrix mrežu. Homeserver pohranjuje povijest razgovora i podatke za prijavu tog određenog korisnika.
Preduvjeti
- Instanca poslužitelja Vultr CentOS 7.
- Korisnik sudoa .
U ovom vodiču koristit ćemo se matrix.example.comkao naziv domene za Matrix Synapse. Zamijenite sva pojavljivanja matrix.example.comsa svojim stvarnim imenom domene koju želite koristiti za svoj Synapse kućni poslužitelj.
Ažurirajte svoj osnovni sustav pomoću vodiča Kako ažurirati CentOS 7 . Nakon što se vaš sustav ažurira, nastavite s instalacijom Pythona.
Matrix Synapse treba Python 2.7 za rad. Python 2.7 dolazi unaprijed instaliran u svim instancama poslužitelja CentOS. Možete provjeriti instaliranu verziju Pythona.
python -V
Trebali biste dobiti sličan izlaz.
[user@vultr ~]$ python -V
Python 2.7.5
Promjena zadane verzije Pythona može pokvariti YUM upravitelja spremišta. Međutim, ako želite najnoviju verziju Pythona, možete napraviti alternativnu instalaciju, bez zamjene zadanog Pythona.
Instalirajte pakete u Development toolsgrupi koji su potrebni za sastavljanje instalacijskih datoteka.
sudo yum groupinstall -y "Development tools"
Instalirajte još nekoliko potrebnih ovisnosti.
sudo yum -y install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel redhat-rpm-config python-virtualenv libffi-devel openssl-devel
Instalirajte Python pip. Pip je upravitelj ovisnosti za Python pakete.
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
Instalirajte Synapse
Stvorite virtualno okruženje za svoju Synapse aplikaciju. Python virtualno okruženje koristi se za stvaranje izoliranog virtualnog okruženja za Python projekt. Virtualno okruženje sadrži vlastite instalacijske direktorije i ne dijeli biblioteke s globalnim i drugim virtualnim okruženjima.
sudo virtualenv -p python2.7 /opt/synapse
Omogućite vlasništvo nad imenikom trenutnom korisniku.
sudo chown -R $USER:$USER /opt/synapse/
Sada aktivirajte virtualno okruženje.
source /opt/synapse/bin/activate
Provjerite imate li najnoviju verziju pipi setuptools.
pip install --upgrade pip
pip install --upgrade setuptools
Instalirajte najnoviju verziju Synapsea koristeći pip.
pip install https://github.com/matrix-org/synapse/tarball/master
Gornjoj naredbi trebat će neko vrijeme da se izvrši dok povlači i instalira najnoviju verziju Synapsea i sve ovisnosti iz Github repozitorija.
Instalacija i konfiguracija PostgreSQL-a
Synapse koristi SQLite kao zadanu bazu podataka. SQLite pohranjuje podatke u bazu podataka koja se čuva kao ravna datoteka na disku. Korištenje SQLite-a je vrlo jednostavno, ali se ne preporučuje za proizvodnju jer je vrlo sporo u usporedbi s PostgreSQL-om.
PostgreSQL je objektni relacijski sustav baze podataka. Morat ćete dodati PostgreSQL repozitorij u svoj sustav, jer aplikacija nije dostupna u zadanom YUM spremištu.
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
Instalirajte PostgreSQL poslužitelj baze podataka.
sudo yum -y install postgresql96-server postgresql96-contrib
Inicijalizirajte bazu podataka.
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Uredite da /var/lib/pgsql/9.6/data/pg_hba.confbiste omogućili provjeru autentičnosti temeljenu na MD5.
sudo nano /var/lib/pgsql/9.6/data/pg_hba.conf
Pronađite sljedeće retke i promijenite peeru trusti idnetu md5.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 idnet
# IPv6 local connections:
host all all ::1/128 idnet
Nakon ažuriranja, konfiguracija bi trebala izgledati ovako.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Pokrenite PostgreSQL poslužitelj i omogućite mu automatsko pokretanje pri pokretanju.
sudo systemctl start postgresql-9.6
sudo systemctl enable postgresql-9.6
Promijenite lozinku za zadanog korisnika PostgreSQL.
sudo passwd postgres
Prijaviti se.
sudo su - postgres
Stvorite novog PostgreSQL korisnika za Synapse.
createuser synapse
PostgreSQL pruža psqlljusku za pokretanje upita u bazi podataka. Prebacite se na ljusku PostgreSQL pokretanjem.
psql
Postavite lozinku za novostvorenog korisnika za Synapse bazu podataka.
ALTER USER synapse WITH ENCRYPTED password 'DBPassword';
Zamijenite DBPasswordjakom lozinkom i zabilježite je jer ćemo je kasnije koristiti. Napravite novu bazu podataka za PostgreSQL bazu podataka.
CREATE DATABASE synapse ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER synapse;
Izađite iz psqlljuske.
\q
Prebacite se na sudokorisnika s trenutnog postgreskorisnika.
exit
Također ćete morati instalirati pakete potrebne da Synapse komunicira s PostgreSQL poslužiteljem baze podataka.
sudo yum -y install postgresql-devel libpqxx-devel.x86_64
source /opt/synapse/bin/activate
pip install psycopg2
Konfiguriranje Synapse
Synapse zahtijeva konfiguracijsku datoteku prije nego što se može pokrenuti. Konfiguracijska datoteka pohranjuje postavke poslužitelja. Prijeđite na virtualno okruženje i generirajte konfiguraciju za Synapse.
source /opt/synapse/bin/activate
cd /opt/synapse
python -m synapse.app.homeserver --server-name matrix.example.com --config-path homeserver.yaml --generate-config --report-stats=yes
Zamijenite matrix.example.comsvojim stvarnim imenom domene i provjerite je li naziv poslužitelja razlučiv na IP adresu vaše Vultr instance. Omogućite --report-stats=yesako želite da poslužitelji generiraju izvješća, omogućite --report-stats=noda onemogućite generiranje izvješća i statistike.
Trebali biste vidjeti sličan izlaz.
(synapse)[user@vultr synapse]$ python -m synapse.app.homeserver --server-name matrix.example.com --config-path homeserver.yaml --generate-config --report-stats=yes
A config file has been generated in 'homeserver.yaml' for server name 'matrix.example.com' with corresponding SSL keys and self-signed certificates. Please review this file and customise it to your needs.
If this server name is incorrect, you will need to regenerate the SSL certificates
Prema zadanim postavkama, homeserver.yamlje konfiguriran za korištenje SQLite baze podataka. Moramo ga modificirati kako bismo koristili PostgreSQL bazu podataka koju smo kreirali ranije.
Uredite novostvoreni homeserver.yaml.
nano homeserver.yaml
Pronađite postojeću konfiguraciju baze podataka koja koristi SQLite3. Komentirajte retke kao što je prikazano u nastavku. Također, dodajte novu konfiguraciju baze podataka za PostgreSQL. Provjerite koristite li ispravne vjerodajnice baze podataka.
# Database configuration
#database:
# The database engine name
#name: "sqlite3"
# Arguments to pass to the engine
#args:
# Path to the database
#database: "/opt/synapse/homeserver.db"
database:
name: psycopg2
args:
user: synapse
password: DBPassword
database: synapse
host: localhost
cp_min: 5
cp_max: 10
Registration of a new user from a web interface is disabled by default. To enable registration, you can set enable_registration to True. You can also set a secret registration key, which allows anyone to register who has the secret key, even if registration is disabled.
enable_registration: False
registration_shared_secret: "YPPqCPYqCQ-Rj,ws~FfeLS@maRV9vz5MnnV^r8~pP.Q6yNBDG;"
Save the file and exit from the editor. Now you will need to register your first user. Before you can register a new user, though, you will need to start the application first.
source /opt/synapse/bin/activate && cd /opt/synapse
synctl start
You should see the following lines.
2017-09-05 11:10:41,921 - twisted - 131 - INFO - - SynapseSite starting on 8008
2017-09-05 11:10:41,921 - twisted - 131 - INFO - - Starting factory <synapse.http.site.SynapseSite instance at 0x44bbc68>
2017-09-05 11:10:41,921 - synapse.app.homeserver - 201 - INFO - - Synapse now listening on port 8008
2017-09-05 11:10:41,922 - synapse.app.homeserver - 442 - INFO - - Scheduling stats reporting for 3 hour intervals
started synapse.app.homeserver('homeserver.yaml')
Register a new Matrix user.
register_new_matrix_user -c homeserver.yaml https://localhost:8448
You should see the following.
(synapse)[user@vultr synapse]$ register_new_matrix_user -c homeserver.yaml https://localhost:8448
New user localpart [user]: admin
Password:
Confirm password:
Make admin [no]: yes
Sending registration request...
Success.
Finally, before you can use the Homeserver, you will need to allow port 8448 through the Firewall. Port 8448 is used as the secured federation port. Homeservers use this port to communicate with each other securely. You can also use the built-in Matrix web chat client through this port.
sudo firewall-cmd --permanent --zone=public --add-port=8448/tcp
sudo firewall-cmd --reload
You can now log in to the Matrix web chat client by going to https://matrix.example.com:8448 through your favorite browser. You will see a warning about the SSL certificate as the certificates used are self-signed. We will not use this web chat client as it is outdated and not maintained anymore. Just try to check if you can log in using the user account you just created.
Setting up Let's Encrypt Certificates
Instead of using a self-signed certificate for securing federation port, we can use Let's Encrypt free SSL. Let's Encrypt free SSL can be obtained through the official Let's Encrypt client called Certbot.
Install Certbot.
sudo yum -y install certbot
Adjust your firewall setting to allow the standard HTTP and HTTPS ports through the firewall. Certbot needs to make an HTTP connection to verify the domain authority.
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
To obtain certificates from Let's Encrypt CA, you must ensure that the domain for which you wish to generate the certificates is pointed towards the server. If it is not, then make the necessary changes to the DNS records of your domain and wait for the DNS to propagate before making the certificate request again. Certbot checks the domain authority before providing the certificates.
Now use the built-in web server in Certbot to generate the certificates for your domain.
sudo certbot certonly --standalone -d matrix.example.com
The generated certificates are likely to be stored in /etc/letsencrypt/live/matrix.example.com/. The SSL certificate will be stored as fullchain.pem and the private key will be stored as privkey.pem.
Copy the certificates.
sudo cp /etc/letsencrypt/live/matrix.example.com/fullchain.pem /opt/synapse/letsencrypt-fullchain.pem
sudo cp /etc/letsencrypt/live/matrix.example.com/privkey.pem /opt/synapse/letsencrypt-privkey.pem
You will need to change the path to the certificates and keys from the homeserver.yaml file. Edit the configuration.
nano /opt/synapse/homeserver.yaml
Find the following lines and modify the path.
tls_certificate_path: "/opt/synapse/letsencrypt-fullchain.pem"
# PEM encoded private key for TLS
tls_private_key_path: "/opt/synapse/letsencrypt-privkey.pem"
Save the file and exit from the editor. Restart the Synapse server so that the changes can take effect.
source /opt/synapse/bin/activate && cd /opt/synapse
synctl restart
Let's Encrypt certificates are due to expire in 90 days, so it is recommended that you setup auto renewal for the certificates using cron jobs. Cron is a system service which is used to run periodic tasks.
Create a new script to renew certificates and copy the renewed certificates to the Synapse directory.
sudo nano /opt/renew-letsencypt.sh
Populate the file.
#!/bin/sh
/usr/bin/certbot renew --quiet --nginx
cp /etc/letsencrypt/live/matrix.example.com/fullchain.pem /opt/synapse/letsencrypt-fullchain.pem
cp /etc/letsencrypt/live/matrix.example.com/privkey.pem /opt/synapse/letsencrypt-privkey.pem
Provide the execution permission.
sudo chmod +x /opt/renew-letsencypt.sh
Open the cron job file.
sudo crontab -e
Add the following line at the end of the file.
30 5 * * 1 /opt/renew-letsencypt.sh
The above cron job will run every Monday at 5:30 AM. If the certificate is due to expire, it will automatically renew them.
Now you can visit https://matrix.example.com:8448. You will see that there is no SSL warning before connection.
Setup Nginx With Let's Encrypt
Apart from the secured federation port 8448, Synapse also listens to the unsecured client port 8008. We will now configure Nginx as a reverse proxy to the Synapse application.
sudo yum -y install nginx
Create a new configuration file.
sudo nano /etc/nginx/conf.d/synapse.conf
Populate the file with the following content.
server {
listen 80;
server_name matrix.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name matrix.example.com;
ssl_certificate /etc/letsencrypt/live/matrix.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matrix.example.com/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/synapse.access.log;
location /_matrix {
proxy_pass http://localhost:8008;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Restart and enable Nginx to automatically start at boot time.
sudo systemctl restart nginx
sudo systemctl enable nginx
Konačno, možete provjeriti može li se Synapseu pristupiti putem obrnutog proxyja.
curl https://matrix.example.com/_matrix/key/v2/server/auto
Trebali biste dobiti sličan izlaz.
[user@vultr ~]$ curl https://matrix.example.com/_matrix/key/v2/server/auto
{"old_verify_keys":{},"server_name":"matrix.example.com","signatures":{"matrix.example.com":{"ed25519:a_ffMf":"T/Uq/UN5vyc4w7v0azALjPIJeZx1vQ+HC6ohUGkTSqiFI4WI/ojGpb2763arwSSQLr/tP/2diCi1KLU2DEnOCQ"}},"tls_fingerprints":[{"sha256":"eorhQj/kubI2PEQZyBZvGV7K1x3EcQ7j/AO2MtZMplw"}],"valid_until_ts":1504876080512,"verify_keys":{"ed25519:a_ffMf":{"key":"Gc1hxkpPmQv71Cvjyk+uzR5UtrpmgV/UwlsLtosawEs"}}}
Postavljanje Systemd usluge
Preporuča se korištenje usluge Systemd za upravljanje procesom poslužitelja Synapse. Korištenje Systemd će osigurati da se poslužitelj automatski pokrene pri pokretanju sustava i kvarovima.
Izradite novu Systemd servisnu datoteku.
sudo nano /etc/systemd/system/matrix-synapse.service
Popunite datoteku.
[Unit]
Description=Matrix Synapse service
After=network.target
[Service]
Type=forking
WorkingDirectory=/opt/synapse/
ExecStart=/opt/synapse/bin/synctl start
ExecStop=/opt/synapse/bin/synctl stop
ExecReload=/opt/synapse/bin/synctl restart
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=synapse
[Install]
WantedBy=multi-user.target
Sada možete brzo pokrenuti Synapse poslužitelj.
sudo systemctl start matrix-synapse
Za zaustavljanje ili ponovno pokretanje poslužitelja pomoću sljedećih naredbi.
sudo systemctl stop matrix-synapse
sudo systemctl restart matrix-synapse
Možete provjeriti status usluge.
sudo systemctl status matrix-synapse
Korištenje Riot
Matrix Synapse poslužitelj je sada instaliran i konfiguriran na vašem poslužitelju. Budući da je ugrađeni web klijent za Matrix zastario, možete birati između raznih klijentskih aplikacija dostupnih za chat. Riot je najpopularniji chat klijent koji je dostupan na gotovo svim platformama. Možete koristiti hostiranu verziju Riotovog web chat klijenta ili također možete ugostiti njegovu kopiju na vlastitom poslužitelju. Osim toga, možete koristiti i Riotove desktop i mobilne chat klijente koji su dostupni za Windows, Mac, Linux, IOS i Android.
Ako želite ugostiti vlastitu kopiju Riot web klijenta, dalje možete pročitati upute za instalaciju Riot na vaš poslužitelj. Za hostirani, desktop i mobilni klijent, možete koristiti svoje korisničko ime i lozinku za prijavu izravno na svoj kućni poslužitelj. Samo odaberite my Matrix IDs padajućeg izbornika Sign Inopcije i unesite korisničko ime i lozinku koje ste kreirali tijekom registracije novog korisnika. Kliknite Custom serveri upotrijebite naziv domene vaše instance Synapse. Kako smo već konfigurirali Nginx, možemo samo koristiti https://matrix.example.comkao kućni poslužitelj i https://matrix.orgkao URL poslužitelja identiteta.
Primjer prijave u Riot
Postavite Riot na vlastiti poslužitelj.
Riot je također otvorenog koda i besplatan za hostiranje na vlastitom poslužitelju. Ne zahtijeva nikakvu bazu podataka ili ovisnosti. Kako već imamo pokrenut Nginx poslužitelj, možemo ga ugostiti na istom poslužitelju.
The domain or subdomain you are using for Synapse and Riot must be different to avoid cross-site scripting. However, you can use two subdomains of the same domain. In this tutorial, we will be using riot.example.com as the domain for the Riot application. Replace all occurrence of riot.example.com with your actual domain or subdomain for the Riot application.
Preuzmite Riot na svoj server.
cd /opt/
sudo wget https://github.com/vector-im/riot-web/releases/download/v0.12.3/riot-v0.12.3.tar.gz
Poveznicu na najnoviju verziju uvijek možete pronaći na Riot's Githubu .
Ekstrahirajte arhivu.
sudo tar -xzf riot-v*.tar.gz
Preimenujte imenik radi lakšeg rukovanja.
sudo mv riot-v*/ riot/
Budući da smo Certbot već instalirali, certifikate možemo generirati izravno. Provjerite je li domena ili poddomena koju koristite usmjerena prema poslužitelju.
sudo systemctl stop nginx
sudo certbot certonly --standalone -d riot.example.com
Generirani certifikati će vjerojatno biti pohranjeni u /etc/letsencrypt/live/riot.example.com/imeniku.
Napravite virtualni host za aplikaciju Riot.
sudo nano /etc/nginx/conf.d/riot.conf
Popunite datoteku.
server {
listen 80;
server_name riot.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name riot.example.com;
ssl_certificate /etc/letsencrypt/live/riot.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/riot.example.com/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
root /opt/riot;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/riot.access.log;
}
Kopirajte uzorak konfiguracijske datoteke.
sudo cp /opt/riot/config.sample.json /opt/riot/config.json
Sada uredite konfiguracijsku datoteku da napravite nekoliko promjena.
sudo nano /opt/riot/config.json
Pronađite sljedeće retke.
"default_hs_url": "https://matrix.org",
"default_is_url": "https://vector.im",
Zamijenite vrijednost URL-a zadanog kućnog poslužitelja URL-om vašeg Matrix poslužitelja. Za URL poslužitelja identiteta možete koristiti zadanu opciju ili također možete dati njegovu vrijednost poslužitelju identiteta Matrix, a to je https://matrix.org.
"default_hs_url": "https://matrix.example.com",
"default_is_url": "https://matrix.org",
Spremite datoteku i izađite. Omogućite vlasništvo nad datotekama korisniku Nginxa.
sudo chown -R nginx:nginx /opt/riot/
Ponovno pokrenite Nginx.
sudo systemctl restart nginx
Riotu možete pristupiti na https://riot.example.com. Sada se možete prijaviti koristeći korisničko ime i lozinku koje ste prethodno kreirali. Možete se povezati pomoću zadanog poslužitelja jer smo već promijenili zadani Matrix poslužitelj za našu aplikaciju.
Sada imate Matrix Synapse kućni poslužitelj koji radi. Također imate hostiranu kopiju Riot-a, koju možete koristiti za slanje poruke drugim ljudima koristeći njihov Matrix ID, e-mail ili broj mobitela. Započnite stvaranjem sobe za razgovor na svom poslužitelju i pozovite svoje prijatelje na Matrixu da se pridruže sobi za razgovor koju ste kreirali.