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.

251 lines
7.5 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 prime256v1;
  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. sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs file git curl
  69. curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
  70. sudo apt-get install nodejs
  71. sudo npm install -g yarn
  72. ## Redis
  73. sudo apt-get install redis-server redis-tools
  74. ## Postgres
  75. sudo apt-get install postgresql postgresql-contrib
  76. Setup a user and database for Mastodon:
  77. sudo su - postgres
  78. psql
  79. In the prompt:
  80. CREATE USER mastodon CREATEDB;
  81. \q
  82. ## Rbenv
  83. 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)
  84. [1]: https://github.com/rbenv/rbenv#installation
  85. [2]: https://github.com/rbenv/ruby-build#installation
  86. [3]: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
  87. Then once `rbenv` is ready, run `rbenv install 2.3.1` to install the Ruby version for Mastodon.
  88. ## Git
  89. You need the `git-core` package installed on your system. If it is so, from the `mastodon` user:
  90. cd ~
  91. git clone https://github.com/tootsuite/mastodon.git live
  92. cd live
  93. Then you can proceed to install project dependencies:
  94. gem install bundler
  95. bundle install --deployment --without development test
  96. yarn install
  97. ## Configuration
  98. Then you have to configure your instance:
  99. cp .env.production.sample .env.production
  100. nano .env.production
  101. 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:
  102. rake secret
  103. 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").
  104. ## Setup
  105. And setup the database for the first time, this will create the tables and basic data:
  106. RAILS_ENV=production bundle exec rails db:setup
  107. Finally, pre-compile all CSS and JavaScript files:
  108. RAILS_ENV=production bundle exec rails assets:precompile
  109. ## Systemd
  110. Example systemd configuration for the web workers, to be placed in `/etc/systemd/system/mastodon-web.service`:
  111. ```systemd
  112. [Unit]
  113. Description=mastodon-web
  114. After=network.target
  115. [Service]
  116. Type=simple
  117. User=mastodon
  118. WorkingDirectory=/home/mastodon/live
  119. Environment="RAILS_ENV=production"
  120. Environment="PORT=3000"
  121. ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
  122. TimeoutSec=15
  123. Restart=always
  124. [Install]
  125. WantedBy=multi-user.target
  126. ```
  127. Example systemd configuration for the background workers, to be placed in `/etc/systemd/system/mastodon-sidekiq.service`:
  128. ```systemd
  129. [Unit]
  130. Description=mastodon-sidekiq
  131. After=network.target
  132. [Service]
  133. Type=simple
  134. User=mastodon
  135. WorkingDirectory=/home/mastodon/live
  136. Environment="RAILS_ENV=production"
  137. Environment="DB_POOL=5"
  138. ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
  139. TimeoutSec=15
  140. Restart=always
  141. [Install]
  142. WantedBy=multi-user.target
  143. ```
  144. Example systemd configuration file for the streaming API, to be placed in `/etc/systemd/system/mastodon-streaming.service`:
  145. ```systemd
  146. [Unit]
  147. Description=mastodon-streaming
  148. After=network.target
  149. [Service]
  150. Type=simple
  151. User=mastodon
  152. WorkingDirectory=/home/mastodon/live
  153. Environment="NODE_ENV=production"
  154. Environment="PORT=4000"
  155. ExecStart=/usr/bin/npm run start
  156. TimeoutSec=15
  157. Restart=always
  158. [Install]
  159. WantedBy=multi-user.target
  160. ```
  161. 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.
  162. ## Cronjobs
  163. I recommend creating a couple cronjobs for the following tasks:
  164. - `RAILS_ENV=production bundle exec rake mastodon:media:clear`
  165. - `RAILS_ENV=production bundle exec rake mastodon:push:refresh`
  166. - `RAILS_ENV=production bundle exec rake mastodon:feeds:clear`
  167. 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.
  168. You can edit the cronjob file for the `mastodon` user by running `sudo crontab -e -u mastodon` (outside of the mastodon user).
  169. ## Things to look out for when upgrading Mastodon
  170. You can upgrade Mastodon with a `git pull` from the repository directory. You may need to run:
  171. - `RAILS_ENV=production bundle exec rails db:migrate`
  172. - `RAILS_ENV=production bundle exec rails assets:precompile`
  173. 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:
  174. sudo systemctl restart mastodon-*.service