You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.4 KiB

7 years ago
  1. Production guide
  2. ================
  3. ## Nginx
  4. Regardless of whether you go with the Docker approach or not, here is an example Nginx server configuration:
  5. ```nginx
  6. map $http_upgrade $connection_upgrade {
  7. default upgrade;
  8. '' close;
  9. }
  10. server {
  11. listen 80;
  12. listen [::]:80;
  13. server_name example.com;
  14. return 301 https://$host$request_uri;
  15. }
  16. server {
  17. listen 443 ssl;
  18. server_name example.com;
  19. ssl_protocols TLSv1.2;
  20. ssl_ciphers EECDH+AESGCM:EECDH+AES;
  21. ssl_ecdh_curve secp384r1;
  22. ssl_prefer_server_ciphers on;
  23. ssl_session_cache shared:SSL:10m;
  24. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  25. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  26. keepalive_timeout 70;
  27. sendfile on;
  28. client_max_body_size 0;
  29. gzip off;
  30. root /home/mastodon/live/public;
  31. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
  32. location / {
  33. try_files $uri @proxy;
  34. }
  35. location @proxy {
  36. proxy_set_header Host $host;
  37. proxy_set_header X-Real-IP $remote_addr;
  38. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  39. proxy_set_header X-Forwarded-Proto https;
  40. proxy_pass_header Server;
  41. proxy_pass http://localhost:3000;
  42. proxy_buffering off;
  43. proxy_redirect off;
  44. proxy_http_version 1.1;
  45. proxy_set_header Upgrade $http_upgrade;
  46. proxy_set_header Connection $connection_upgrade;
  47. tcp_nodelay on;
  48. }
  49. location /api/v1/streaming {
  50. proxy_set_header Host $host;
  51. proxy_set_header X-Real-IP $remote_addr;
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. proxy_set_header X-Forwarded-Proto https;
  54. proxy_pass http://localhost:4000;
  55. proxy_buffering off;
  56. proxy_redirect off;
  57. proxy_http_version 1.1;
  58. proxy_set_header Upgrade $http_upgrade;
  59. proxy_set_header Connection $connection_upgrade;
  60. tcp_nodelay on;
  61. }
  62. error_page 500 501 502 503 504 /500.html;
  63. }
  64. ```
  65. ## Running in production without Docker
  66. It is recommended to create a special user for mastodon on the server (you could call the user `mastodon`), though remember to disable outside login for it. You should only be able to get into that user through `sudo su - mastodon`.
  67. ## General dependencies
  68. curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
  69. sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs file
  70. sudo npm install -g yarn
  71. ## Redis
  72. sudo apt-get install redis-server redis-tools
  73. ## Postgres
  74. sudo apt-get install postgresql postgresql-contrib
  75. Setup a user and database for Mastodon:
  76. sudo su - postgres
  77. psql
  78. In the prompt:
  79. CREATE USER mastodon CREATEDB;
  80. \q
  81. ## Rbenv
  82. It is recommended to use rbenv (exclusively from the `mastodon` user) to install the desired Ruby version. Follow the guides to [install rbenv][1] and [rbenv-build][2] (I recommend checking the [prerequisites][3] for your system on the rbenv-build project and installing them beforehand, obviously outside the unprivileged `mastodon` user)
  83. [1]: https://github.com/rbenv/rbenv#installation
  84. [2]: https://github.com/rbenv/ruby-build#installation
  85. [3]: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
  86. Then once `rbenv` is ready, run `rbenv install 2.3.1` to install the Ruby version for Mastodon.
  87. ## Git
  88. You need the `git-core` package installed on your system. If it is so, from the `mastodon` user:
  89. cd ~
  90. git clone https://github.com/tootsuite/mastodon.git live
  91. cd live
  92. Then you can proceed to install project dependencies:
  93. gem install bundler
  94. bundle install --deployment --without development test
  95. yarn install
  96. ## Configuration
  97. Then you have to configure your instance:
  98. cp .env.production.sample .env.production
  99. nano .env.production
  100. Fill in the important data, like host/port of the redis database, host/port/username/password of the postgres database, your domain name, SMTP details (e.g. from Mailgun or equivalent transactional e-mail service, many have free tiers), whether you intend to use SSL, etc. If you need to generate secrets, you can use:
  101. rake secret
  102. To get a random string. If you are setting up on one single server (most likely), then `REDIS_HOST` is localhost and `DB_HOST` is `/var/run/postgresql`, `DB_USER` is `mastodon` and `DB_NAME` is `mastodon_production` while `DB_PASS` is empty because this setup will use the ident authentication method (system user "mastodon" maps to postgres user "mastodon").
  103. ## Setup
  104. And setup the database for the first time, this will create the tables and basic data:
  105. RAILS_ENV=production bundle exec rails db:setup
  106. Finally, pre-compile all CSS and JavaScript files:
  107. RAILS_ENV=production bundle exec rails assets:precompile
  108. ## Systemd
  109. Example systemd configuration for the web workers, to be placed in `/etc/systemd/system/mastodon-web.service`:
  110. ```systemd
  111. [Unit]
  112. Description=mastodon-web
  113. After=network.target
  114. [Service]
  115. Type=simple
  116. User=mastodon
  117. WorkingDirectory=/home/mastodon/live
  118. Environment="RAILS_ENV=production"
  119. Environment="PORT=3000"
  120. ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
  121. TimeoutSec=15
  122. Restart=always
  123. [Install]
  124. WantedBy=multi-user.target
  125. ```
  126. Example systemd configuration for the background workers, to be placed in `/etc/systemd/system/mastodon-sidekiq.service`:
  127. ```systemd
  128. [Unit]
  129. Description=mastodon-sidekiq
  130. After=network.target
  131. [Service]
  132. Type=simple
  133. User=mastodon
  134. WorkingDirectory=/home/mastodon/live
  135. Environment="RAILS_ENV=production"
  136. Environment="DB_POOL=5"
  137. ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
  138. TimeoutSec=15
  139. Restart=always
  140. [Install]
  141. WantedBy=multi-user.target
  142. ```
  143. Example systemd configuration file for the streaming API, to be placed in `/etc/systemd/system/mastodon-streaming.service`:
  144. ```systemd
  145. [Unit]
  146. Description=mastodon-streaming
  147. After=network.target
  148. [Service]
  149. Type=simple
  150. User=mastodon
  151. WorkingDirectory=/home/mastodon/live
  152. Environment="NODE_ENV=production"
  153. Environment="PORT=4000"
  154. ExecStart=/usr/bin/npm run start
  155. TimeoutSec=15
  156. Restart=always
  157. [Install]
  158. WantedBy=multi-user.target
  159. ```
  160. This allows you to `sudo systemctl enable /etc/systemd/system/mastodon-*.service` and `sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service` to get things going.
  161. ## Cronjobs
  162. I recommend creating a couple cronjobs for the following tasks:
  163. - `RAILS_ENV=production bundle exec rake mastodon:media:clear`
  164. - `RAILS_ENV=production bundle exec rake mastodon:push:refresh`
  165. - `RAILS_ENV=production bundle exec rake mastodon:feeds:clear`
  166. You may want to run `which bundle` first and copypaste that full path instead of simply `bundle` in the above commands because cronjobs usually don't have all the paths set. The time and intervals of when to run these jobs are up to you, but once every day should be enough for all.
  167. You can edit the cronjob file for the `mastodon` user by running `sudo crontab -e -u mastodon` (outside of the mastodon user).
  168. ## Things to look out for when upgrading Mastodon
  169. You can upgrade Mastodon with a `git pull` from the repository directory. You may need to run:
  170. - `RAILS_ENV=production bundle exec rails db:migrate`
  171. - `RAILS_ENV=production bundle exec rails assets:precompile`
  172. Depending on which files changed, e.g. if anything in the `/db/` or `/app/assets` directory changed, respectively. Also, Mastodon runs in memory, so you need to restart it before you see any changes. If you're using systemd, that would be:
  173. sudo systemctl restart mastodon-*.service