1
0

README.rst 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 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), 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. <http://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 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 python2.7-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 python2 python-pip \
  81. python-setuptools python-virtualenv sqlite3
  82. Installing prerequisites on CentOS 7::
  83. sudo yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel \
  84. lcms2-devel libwebp-devel tcl-devel tk-devel \
  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 python2.7-dev libffi-dev \
  94. python-pip python-setuptools sqlite3 \
  95. libssl-dev python-virtualenv libjpeg-dev
  96. sudo pip install --upgrade pip
  97. sudo pip install --upgrade ndg-httpsclient
  98. sudo pip install --upgrade virtualenv
  99. Installing prerequisites on openSUSE::
  100. sudo zypper in -t pattern devel_basis
  101. sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \
  102. python-devel libffi-devel libopenssl-devel libjpeg62-devel
  103. Installing prerequisites on OpenBSD::
  104. doas pkg_add python libffi py-pip py-setuptools sqlite3 py-virtualenv \
  105. libxslt
  106. To install the synapse homeserver run::
  107. virtualenv -p python2.7 ~/.synapse
  108. source ~/.synapse/bin/activate
  109. pip install --upgrade setuptools
  110. pip install https://github.com/matrix-org/synapse/tarball/master
  111. This installs synapse, along with the libraries it uses, into a virtual
  112. environment under ``~/.synapse``. Feel free to pick a different directory
  113. if you prefer.
  114. In case of problems, please see the _`Troubleshooting` section below.
  115. Alternatively, Silvio Fricke has contributed a Dockerfile to automate the
  116. above in Docker at https://registry.hub.docker.com/u/silviof/docker-matrix/.
  117. Also, Martin Giess has created an auto-deployment process with vagrant/ansible,
  118. tested with VirtualBox/AWS/DigitalOcean - see https://github.com/EMnify/matrix-synapse-auto-deploy
  119. for details.
  120. Configuring synapse
  121. -------------------
  122. Before you can start Synapse, you will need to generate a configuration
  123. file. To do this, run (in your virtualenv, as before)::
  124. cd ~/.synapse
  125. python -m synapse.app.homeserver \
  126. --server-name my.domain.name \
  127. --config-path homeserver.yaml \
  128. --generate-config \
  129. --report-stats=[yes|no]
  130. ... substituting an appropriate value for ``--server-name``. The server name
  131. determines the "domain" part of user-ids for users on your server: these will
  132. all be of the format ``@user:my.domain.name``. It also determines how other
  133. matrix servers will reach yours for `Federation`_. For a test configuration,
  134. set this to the hostname of your server. For a more production-ready setup, you
  135. will probably want to specify your domain (``example.com``) rather than a
  136. matrix-specific hostname here (in the same way that your email address is
  137. probably ``user@example.com`` rather than ``user@email.example.com``) - but
  138. doing so may require more advanced setup - see `Setting up
  139. Federation`_. Beware that the server name cannot be changed later.
  140. This command will generate you a config file that you can then customise, but it will
  141. also generate a set of keys for you. These keys will allow your Home Server to
  142. identify itself to other Home Servers, so don't lose or delete them. It would be
  143. wise to back them up somewhere safe. (If, for whatever reason, you do need to
  144. change your Home Server's keys, you may find that other Home Servers have the
  145. old key cached. If you update the signing key, you should change the name of the
  146. key in the ``<server name>.signing.key`` file (the second word) to something
  147. different. See `the spec`__ for more information on key management.)
  148. .. __: `key_management`_
  149. The default configuration exposes two HTTP ports: 8008 and 8448. Port 8008 is
  150. configured without TLS; it is not recommended this be exposed outside your
  151. local network. Port 8448 is configured to use TLS with a self-signed
  152. certificate. This is fine for testing with but, to avoid your clients
  153. complaining about the certificate, you will almost certainly want to use
  154. another certificate for production purposes. (Note that a self-signed
  155. certificate is fine for `Federation`_). You can do so by changing
  156. ``tls_certificate_path``, ``tls_private_key_path`` and ``tls_dh_params_path``
  157. in ``homeserver.yaml``; alternatively, you can use a reverse-proxy, but be sure
  158. to read `Using a reverse proxy with Synapse`_ when doing so.
  159. Apart from port 8448 using TLS, both ports are the same in the default
  160. configuration.
  161. Registering a user
  162. ------------------
  163. You will need at least one user on your server in order to use a Matrix
  164. client. Users can be registered either `via a Matrix client`__, or via a
  165. commandline script.
  166. .. __: `client-user-reg`_
  167. To get started, it is easiest to use the command line to register new users::
  168. $ source ~/.synapse/bin/activate
  169. $ synctl start # if not already running
  170. $ register_new_matrix_user -c homeserver.yaml https://localhost:8448
  171. New user localpart: erikj
  172. Password:
  173. Confirm password:
  174. Success!
  175. This process uses a setting ``registration_shared_secret`` in
  176. ``homeserver.yaml``, which is shared between Synapse itself and the
  177. ``register_new_matrix_user`` script. It doesn't matter what it is (a random
  178. value is generated by ``--generate-config``), but it should be kept secret, as
  179. anyone with knowledge of it can register users on your server even if
  180. ``enable_registration`` is ``false``.
  181. Setting up a TURN server
  182. ------------------------
  183. For reliable VoIP calls to be routed via this homeserver, you MUST configure
  184. a TURN server. See `<docs/turn-howto.rst>`_ for details.
  185. Running Synapse
  186. ===============
  187. To actually run your new homeserver, pick a working directory for Synapse to
  188. run (e.g. ``~/.synapse``), and::
  189. cd ~/.synapse
  190. source ./bin/activate
  191. synctl start
  192. Connecting to Synapse from a client
  193. ===================================
  194. The easiest way to try out your new Synapse installation is by connecting to it
  195. from a web client. The easiest option is probably the one at
  196. http://riot.im/app. You will need to specify a "Custom server" when you log on
  197. or register: set this to ``https://localhost:8448`` - remember to specify the
  198. port (``:8448``) unless you changed the configuration. (Leave the identity
  199. server as the default - see `Identity servers`_.)
  200. If all goes well you should at least be able to log in, create a room, and
  201. start sending messages.
  202. (The homeserver runs a web client by default at https://localhost:8448/, though
  203. as of the time of writing it is somewhat outdated and not really recommended -
  204. https://github.com/matrix-org/synapse/issues/1527).
  205. .. _`client-user-reg`:
  206. Registering a new user from a client
  207. ------------------------------------
  208. By default, registration of new users via Matrix clients is disabled. To enable
  209. it, specify ``enable_registration: true`` in ``homeserver.yaml``. (It is then
  210. recommended to also set up CAPTCHA - see `<docs/CAPTCHA_SETUP.rst>`_.)
  211. Once ``enable_registration`` is set to ``true``, it is possible to register a
  212. user via `riot.im <https://riot.im/app/#/register>`_ or other Matrix clients.
  213. Your new user name will be formed partly from the ``server_name`` (see
  214. `Configuring synapse`_), and partly from a localpart you specify when you
  215. create the account. Your name will take the form of::
  216. @localpart:my.domain.name
  217. (pronounced "at localpart on my dot domain dot name").
  218. As when logging in, you will need to specify a "Custom server". Specify your
  219. desired ``localpart`` in the 'User name' box.
  220. Security Note
  221. =============
  222. Matrix serves raw user generated data in some APIs - specifically the `content
  223. repository endpoints <http://matrix.org/docs/spec/client_server/latest.html#get-matrix-media-r0-download-servername-mediaid>`_.
  224. Whilst we have tried to mitigate against possible XSS attacks (e.g.
  225. https://github.com/matrix-org/synapse/pull/1021) we recommend running
  226. matrix homeservers on a dedicated domain name, to limit any malicious user generated
  227. content served to web browsers a matrix API from being able to attack webapps hosted
  228. on the same domain. This is particularly true of sharing a matrix webclient and
  229. server on the same domain.
  230. See https://github.com/vector-im/vector-web/issues/1977 and
  231. https://developer.github.com/changes/2014-04-25-user-content-security for more details.
  232. Platform-Specific Instructions
  233. ==============================
  234. Debian
  235. ------
  236. Matrix provides official Debian packages via apt from http://matrix.org/packages/debian/.
  237. Note that these packages do not include a client - choose one from
  238. https://matrix.org/docs/projects/try-matrix-now/ (or build your own with one of our SDKs :)
  239. Fedora
  240. ------
  241. Oleg Girko provides Fedora RPMs at
  242. https://obs.infoserver.lv/project/monitor/matrix-synapse
  243. ArchLinux
  244. ---------
  245. The quickest way to get up and running with ArchLinux is probably with Ivan
  246. Shapovalov's AUR package from
  247. https://aur.archlinux.org/packages/matrix-synapse/, which should pull in all
  248. the necessary dependencies.
  249. Alternatively, to install using pip a few changes may be needed as ArchLinux
  250. defaults to python 3, but synapse currently assumes python 2.7 by default:
  251. pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 )::
  252. sudo pip2.7 install --upgrade pip
  253. You also may need to explicitly specify python 2.7 again during the install
  254. request::
  255. pip2.7 install https://github.com/matrix-org/synapse/tarball/master
  256. If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  257. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  258. compile it under the right architecture. (This should not be needed if
  259. installing under virtualenv)::
  260. sudo pip2.7 uninstall py-bcrypt
  261. sudo pip2.7 install py-bcrypt
  262. During setup of Synapse you need to call python2.7 directly again::
  263. cd ~/.synapse
  264. python2.7 -m synapse.app.homeserver \
  265. --server-name machine.my.domain.name \
  266. --config-path homeserver.yaml \
  267. --generate-config
  268. ...substituting your host and domain name as appropriate.
  269. FreeBSD
  270. -------
  271. Synapse can be installed via FreeBSD Ports or Packages contributed by Brendan Molloy from:
  272. - Ports: ``cd /usr/ports/net/py-matrix-synapse && make install clean``
  273. - Packages: ``pkg install py27-matrix-synapse``
  274. OpenBSD
  275. -------
  276. There is currently no port for OpenBSD. Additionally, OpenBSD's security
  277. settings require a slightly more difficult installation process.
  278. 1) Create a new directory in ``/usr/local`` called ``_synapse``. Also, create a
  279. new user called ``_synapse`` and set that directory as the new user's home.
  280. This is required because, by default, OpenBSD only allows binaries which need
  281. write and execute permissions on the same memory space to be run from
  282. ``/usr/local``.
  283. 2) ``su`` to the new ``_synapse`` user and change to their home directory.
  284. 3) Create a new virtualenv: ``virtualenv -p python2.7 ~/.synapse``
  285. 4) Source the virtualenv configuration located at
  286. ``/usr/local/_synapse/.synapse/bin/activate``. This is done in ``ksh`` by
  287. using the ``.`` command, rather than ``bash``'s ``source``.
  288. 5) Optionally, use ``pip`` to install ``lxml``, which Synapse needs to parse
  289. webpages for their titles.
  290. 6) Use ``pip`` to install this repository: ``pip install
  291. https://github.com/matrix-org/synapse/tarball/master``
  292. 7) Optionally, change ``_synapse``'s shell to ``/bin/false`` to reduce the
  293. chance of a compromised Synapse server being used to take over your box.
  294. After this, you may proceed with the rest of the install directions.
  295. NixOS
  296. -----
  297. Robin Lambertz has packaged Synapse for NixOS at:
  298. https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix
  299. Windows Install
  300. ---------------
  301. Synapse can be installed on Cygwin. It requires the following Cygwin packages:
  302. - gcc
  303. - git
  304. - libffi-devel
  305. - openssl (and openssl-devel, python-openssl)
  306. - python
  307. - python-setuptools
  308. The content repository requires additional packages and will be unable to process
  309. uploads without them:
  310. - libjpeg8
  311. - libjpeg8-devel
  312. - zlib
  313. If you choose to install Synapse without these packages, you will need to reinstall
  314. ``pillow`` for changes to be applied, e.g. ``pip uninstall pillow`` ``pip install
  315. pillow --user``
  316. Troubleshooting:
  317. - You may need to upgrade ``setuptools`` to get this to work correctly:
  318. ``pip install setuptools --upgrade``.
  319. - You may encounter errors indicating that ``ffi.h`` is missing, even with
  320. ``libffi-devel`` installed. If you do, copy the ``.h`` files:
  321. ``cp /usr/lib/libffi-3.0.13/include/*.h /usr/include``
  322. - You may need to install libsodium from source in order to install PyNacl. If
  323. you do, you may need to create a symlink to ``libsodium.a`` so ``ld`` can find
  324. it: ``ln -s /usr/local/lib/libsodium.a /usr/lib/libsodium.a``
  325. Troubleshooting
  326. ===============
  327. Troubleshooting Installation
  328. ----------------------------
  329. Synapse requires pip 1.7 or later, so if your OS provides too old a version you
  330. may need to manually upgrade it::
  331. sudo pip install --upgrade pip
  332. Installing may fail with ``Could not find any downloads that satisfy the requirement pymacaroons-pynacl (from matrix-synapse==0.12.0)``.
  333. You can fix this by manually upgrading pip and virtualenv::
  334. sudo pip install --upgrade virtualenv
  335. You can next rerun ``virtualenv -p python2.7 synapse`` to update the virtual env.
  336. 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.``
  337. You can fix this by manually installing ndg-httpsclient::
  338. pip install --upgrade ndg-httpsclient
  339. Installing may fail with ``mock requires setuptools>=17.1. Aborting installation``.
  340. You can fix this by upgrading setuptools::
  341. pip install --upgrade setuptools
  342. If pip crashes mid-installation for reason (e.g. lost terminal), pip may
  343. refuse to run until you remove the temporary installation directory it
  344. created. To reset the installation::
  345. rm -rf /tmp/pip_install_matrix
  346. pip seems to leak *lots* of memory during installation. For instance, a Linux
  347. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  348. happens, you will have to individually install the dependencies which are
  349. failing, e.g.::
  350. pip install twisted
  351. On OS X, if you encounter clang: error: unknown argument: '-mno-fused-madd' you
  352. will need to export CFLAGS=-Qunused-arguments.
  353. Troubleshooting Running
  354. -----------------------
  355. If synapse fails with ``missing "sodium.h"`` crypto errors, you may need
  356. to manually upgrade PyNaCL, as synapse uses NaCl (http://nacl.cr.yp.to/) for
  357. encryption and digital signatures.
  358. Unfortunately PyNACL currently has a few issues
  359. (https://github.com/pyca/pynacl/issues/53) and
  360. (https://github.com/pyca/pynacl/issues/79) that mean it may not install
  361. correctly, causing all tests to fail with errors about missing "sodium.h". To
  362. fix try re-installing from PyPI or directly from
  363. (https://github.com/pyca/pynacl)::
  364. # Install from PyPI
  365. pip install --user --upgrade --force pynacl
  366. # Install from github
  367. pip install --user https://github.com/pyca/pynacl/tarball/master
  368. ArchLinux
  369. ~~~~~~~~~
  370. If running `$ synctl start` fails with 'returned non-zero exit status 1',
  371. you will need to explicitly call Python2.7 - either running as::
  372. python2.7 -m synapse.app.homeserver --daemonize -c homeserver.yaml
  373. ...or by editing synctl with the correct python executable.
  374. Upgrading an existing Synapse
  375. =============================
  376. The instructions for upgrading synapse are in `UPGRADE.rst`_.
  377. Please check these instructions as upgrading may require extra steps for some
  378. versions of synapse.
  379. .. _UPGRADE.rst: UPGRADE.rst
  380. .. _federation:
  381. Setting up Federation
  382. =====================
  383. Federation is the process by which users on different servers can participate
  384. in the same room. For this to work, those other servers must be able to contact
  385. yours to send messages.
  386. As explained in `Configuring synapse`_, the ``server_name`` in your
  387. ``homeserver.yaml`` file determines the way that other servers will reach
  388. yours. By default, they will treat it as a hostname and try to connect to
  389. port 8448. This is easy to set up and will work with the default configuration,
  390. provided you set the ``server_name`` to match your machine's public DNS
  391. hostname.
  392. For a more flexible configuration, you can set up a DNS SRV record. This allows
  393. you to run your server on a machine that might not have the same name as your
  394. domain name. For example, you might want to run your server at
  395. ``synapse.example.com``, but have your Matrix user-ids look like
  396. ``@user:example.com``. (A SRV record also allows you to change the port from
  397. the default 8448. However, if you are thinking of using a reverse-proxy, be
  398. sure to read `Reverse-proxying the federation port`_ first.)
  399. To use a SRV record, first create your SRV record and publish it in DNS. This
  400. should have the format ``_matrix._tcp.<yourdomain.com> <ttl> IN SRV 10 0 <port>
  401. <synapse.server.name>``. The DNS record should then look something like::
  402. $ dig -t srv _matrix._tcp.example.com
  403. _matrix._tcp.example.com. 3600 IN SRV 10 0 8448 synapse.example.com.
  404. You can then configure your homeserver to use ``<yourdomain.com>`` as the domain in
  405. its user-ids, by setting ``server_name``::
  406. python -m synapse.app.homeserver \
  407. --server-name <yourdomain.com> \
  408. --config-path homeserver.yaml \
  409. --generate-config
  410. python -m synapse.app.homeserver --config-path homeserver.yaml
  411. If you've already generated the config file, you need to edit the ``server_name``
  412. in your ``homeserver.yaml`` file. If you've already started Synapse and a
  413. database has been created, you will have to recreate the database.
  414. If all goes well, you should be able to `connect to your server with a client`__,
  415. and then join a room via federation. (Try ``#matrix-dev:matrix.org`` as a first
  416. step. "Matrix HQ"'s sheer size and activity level tends to make even the
  417. largest boxes pause for thought.)
  418. .. __: `Connecting to Synapse from a client`_
  419. Troubleshooting
  420. ---------------
  421. The typical failure mode with federation is that when you try to join a room,
  422. it is rejected with "401: Unauthorized". Generally this means that other
  423. servers in the room couldn't access yours. (Joining a room over federation is a
  424. complicated dance which requires connections in both directions).
  425. So, things to check are:
  426. * If you are trying to use a reverse-proxy, read `Reverse-proxying the
  427. federation port`_.
  428. * If you are not using a SRV record, check that your ``server_name`` (the part
  429. of your user-id after the ``:``) matches your hostname, and that port 8448 on
  430. that hostname is reachable from outside your network.
  431. * If you *are* using a SRV record, check that it matches your ``server_name``
  432. (it should be ``_matrix._tcp.<server_name>``), and that the port and hostname
  433. it specifies are reachable from outside your network.
  434. Running a Demo Federation of Synapses
  435. -------------------------------------
  436. If you want to get up and running quickly with a trio of homeservers in a
  437. private federation, there is a script in the ``demo`` directory. This is mainly
  438. useful just for development purposes. See `<demo/README>`_.
  439. Using PostgreSQL
  440. ================
  441. As of Synapse 0.9, `PostgreSQL <http://www.postgresql.org>`_ is supported as an
  442. alternative to the `SQLite <http://sqlite.org/>`_ database that Synapse has
  443. traditionally used for convenience and simplicity.
  444. The advantages of Postgres include:
  445. * significant performance improvements due to the superior threading and
  446. caching model, smarter query optimiser
  447. * allowing the DB to be run on separate hardware
  448. * allowing basic active/backup high-availability with a "hot spare" synapse
  449. pointing at the same DB master, as well as enabling DB replication in
  450. synapse itself.
  451. For information on how to install and use PostgreSQL, please see
  452. `docs/postgres.rst <docs/postgres.rst>`_.
  453. .. _reverse-proxy:
  454. Using a reverse proxy with Synapse
  455. ==================================
  456. It is possible to put a reverse proxy such as
  457. `nginx <https://nginx.org/en/docs/http/ngx_http_proxy_module.html>`_,
  458. `Apache <https://httpd.apache.org/docs/current/mod/mod_proxy_http.html>`_ or
  459. `HAProxy <http://www.haproxy.org/>`_ in front of Synapse. One advantage of
  460. doing so is that it means that you can expose the default https port (443) to
  461. Matrix clients without needing to run Synapse with root privileges.
  462. The most important thing to know here is that Matrix clients and other Matrix
  463. servers do not necessarily need to connect to your server via the same
  464. port. Indeed, clients will use port 443 by default, whereas servers default to
  465. port 8448. Where these are different, we refer to the 'client port' and the
  466. 'federation port'.
  467. The next most important thing to know is that using a reverse-proxy on the
  468. federation port has a number of pitfalls. It is possible, but be sure to read
  469. `Reverse-proxying the federation port`_.
  470. The recommended setup is therefore to configure your reverse-proxy on port 443
  471. for client connections, but to also expose port 8448 for server-server
  472. connections. All the Matrix endpoints begin ``/_matrix``, so an example nginx
  473. configuration might look like::
  474. server {
  475. listen 443 ssl;
  476. listen [::]:443 ssl;
  477. server_name matrix.example.com;
  478. location /_matrix {
  479. proxy_pass http://localhost:8008;
  480. proxy_set_header X-Forwarded-For $remote_addr;
  481. }
  482. }
  483. You will also want to set ``bind_addresses: ['127.0.0.1']`` and ``x_forwarded: true``
  484. for port 8008 in ``homeserver.yaml`` to ensure that client IP addresses are
  485. recorded correctly.
  486. Having done so, you can then use ``https://matrix.example.com`` (instead of
  487. ``https://matrix.example.com:8448``) as the "Custom server" when `Connecting to
  488. Synapse from a client`_.
  489. Reverse-proxying the federation port
  490. ------------------------------------
  491. There are two issues to consider before using a reverse-proxy on the federation
  492. port:
  493. * Due to the way SSL certificates are managed in the Matrix federation protocol
  494. (see `spec`__), Synapse needs to be configured with the path to the SSL
  495. certificate, *even if you do not terminate SSL at Synapse*.
  496. .. __: `key_management`_
  497. * Synapse does not currently support SNI on the federation protocol
  498. (`bug #1491 <https://github.com/matrix-org/synapse/issues/1491>`_), which
  499. means that using name-based virtual hosting is unreliable.
  500. Furthermore, a number of the normal reasons for using a reverse-proxy do not
  501. apply:
  502. * Other servers will connect on port 8448 by default, so there is no need to
  503. listen on port 443 (for federation, at least), which avoids the need for root
  504. privileges and virtual hosting.
  505. * A self-signed SSL certificate is fine for federation, so there is no need to
  506. automate renewals. (The certificate generated by ``--generate-config`` is
  507. valid for 10 years.)
  508. If you want to set up a reverse-proxy on the federation port despite these
  509. caveats, you will need to do the following:
  510. * In ``homeserver.yaml``, set ``tls_certificate_path`` to the path to the SSL
  511. certificate file used by your reverse-proxy, and set ``no_tls`` to ``True``.
  512. (``tls_private_key_path`` will be ignored if ``no_tls`` is ``True``.)
  513. * In your reverse-proxy configuration:
  514. * If there are other virtual hosts on the same port, make sure that the
  515. *default* one uses the certificate configured above.
  516. * Forward ``/_matrix`` to Synapse.
  517. * If your reverse-proxy is not listening on port 8448, publish a SRV record to
  518. tell other servers how to find you. See `Setting up Federation`_.
  519. When updating the SSL certificate, just update the file pointed to by
  520. ``tls_certificate_path``: there is no need to restart synapse. (You may like to
  521. use a symbolic link to help make this process atomic.)
  522. The most common mistake when setting up federation is not to tell Synapse about
  523. your SSL certificate. To check it, you can visit
  524. ``https://matrix.org/federationtester/api/report?server_name=<your_server_name>``.
  525. Unfortunately, there is no UI for this yet, but, you should see
  526. ``"MatchingTLSFingerprint": true``. If not, check that
  527. ``Certificates[0].SHA256Fingerprint`` (the fingerprint of the certificate
  528. presented by your reverse-proxy) matches ``Keys.tls_fingerprints[0].sha256``
  529. (the fingerprint of the certificate Synapse is using).
  530. Identity Servers
  531. ================
  532. Identity servers have the job of mapping email addresses and other 3rd Party
  533. IDs (3PIDs) to Matrix user IDs, as well as verifying the ownership of 3PIDs
  534. before creating that mapping.
  535. **They are not where accounts or credentials are stored - these live on home
  536. servers. Identity Servers are just for mapping 3rd party IDs to matrix IDs.**
  537. This process is very security-sensitive, as there is obvious risk of spam if it
  538. is too easy to sign up for Matrix accounts or harvest 3PID data. In the longer
  539. term, we hope to create a decentralised system to manage it (`matrix-doc #712
  540. <https://github.com/matrix-org/matrix-doc/issues/712>`_), but in the meantime,
  541. the role of managing trusted identity in the Matrix ecosystem is farmed out to
  542. a cluster of known trusted ecosystem partners, who run 'Matrix Identity
  543. Servers' such as `Sydent <https://github.com/matrix-org/sydent>`_, whose role
  544. is purely to authenticate and track 3PID logins and publish end-user public
  545. keys.
  546. You can host your own copy of Sydent, but this will prevent you reaching other
  547. users in the Matrix ecosystem via their email address, and prevent them finding
  548. you. We therefore recommend that you use one of the centralised identity servers
  549. at ``https://matrix.org`` or ``https://vector.im`` for now.
  550. To reiterate: the Identity server will only be used if you choose to associate
  551. an email address with your account, or send an invite to another user via their
  552. email address.
  553. URL Previews
  554. ============
  555. Synapse 0.15.0 introduces a new API for previewing URLs at
  556. ``/_matrix/media/r0/preview_url``. This is disabled by default. To turn it on
  557. you must enable the ``url_preview_enabled: True`` config parameter and
  558. explicitly specify the IP ranges that Synapse is not allowed to spider for
  559. previewing in the ``url_preview_ip_range_blacklist`` configuration parameter.
  560. This is critical from a security perspective to stop arbitrary Matrix users
  561. spidering 'internal' URLs on your network. At the very least we recommend that
  562. your loopback and RFC1918 IP addresses are blacklisted.
  563. This also requires the optional lxml and netaddr python dependencies to be
  564. installed.
  565. Password reset
  566. ==============
  567. If a user has registered an email address to their account using an identity
  568. server, they can request a password-reset token via clients such as Vector.
  569. A manual password reset can be done via direct database access as follows.
  570. First calculate the hash of the new password::
  571. $ source ~/.synapse/bin/activate
  572. $ ./scripts/hash_password
  573. Password:
  574. Confirm password:
  575. $2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  576. Then update the `users` table in the database::
  577. UPDATE users SET password_hash='$2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  578. WHERE name='@test:test.com';
  579. Synapse Development
  580. ===================
  581. Before setting up a development environment for synapse, make sure you have the
  582. system dependencies (such as the python header files) installed - see
  583. `Installing from source`_.
  584. To check out a synapse for development, clone the git repo into a working
  585. directory of your choice::
  586. git clone https://github.com/matrix-org/synapse.git
  587. cd synapse
  588. Synapse has a number of external dependencies, that are easiest
  589. to install using pip and a virtualenv::
  590. virtualenv env
  591. source env/bin/activate
  592. python synapse/python_dependencies.py | xargs pip install
  593. pip install lxml mock
  594. This will run a process of downloading and installing all the needed
  595. dependencies into a virtual env.
  596. Once this is done, you may wish to run Synapse's unit tests, to
  597. check that everything is installed as it should be::
  598. PYTHONPATH="." trial tests
  599. This should end with a 'PASSED' result::
  600. Ran 143 tests in 0.601s
  601. PASSED (successes=143)
  602. Building Internal API Documentation
  603. ===================================
  604. Before building internal API documentation install sphinx and
  605. sphinxcontrib-napoleon::
  606. pip install sphinx
  607. pip install sphinxcontrib-napoleon
  608. Building internal API documentation::
  609. python setup.py build_sphinx
  610. Help!! Synapse eats all my RAM!
  611. ===============================
  612. Synapse's architecture is quite RAM hungry currently - we deliberately
  613. cache a lot of recent room data and metadata in RAM in order to speed up
  614. common requests. We'll improve this in future, but for now the easiest
  615. way to either reduce the RAM usage (at the risk of slowing things down)
  616. is to set the almost-undocumented ``SYNAPSE_CACHE_FACTOR`` environment
  617. variable. Roughly speaking, a SYNAPSE_CACHE_FACTOR of 1.0 will max out
  618. at around 3-4GB of resident memory - this is what we currently run the
  619. matrix.org on. The default setting is currently 0.1, which is probably
  620. around a ~700MB footprint. You can dial it down further to 0.02 if
  621. desired, which targets roughly ~512MB. Conversely you can dial it up if
  622. you need performance for lots of users and have a box with a lot of RAM.
  623. .. _`key_management`: https://matrix.org/docs/spec/server_server/unstable.html#retrieving-server-keys