README.rst 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. .. contents::
  2. Introduction
  3. ============
  4. Matrix is an ambitious new ecosystem for open federated Instant Messaging and
  5. VoIP. The basics you need to know to get up and running are:
  6. - Everything in Matrix happens in a room. Rooms are distributed and do not
  7. exist on any single server. Rooms can be located using convenience aliases
  8. like ``#matrix:matrix.org`` or ``#test:localhost:8448``.
  9. - Matrix user IDs look like ``@matthew:matrix.org`` (although in the future
  10. you will normally refer to yourself and others using a third party identifier
  11. (3PID): email address, phone number, etc rather than manipulating Matrix user IDs)
  12. The overall architecture is::
  13. client <----> homeserver <=====================> homeserver <----> client
  14. https://somewhere.org/_matrix https://elsewhere.net/_matrix
  15. ``#matrix:matrix.org`` is the official support room for Matrix, and can be
  16. accessed by any client from https://matrix.org/docs/projects/try-matrix-now.html or
  17. via IRC bridge at irc://irc.freenode.net/matrix.
  18. Synapse is currently in rapid development, but as of version 0.5 we believe it
  19. is sufficiently stable to be run as an internet-facing service for real usage!
  20. About Matrix
  21. ============
  22. Matrix specifies a set of pragmatic RESTful HTTP JSON APIs as an open standard,
  23. which handle:
  24. - Creating and managing fully distributed chat rooms with no
  25. single points of control or failure
  26. - Eventually-consistent cryptographically secure synchronisation of room
  27. state across a global open network of federated servers and services
  28. - Sending and receiving extensible messages in a room with (optional)
  29. end-to-end encryption[1]
  30. - Inviting, joining, leaving, kicking, banning room members
  31. - Managing user accounts (registration, login, logout)
  32. - Using 3rd Party IDs (3PIDs) such as email addresses, phone numbers,
  33. Facebook accounts to authenticate, identify and discover users on Matrix.
  34. - Placing 1:1 VoIP and Video calls
  35. These APIs are intended to be implemented on a wide range of servers, services
  36. and clients, letting developers build messaging and VoIP functionality on top
  37. of the entirely open Matrix ecosystem rather than using closed or proprietary
  38. solutions. The hope is for Matrix to act as the building blocks for a new
  39. generation of fully open and interoperable messaging and VoIP apps for the
  40. internet.
  41. Synapse is a reference "homeserver" implementation of Matrix from the core
  42. development team at matrix.org, written in Python/Twisted. It is intended to
  43. showcase the concept of Matrix and let folks see the spec in the context of a
  44. codebase and let you run your own homeserver and generally help bootstrap the
  45. ecosystem.
  46. In Matrix, every user runs one or more Matrix clients, which connect through to
  47. a Matrix homeserver. The homeserver stores all their personal chat history and
  48. user account information - much as a mail client connects through to an
  49. IMAP/SMTP server. Just like email, you can either run your own Matrix
  50. homeserver and control and own your own communications and history or use one
  51. hosted by someone else (e.g. matrix.org) - there is no single point of control
  52. or mandatory service provider in Matrix, unlike WhatsApp, Facebook, Hangouts,
  53. etc.
  54. We'd like to invite you to join #matrix:matrix.org (via
  55. https://matrix.org/docs/projects/try-matrix-now.html), run a homeserver, take a look
  56. at the `Matrix spec <https://matrix.org/docs/spec>`_, and experiment with the
  57. `APIs <https://matrix.org/docs/api>`_ and `Client SDKs
  58. <https://matrix.org/docs/projects/try-matrix-now.html#client-sdks>`_.
  59. Thanks for using Matrix!
  60. [1] End-to-end encryption is currently in beta: `blog post <https://matrix.org/blog/2016/11/21/matrixs-olm-end-to-end-encryption-security-assessment-released-and-implemented-cross-platform-on-riot-at-last>`_.
  61. Synapse Installation
  62. ====================
  63. Synapse is the reference Python/Twisted Matrix homeserver implementation.
  64. System requirements:
  65. - POSIX-compliant system (tested on Linux & OS X)
  66. - Python 3.5, 3.6, 3.7, or 2.7
  67. - At least 1GB of free RAM if you want to join large public rooms like #matrix:matrix.org
  68. Installing from source
  69. ----------------------
  70. (Prebuilt packages are available for some platforms - see `Platform-Specific
  71. Instructions`_.)
  72. Synapse is written in Python but some of the libraries it uses are written in
  73. C. So before we can install Synapse itself we need a working C compiler and the
  74. header files for Python C extensions.
  75. Installing prerequisites on Ubuntu or Debian::
  76. sudo apt-get install build-essential python3-dev libffi-dev \
  77. python-pip python-setuptools sqlite3 \
  78. libssl-dev python-virtualenv libjpeg-dev libxslt1-dev
  79. Installing prerequisites on ArchLinux::
  80. sudo pacman -S base-devel python python-pip \
  81. python-setuptools python-virtualenv sqlite3
  82. Installing prerequisites on CentOS 7 or Fedora 25::
  83. sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel \
  84. lcms2-devel libwebp-devel tcl-devel tk-devel redhat-rpm-config \
  85. python-virtualenv libffi-devel openssl-devel
  86. sudo yum groupinstall "Development Tools"
  87. Installing prerequisites on Mac OS X::
  88. xcode-select --install
  89. sudo easy_install pip
  90. sudo pip install virtualenv
  91. brew install pkg-config libffi
  92. Installing prerequisites on Raspbian::
  93. sudo apt-get install build-essential python3-dev libffi-dev \
  94. python-pip python-setuptools sqlite3 \
  95. libssl-dev python-virtualenv libjpeg-dev
  96. Installing prerequisites on openSUSE::
  97. sudo zypper in -t pattern devel_basis
  98. sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \
  99. python-devel libffi-devel libopenssl-devel libjpeg62-devel
  100. Installing prerequisites on OpenBSD::
  101. doas pkg_add python libffi py-pip py-setuptools sqlite3 py-virtualenv \
  102. libxslt jpeg
  103. To install the Synapse homeserver run::
  104. mkdir -p ~/synapse
  105. virtualenv -p python3 ~/synapse/env
  106. source ~/synapse/env/bin/activate
  107. pip install --upgrade pip
  108. pip install --upgrade setuptools
  109. pip install matrix-synapse[all]
  110. This installs Synapse, along with the libraries it uses, into a virtual
  111. environment under ``~/synapse/env``. Feel free to pick a different directory
  112. if you prefer.
  113. This Synapse installation can then be later upgraded by using pip again with the
  114. update flag::
  115. source ~/synapse/env/bin/activate
  116. pip install -U matrix-synapse[all]
  117. In case of problems, please see the _`Troubleshooting` section below.
  118. There is an offical synapse image available at
  119. https://hub.docker.com/r/matrixdotorg/synapse/tags/ which can be used with
  120. the docker-compose file available at `contrib/docker <contrib/docker>`_. Further information on
  121. this including configuration options is available in the README on
  122. hub.docker.com.
  123. Alternatively, Andreas Peters (previously Silvio Fricke) has contributed a
  124. Dockerfile to automate a synapse server in a single Docker image, at
  125. https://hub.docker.com/r/avhost/docker-matrix/tags/
  126. Slavi Pantaleev has created an Ansible playbook,
  127. which installs the offical Docker image of Matrix Synapse
  128. along with many other Matrix-related services (Postgres database, riot-web, coturn, mxisd, SSL support, etc.).
  129. For more details, see
  130. https://github.com/spantaleev/matrix-docker-ansible-deploy
  131. Configuring Synapse
  132. -------------------
  133. Before you can start Synapse, you will need to generate a configuration
  134. file. To do this, run (in your virtualenv, as before)::
  135. cd ~/synapse
  136. python -m synapse.app.homeserver \
  137. --server-name my.domain.name \
  138. --config-path homeserver.yaml \
  139. --generate-config \
  140. --report-stats=[yes|no]
  141. ... substituting an appropriate value for ``--server-name``. The server name
  142. determines the "domain" part of user-ids for users on your server: these will
  143. all be of the format ``@user:my.domain.name``. It also determines how other
  144. matrix servers will reach yours for `Federation`_. For a test configuration,
  145. set this to the hostname of your server. For a more production-ready setup, you
  146. will probably want to specify your domain (``example.com``) rather than a
  147. matrix-specific hostname here (in the same way that your email address is
  148. probably ``user@example.com`` rather than ``user@email.example.com``) - but
  149. doing so may require more advanced setup - see `Setting up
  150. Federation`_. Beware that the server name cannot be changed later.
  151. This command will generate you a config file that you can then customise, but it will
  152. also generate a set of keys for you. These keys will allow your Home Server to
  153. identify itself to other Home Servers, so don't lose or delete them. It would be
  154. wise to back them up somewhere safe. (If, for whatever reason, you do need to
  155. change your Home Server's keys, you may find that other Home Servers have the
  156. old key cached. If you update the signing key, you should change the name of the
  157. key in the ``<server name>.signing.key`` file (the second word) to something
  158. different. See `the spec`__ for more information on key management.)
  159. .. __: `key_management`_
  160. The default configuration exposes two HTTP ports: 8008 and 8448. Port 8008 is
  161. configured without TLS; it should be behind a reverse proxy for TLS/SSL
  162. termination on port 443 which in turn should be used for clients. Port 8448
  163. is configured to use TLS with a self-signed certificate. If you would like
  164. to do initial test with a client without having to setup a reverse proxy,
  165. you can temporarly use another certificate. (Note that a self-signed
  166. certificate is fine for `Federation`_). You can do so by changing
  167. ``tls_certificate_path``, ``tls_private_key_path`` and ``tls_dh_params_path``
  168. in ``homeserver.yaml``; alternatively, you can use a reverse-proxy, but be sure
  169. to read `Using a reverse proxy with Synapse`_ when doing so.
  170. Apart from port 8448 using TLS, both ports are the same in the default
  171. configuration.
  172. Registering a user
  173. ------------------
  174. You will need at least one user on your server in order to use a Matrix
  175. client. Users can be registered either `via a Matrix client`__, or via a
  176. commandline script.
  177. .. __: `client-user-reg`_
  178. To get started, it is easiest to use the command line to register new users::
  179. $ source ~/synapse/env/bin/activate
  180. $ synctl start # if not already running
  181. $ register_new_matrix_user -c homeserver.yaml https://localhost:8448
  182. New user localpart: erikj
  183. Password:
  184. Confirm password:
  185. Make admin [no]:
  186. Success!
  187. This process uses a setting ``registration_shared_secret`` in
  188. ``homeserver.yaml``, which is shared between Synapse itself and the
  189. ``register_new_matrix_user`` script. It doesn't matter what it is (a random
  190. value is generated by ``--generate-config``), but it should be kept secret, as
  191. anyone with knowledge of it can register users on your server even if
  192. ``enable_registration`` is ``false``.
  193. Setting up a TURN server
  194. ------------------------
  195. For reliable VoIP calls to be routed via this homeserver, you MUST configure
  196. a TURN server. See `<docs/turn-howto.rst>`_ for details.
  197. Running Synapse
  198. ===============
  199. To actually run your new homeserver, pick a working directory for Synapse to
  200. run (e.g. ``~/synapse``), and::
  201. cd ~/synapse
  202. source env/bin/activate
  203. synctl start
  204. Connecting to Synapse from a client
  205. ===================================
  206. The easiest way to try out your new Synapse installation is by connecting to it
  207. from a web client. The easiest option is probably the one at
  208. https://riot.im/app. You will need to specify a "Custom server" when you log on
  209. or register: set this to ``https://domain.tld`` if you setup a reverse proxy
  210. following the recommended setup, or ``https://localhost:8448`` - remember to specify the
  211. port (``:8448``) if not ``:443`` unless you changed the configuration. (Leave the identity
  212. server as the default - see `Identity servers`_.)
  213. If using port 8448 you will run into errors until you accept the self-signed
  214. certificate. You can easily do this by going to ``https://localhost:8448``
  215. directly with your browser and accept the presented certificate. You can then
  216. go back in your web client and proceed further.
  217. If all goes well you should at least be able to log in, create a room, and
  218. start sending messages.
  219. .. _`client-user-reg`:
  220. Registering a new user from a client
  221. ------------------------------------
  222. By default, registration of new users via Matrix clients is disabled. To enable
  223. it, specify ``enable_registration: true`` in ``homeserver.yaml``. (It is then
  224. recommended to also set up CAPTCHA - see `<docs/CAPTCHA_SETUP.rst>`_.)
  225. Once ``enable_registration`` is set to ``true``, it is possible to register a
  226. user via `riot.im <https://riot.im/app/#/register>`_ or other Matrix clients.
  227. Your new user name will be formed partly from the ``server_name`` (see
  228. `Configuring synapse`_), and partly from a localpart you specify when you
  229. create the account. Your name will take the form of::
  230. @localpart:my.domain.name
  231. (pronounced "at localpart on my dot domain dot name").
  232. As when logging in, you will need to specify a "Custom server". Specify your
  233. desired ``localpart`` in the 'User name' box.
  234. Security Note
  235. =============
  236. Matrix serves raw user generated data in some APIs - specifically the `content
  237. repository endpoints <https://matrix.org/docs/spec/client_server/latest.html#get-matrix-media-r0-download-servername-mediaid>`_.
  238. Whilst we have tried to mitigate against possible XSS attacks (e.g.
  239. https://github.com/matrix-org/synapse/pull/1021) we recommend running
  240. matrix homeservers on a dedicated domain name, to limit any malicious user generated
  241. content served to web browsers a matrix API from being able to attack webapps hosted
  242. on the same domain. This is particularly true of sharing a matrix webclient and
  243. server on the same domain.
  244. See https://github.com/vector-im/riot-web/issues/1977 and
  245. https://developer.github.com/changes/2014-04-25-user-content-security for more details.
  246. Platform-Specific Instructions
  247. ==============================
  248. Debian
  249. ------
  250. Matrix provides official Debian packages via apt from https://matrix.org/packages/debian/.
  251. Note that these packages do not include a client - choose one from
  252. https://matrix.org/docs/projects/try-matrix-now.html (or build your own with one of our SDKs :)
  253. Fedora
  254. ------
  255. Synapse is in the Fedora repositories as ``matrix-synapse``::
  256. sudo dnf install matrix-synapse
  257. Oleg Girko provides Fedora RPMs at
  258. https://obs.infoserver.lv/project/monitor/matrix-synapse
  259. OpenSUSE
  260. --------
  261. Synapse is in the OpenSUSE repositories as ``matrix-synapse``::
  262. sudo zypper install matrix-synapse
  263. SUSE Linux Enterprise Server
  264. ----------------------------
  265. Unofficial package are built for SLES 15 in the openSUSE:Backports:SLE-15 repository at
  266. https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/
  267. ArchLinux
  268. ---------
  269. The quickest way to get up and running with ArchLinux is probably with the community package
  270. https://www.archlinux.org/packages/community/any/matrix-synapse/, which should pull in most of
  271. the necessary dependencies.
  272. pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 )::
  273. sudo pip install --upgrade pip
  274. If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  275. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  276. compile it under the right architecture. (This should not be needed if
  277. installing under virtualenv)::
  278. sudo pip uninstall py-bcrypt
  279. sudo pip install py-bcrypt
  280. FreeBSD
  281. -------
  282. Synapse can be installed via FreeBSD Ports or Packages contributed by Brendan Molloy from:
  283. - Ports: ``cd /usr/ports/net-im/py-matrix-synapse && make install clean``
  284. - Packages: ``pkg install py27-matrix-synapse``
  285. OpenBSD
  286. -------
  287. There is currently no port for OpenBSD. Additionally, OpenBSD's security
  288. settings require a slightly more difficult installation process.
  289. 1) Create a new directory in ``/usr/local`` called ``_synapse``. Also, create a
  290. new user called ``_synapse`` and set that directory as the new user's home.
  291. This is required because, by default, OpenBSD only allows binaries which need
  292. write and execute permissions on the same memory space to be run from
  293. ``/usr/local``.
  294. 2) ``su`` to the new ``_synapse`` user and change to their home directory.
  295. 3) Create a new virtualenv: ``virtualenv -p python2.7 ~/.synapse``
  296. 4) Source the virtualenv configuration located at
  297. ``/usr/local/_synapse/.synapse/bin/activate``. This is done in ``ksh`` by
  298. using the ``.`` command, rather than ``bash``'s ``source``.
  299. 5) Optionally, use ``pip`` to install ``lxml``, which Synapse needs to parse
  300. webpages for their titles.
  301. 6) Use ``pip`` to install this repository: ``pip install matrix-synapse``
  302. 7) Optionally, change ``_synapse``'s shell to ``/bin/false`` to reduce the
  303. chance of a compromised Synapse server being used to take over your box.
  304. After this, you may proceed with the rest of the install directions.
  305. NixOS
  306. -----
  307. Robin Lambertz has packaged Synapse for NixOS at:
  308. https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix
  309. Windows Install
  310. ---------------
  311. If you wish to run or develop Synapse on Windows, the Windows Subsystem For
  312. Linux provides a Linux environment on Windows 10 which is capable of using the
  313. Debian, Fedora, or source installation methods. More information about WSL can
  314. be found at https://docs.microsoft.com/en-us/windows/wsl/install-win10 for
  315. Windows 10 and https://docs.microsoft.com/en-us/windows/wsl/install-on-server
  316. for Windows Server.
  317. Troubleshooting
  318. ===============
  319. Troubleshooting Installation
  320. ----------------------------
  321. Synapse requires pip 8 or later, so if your OS provides too old a version you
  322. may need to manually upgrade it::
  323. sudo pip install --upgrade pip
  324. Installing may fail with ``Could not find any downloads that satisfy the requirement pymacaroons-pynacl (from matrix-synapse==0.12.0)``.
  325. You can fix this by manually upgrading pip and virtualenv::
  326. sudo pip install --upgrade virtualenv
  327. You can next rerun ``virtualenv -p python3 synapse`` to update the virtual env.
  328. Installing may fail during installing virtualenv with ``InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.``
  329. You can fix this by manually installing ndg-httpsclient::
  330. pip install --upgrade ndg-httpsclient
  331. Installing may fail with ``mock requires setuptools>=17.1. Aborting installation``.
  332. You can fix this by upgrading setuptools::
  333. pip install --upgrade setuptools
  334. If pip crashes mid-installation for reason (e.g. lost terminal), pip may
  335. refuse to run until you remove the temporary installation directory it
  336. created. To reset the installation::
  337. rm -rf /tmp/pip_install_matrix
  338. pip seems to leak *lots* of memory during installation. For instance, a Linux
  339. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  340. happens, you will have to individually install the dependencies which are
  341. failing, e.g.::
  342. pip install twisted
  343. Running out of File Handles
  344. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  345. If synapse runs out of filehandles, it typically fails badly - live-locking
  346. at 100% CPU, and/or failing to accept new TCP connections (blocking the
  347. connecting client). Matrix currently can legitimately use a lot of file handles,
  348. thanks to busy rooms like #matrix:matrix.org containing hundreds of participating
  349. servers. The first time a server talks in a room it will try to connect
  350. simultaneously to all participating servers, which could exhaust the available
  351. file descriptors between DNS queries & HTTPS sockets, especially if DNS is slow
  352. to respond. (We need to improve the routing algorithm used to be better than
  353. full mesh, but as of June 2017 this hasn't happened yet).
  354. If you hit this failure mode, we recommend increasing the maximum number of
  355. open file handles to be at least 4096 (assuming a default of 1024 or 256).
  356. This is typically done by editing ``/etc/security/limits.conf``
  357. Separately, Synapse may leak file handles if inbound HTTP requests get stuck
  358. during processing - e.g. blocked behind a lock or talking to a remote server etc.
  359. This is best diagnosed by matching up the 'Received request' and 'Processed request'
  360. log lines and looking for any 'Processed request' lines which take more than
  361. a few seconds to execute. Please let us know at #matrix-dev:matrix.org if
  362. you see this failure mode so we can help debug it, however.
  363. Upgrading an existing Synapse
  364. =============================
  365. The instructions for upgrading synapse are in `UPGRADE.rst`_.
  366. Please check these instructions as upgrading may require extra steps for some
  367. versions of synapse.
  368. .. _UPGRADE.rst: UPGRADE.rst
  369. .. _federation:
  370. Setting up Federation
  371. =====================
  372. Federation is the process by which users on different servers can participate
  373. in the same room. For this to work, those other servers must be able to contact
  374. yours to send messages.
  375. As explained in `Configuring synapse`_, the ``server_name`` in your
  376. ``homeserver.yaml`` file determines the way that other servers will reach
  377. yours. By default, they will treat it as a hostname and try to connect to
  378. port 8448. This is easy to set up and will work with the default configuration,
  379. provided you set the ``server_name`` to match your machine's public DNS
  380. hostname.
  381. For a more flexible configuration, you can set up a DNS SRV record. This allows
  382. you to run your server on a machine that might not have the same name as your
  383. domain name. For example, you might want to run your server at
  384. ``synapse.example.com``, but have your Matrix user-ids look like
  385. ``@user:example.com``. (A SRV record also allows you to change the port from
  386. the default 8448. However, if you are thinking of using a reverse-proxy on the
  387. federation port, which is not recommended, be sure to read
  388. `Reverse-proxying the federation port`_ first.)
  389. To use a SRV record, first create your SRV record and publish it in DNS. This
  390. should have the format ``_matrix._tcp.<yourdomain.com> <ttl> IN SRV 10 0 <port>
  391. <synapse.server.name>``. The DNS record should then look something like::
  392. $ dig -t srv _matrix._tcp.example.com
  393. _matrix._tcp.example.com. 3600 IN SRV 10 0 8448 synapse.example.com.
  394. Note that the server hostname cannot be an alias (CNAME record): it has to point
  395. directly to the server hosting the synapse instance.
  396. You can then configure your homeserver to use ``<yourdomain.com>`` as the domain in
  397. its user-ids, by setting ``server_name``::
  398. python -m synapse.app.homeserver \
  399. --server-name <yourdomain.com> \
  400. --config-path homeserver.yaml \
  401. --generate-config
  402. python -m synapse.app.homeserver --config-path homeserver.yaml
  403. If you've already generated the config file, you need to edit the ``server_name``
  404. in your ``homeserver.yaml`` file. If you've already started Synapse and a
  405. database has been created, you will have to recreate the database.
  406. If all goes well, you should be able to `connect to your server with a client`__,
  407. and then join a room via federation. (Try ``#matrix-dev:matrix.org`` as a first
  408. step. "Matrix HQ"'s sheer size and activity level tends to make even the
  409. largest boxes pause for thought.)
  410. .. __: `Connecting to Synapse from a client`_
  411. Troubleshooting
  412. ---------------
  413. You can use the federation tester to check if your homeserver is all set:
  414. ``https://matrix.org/federationtester/api/report?server_name=<your_server_name>``
  415. If any of the attributes under "checks" is false, federation won't work.
  416. The typical failure mode with federation is that when you try to join a room,
  417. it is rejected with "401: Unauthorized". Generally this means that other
  418. servers in the room couldn't access yours. (Joining a room over federation is a
  419. complicated dance which requires connections in both directions).
  420. So, things to check are:
  421. * If you are trying to use a reverse-proxy, read `Reverse-proxying the
  422. federation port`_.
  423. * If you are not using a SRV record, check that your ``server_name`` (the part
  424. of your user-id after the ``:``) matches your hostname, and that port 8448 on
  425. that hostname is reachable from outside your network.
  426. * If you *are* using a SRV record, check that it matches your ``server_name``
  427. (it should be ``_matrix._tcp.<server_name>``), and that the port and hostname
  428. it specifies are reachable from outside your network.
  429. Running a Demo Federation of Synapses
  430. -------------------------------------
  431. If you want to get up and running quickly with a trio of homeservers in a
  432. private federation, there is a script in the ``demo`` directory. This is mainly
  433. useful just for development purposes. See `<demo/README>`_.
  434. Using PostgreSQL
  435. ================
  436. As of Synapse 0.9, `PostgreSQL <https://www.postgresql.org>`_ is supported as an
  437. alternative to the `SQLite <https://sqlite.org/>`_ database that Synapse has
  438. traditionally used for convenience and simplicity.
  439. The advantages of Postgres include:
  440. * significant performance improvements due to the superior threading and
  441. caching model, smarter query optimiser
  442. * allowing the DB to be run on separate hardware
  443. * allowing basic active/backup high-availability with a "hot spare" synapse
  444. pointing at the same DB master, as well as enabling DB replication in
  445. synapse itself.
  446. For information on how to install and use PostgreSQL, please see
  447. `docs/postgres.rst <docs/postgres.rst>`_.
  448. .. _reverse-proxy:
  449. Using a reverse proxy with Synapse
  450. ==================================
  451. It is recommended to put a reverse proxy such as
  452. `nginx <https://nginx.org/en/docs/http/ngx_http_proxy_module.html>`_,
  453. `Apache <https://httpd.apache.org/docs/current/mod/mod_proxy_http.html>`_,
  454. `Caddy <https://caddyserver.com/docs/proxy>`_ or
  455. `HAProxy <https://www.haproxy.org/>`_ in front of Synapse. One advantage of
  456. doing so is that it means that you can expose the default https port (443) to
  457. Matrix clients without needing to run Synapse with root privileges.
  458. The most important thing to know here is that Matrix clients and other Matrix
  459. servers do not necessarily need to connect to your server via the same
  460. port. Indeed, clients will use port 443 by default, whereas servers default to
  461. port 8448. Where these are different, we refer to the 'client port' and the
  462. 'federation port'.
  463. The next most important thing to know is that using a reverse-proxy on the
  464. federation port has a number of pitfalls. It is possible, but be sure to read
  465. `Reverse-proxying the federation port`_.
  466. The recommended setup is therefore to configure your reverse-proxy on port 443
  467. to port 8008 of synapse for client connections, but to also directly expose port
  468. 8448 for server-server connections. All the Matrix endpoints begin ``/_matrix``,
  469. so an example nginx configuration might look like::
  470. server {
  471. listen 443 ssl;
  472. listen [::]:443 ssl;
  473. server_name matrix.example.com;
  474. location /_matrix {
  475. proxy_pass http://localhost:8008;
  476. proxy_set_header X-Forwarded-For $remote_addr;
  477. }
  478. }
  479. an example Caddy configuration might look like::
  480. matrix.example.com {
  481. proxy /_matrix http://localhost:8008 {
  482. transparent
  483. }
  484. }
  485. and an example Apache configuration might look like::
  486. <VirtualHost *:443>
  487. SSLEngine on
  488. ServerName matrix.example.com;
  489. <Location /_matrix>
  490. ProxyPass http://127.0.0.1:8008/_matrix nocanon
  491. ProxyPassReverse http://127.0.0.1:8008/_matrix
  492. </Location>
  493. </VirtualHost>
  494. You will also want to set ``bind_addresses: ['127.0.0.1']`` and ``x_forwarded: true``
  495. for port 8008 in ``homeserver.yaml`` to ensure that client IP addresses are
  496. recorded correctly.
  497. Having done so, you can then use ``https://matrix.example.com`` (instead of
  498. ``https://matrix.example.com:8448``) as the "Custom server" when `Connecting to
  499. Synapse from a client`_.
  500. Reverse-proxying the federation port
  501. ------------------------------------
  502. There are two issues to consider before using a reverse-proxy on the federation
  503. port:
  504. * Due to the way SSL certificates are managed in the Matrix federation protocol
  505. (see `spec`__), Synapse needs to be configured with the path to the SSL
  506. certificate, *even if you do not terminate SSL at Synapse*.
  507. .. __: `key_management`_
  508. * Until v0.33.3, Synapse did not support SNI on the federation port
  509. (`bug #1491 <https://github.com/matrix-org/synapse/issues/1491>`_). This bug
  510. is now fixed, but means that federating with older servers can be unreliable
  511. when using name-based virtual hosting.
  512. Furthermore, a number of the normal reasons for using a reverse-proxy do not
  513. apply:
  514. * Other servers will connect on port 8448 by default, so there is no need to
  515. listen on port 443 (for federation, at least), which avoids the need for root
  516. privileges and virtual hosting.
  517. * A self-signed SSL certificate is fine for federation, so there is no need to
  518. automate renewals. (The certificate generated by ``--generate-config`` is
  519. valid for 10 years.)
  520. If you want to set up a reverse-proxy on the federation port despite these
  521. caveats, you will need to do the following:
  522. * In ``homeserver.yaml``, set ``tls_certificate_path`` to the path to the SSL
  523. certificate file used by your reverse-proxy, and set ``no_tls`` to ``True``.
  524. (``tls_private_key_path`` will be ignored if ``no_tls`` is ``True``.)
  525. * In your reverse-proxy configuration:
  526. * If there are other virtual hosts on the same port, make sure that the
  527. *default* one uses the certificate configured above.
  528. * Forward ``/_matrix`` to Synapse.
  529. * If your reverse-proxy is not listening on port 8448, publish a SRV record to
  530. tell other servers how to find you. See `Setting up Federation`_.
  531. When updating the SSL certificate, just update the file pointed to by
  532. ``tls_certificate_path`` and then restart Synapse. (You may like to use a symbolic link
  533. to help make this process atomic.)
  534. The most common mistake when setting up federation is not to tell Synapse about
  535. your SSL certificate. To check it, you can visit
  536. ``https://matrix.org/federationtester/api/report?server_name=<your_server_name>``.
  537. Unfortunately, there is no UI for this yet, but, you should see
  538. ``"MatchingTLSFingerprint": true``. If not, check that
  539. ``Certificates[0].SHA256Fingerprint`` (the fingerprint of the certificate
  540. presented by your reverse-proxy) matches ``Keys.tls_fingerprints[0].sha256``
  541. (the fingerprint of the certificate Synapse is using).
  542. Identity Servers
  543. ================
  544. Identity servers have the job of mapping email addresses and other 3rd Party
  545. IDs (3PIDs) to Matrix user IDs, as well as verifying the ownership of 3PIDs
  546. before creating that mapping.
  547. **They are not where accounts or credentials are stored - these live on home
  548. servers. Identity Servers are just for mapping 3rd party IDs to matrix IDs.**
  549. This process is very security-sensitive, as there is obvious risk of spam if it
  550. is too easy to sign up for Matrix accounts or harvest 3PID data. In the longer
  551. term, we hope to create a decentralised system to manage it (`matrix-doc #712
  552. <https://github.com/matrix-org/matrix-doc/issues/712>`_), but in the meantime,
  553. the role of managing trusted identity in the Matrix ecosystem is farmed out to
  554. a cluster of known trusted ecosystem partners, who run 'Matrix Identity
  555. Servers' such as `Sydent <https://github.com/matrix-org/sydent>`_, whose role
  556. is purely to authenticate and track 3PID logins and publish end-user public
  557. keys.
  558. You can host your own copy of Sydent, but this will prevent you reaching other
  559. users in the Matrix ecosystem via their email address, and prevent them finding
  560. you. We therefore recommend that you use one of the centralised identity servers
  561. at ``https://matrix.org`` or ``https://vector.im`` for now.
  562. To reiterate: the Identity server will only be used if you choose to associate
  563. an email address with your account, or send an invite to another user via their
  564. email address.
  565. URL Previews
  566. ============
  567. Synapse 0.15.0 introduces a new API for previewing URLs at
  568. ``/_matrix/media/r0/preview_url``. This is disabled by default. To turn it on
  569. you must enable the ``url_preview_enabled: True`` config parameter and
  570. explicitly specify the IP ranges that Synapse is not allowed to spider for
  571. previewing in the ``url_preview_ip_range_blacklist`` configuration parameter.
  572. This is critical from a security perspective to stop arbitrary Matrix users
  573. spidering 'internal' URLs on your network. At the very least we recommend that
  574. your loopback and RFC1918 IP addresses are blacklisted.
  575. This also requires the optional lxml and netaddr python dependencies to be
  576. installed. This in turn requires the libxml2 library to be available - on
  577. Debian/Ubuntu this means ``apt-get install libxml2-dev``, or equivalent for
  578. your OS.
  579. Password reset
  580. ==============
  581. If a user has registered an email address to their account using an identity
  582. server, they can request a password-reset token via clients such as Riot.
  583. A manual password reset can be done via direct database access as follows.
  584. First calculate the hash of the new password::
  585. $ ~/synapse/env/bin/hash_password
  586. Password:
  587. Confirm password:
  588. $2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  589. Then update the `users` table in the database::
  590. UPDATE users SET password_hash='$2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  591. WHERE name='@test:test.com';
  592. Synapse Development
  593. ===================
  594. Before setting up a development environment for synapse, make sure you have the
  595. system dependencies (such as the python header files) installed - see
  596. `Installing from source`_.
  597. To check out a synapse for development, clone the git repo into a working
  598. directory of your choice::
  599. git clone https://github.com/matrix-org/synapse.git
  600. cd synapse
  601. Synapse has a number of external dependencies, that are easiest
  602. to install using pip and a virtualenv::
  603. virtualenv -p python2.7 env
  604. source env/bin/activate
  605. python -m pip install -e .[all]
  606. This will run a process of downloading and installing all the needed
  607. dependencies into a virtual env.
  608. Once this is done, you may wish to run Synapse's unit tests, to
  609. check that everything is installed as it should be::
  610. python -m twisted.trial tests
  611. This should end with a 'PASSED' result::
  612. Ran 143 tests in 0.601s
  613. PASSED (successes=143)
  614. Running the Integration Tests
  615. =============================
  616. Synapse is accompanied by `SyTest <https://github.com/matrix-org/sytest>`_,
  617. a Matrix homeserver integration testing suite, which uses HTTP requests to
  618. access the API as a Matrix client would. It is able to run Synapse directly from
  619. the source tree, so installation of the server is not required.
  620. Testing with SyTest is recommended for verifying that changes related to the
  621. Client-Server API are functioning correctly. See the `installation instructions
  622. <https://github.com/matrix-org/sytest#installing>`_ for details.
  623. Building Internal API Documentation
  624. ===================================
  625. Before building internal API documentation install sphinx and
  626. sphinxcontrib-napoleon::
  627. pip install sphinx
  628. pip install sphinxcontrib-napoleon
  629. Building internal API documentation::
  630. python setup.py build_sphinx
  631. Help!! Synapse eats all my RAM!
  632. ===============================
  633. Synapse's architecture is quite RAM hungry currently - we deliberately
  634. cache a lot of recent room data and metadata in RAM in order to speed up
  635. common requests. We'll improve this in future, but for now the easiest
  636. way to either reduce the RAM usage (at the risk of slowing things down)
  637. is to set the almost-undocumented ``SYNAPSE_CACHE_FACTOR`` environment
  638. variable. The default is 0.5, which can be decreased to reduce RAM usage
  639. in memory constrained enviroments, or increased if performance starts to
  640. degrade.
  641. Using `libjemalloc <http://jemalloc.net/>`_ can also yield a significant
  642. improvement in overall amount, and especially in terms of giving back RAM
  643. to the OS. To use it, the library must simply be put in the LD_PRELOAD
  644. environment variable when launching Synapse. On Debian, this can be done
  645. by installing the ``libjemalloc1`` package and adding this line to
  646. ``/etc/default/matrix-synapse``::
  647. LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1
  648. .. _`key_management`: https://matrix.org/docs/spec/server_server/unstable.html#retrieving-server-keys