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.

225 lines
6.6 KiB

  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 443 ssl;
  12. server_name example.com;
  13. ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  14. ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  15. keepalive_timeout 70;
  16. sendfile on;
  17. client_max_body_size 0;
  18. gzip off;
  19. root /home/mastodon/live/public;
  20. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
  21. location / {
  22. try_files $uri @proxy;
  23. }
  24. location @proxy {
  25. proxy_set_header Host $host;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. proxy_set_header X-Forwarded-Proto https;
  29. proxy_pass_header Server;
  30. proxy_pass http://localhost:3000;
  31. proxy_buffering off;
  32. proxy_redirect off;
  33. proxy_http_version 1.1;
  34. proxy_set_header Upgrade $http_upgrade;
  35. proxy_set_header Connection $connection_upgrade;
  36. tcp_nodelay on;
  37. }
  38. location /api/v1/streaming {
  39. proxy_set_header Host $host;
  40. proxy_set_header X-Real-IP $remote_addr;
  41. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  42. proxy_set_header X-Forwarded-Proto https;
  43. proxy_pass http://localhost:4000;
  44. proxy_buffering off;
  45. proxy_redirect off;
  46. proxy_http_version 1.1;
  47. proxy_set_header Upgrade $http_upgrade;
  48. proxy_set_header Connection $connection_upgrade;
  49. tcp_nodelay on;
  50. }
  51. error_page 500 501 502 503 504 /500.html;
  52. }
  53. ```
  54. ## Running in production without Docker
  55. 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`.
  56. ## General dependencies
  57. curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
  58. sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs
  59. sudo npm install -g yarn
  60. ## Redis
  61. sudo apt-get install redis-server redis-tools
  62. ## Postgres
  63. sudo apt-get install postgresql postgresql-contrib
  64. ## Rbenv
  65. 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)
  66. [1]: https://github.com/rbenv/rbenv#installation
  67. [2]: https://github.com/rbenv/ruby-build#installation
  68. [3]: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
  69. Then once `rbenv` is ready, run `rbenv install 2.3.1` to install the Ruby version for Mastodon.
  70. ## Git
  71. You need the `git-core` package installed on your system. If it is so, from the `mastodon` user:
  72. cd ~
  73. git clone https://github.com/Gargron/mastodon.git live
  74. cd live
  75. Then you can proceed to install project dependencies:
  76. gem install bundler
  77. bundle install --deployment --without development test
  78. yarn install
  79. ## Configuration
  80. Then you have to configure your instance:
  81. cp .env.production.sample .env.production
  82. nano .env.production
  83. 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:
  84. rake secret
  85. To get a random string.
  86. ## Setup
  87. And setup the database for the first time, this will create the tables and basic data:
  88. RAILS_ENV=production bundle exec rails db:setup
  89. Finally, pre-compile all CSS and JavaScript files:
  90. RAILS_ENV=production bundle exec rails assets:precompile
  91. ## Systemd
  92. Example systemd configuration for the web workers, to be placed in `/etc/systemd/system/mastodon-web.service`:
  93. ```systemd
  94. [Unit]
  95. Description=mastodon-web
  96. After=network.target
  97. [Service]
  98. Type=simple
  99. User=mastodon
  100. WorkingDirectory=/home/mastodon/live
  101. Environment="RAILS_ENV=production"
  102. Environment="PORT=3000"
  103. ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
  104. TimeoutSec=15
  105. Restart=always
  106. [Install]
  107. WantedBy=multi-user.target
  108. ```
  109. Example systemd configuration for the background workers, to be placed in `/etc/systemd/system/mastodon-sidekiq.service`:
  110. ```systemd
  111. [Unit]
  112. Description=mastodon-sidekiq
  113. After=network.target
  114. [Service]
  115. Type=simple
  116. User=mastodon
  117. WorkingDirectory=/home/mastodon/live
  118. Environment="RAILS_ENV=production"
  119. Environment="DB_POOL=5"
  120. ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q push
  121. TimeoutSec=15
  122. Restart=always
  123. [Install]
  124. WantedBy=multi-user.target
  125. ```
  126. Example systemd configuration file for the streaming API, to be placed in `/etc/systemd/system/mastodon-streaming.service`:
  127. ```systemd
  128. [Unit]
  129. Description=mastodon-streaming
  130. After=network.target
  131. [Service]
  132. Type=simple
  133. User=mastodon
  134. WorkingDirectory=/home/mastodon/live
  135. Environment="NODE_ENV=production"
  136. Environment="PORT=4000"
  137. ExecStart=/usr/bin/npm run start
  138. TimeoutSec=15
  139. Restart=always
  140. [Install]
  141. WantedBy=multi-user.target
  142. ```
  143. This allows you to `sudo systemctl enable mastodon-*.service` and `sudo systemctl start mastodon-*.service` to get things going.
  144. ## Cronjobs
  145. I recommend creating a couple cronjobs for the following tasks:
  146. - `RAILS_ENV=production bundle exec rake mastodon:media:clear`
  147. - `RAILS_ENV=production bundle exec rake mastodon:push:refresh`
  148. - `RAILS_ENV=production bundle exec rake mastodon:feeds:clear`
  149. 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.
  150. You can edit the cronjob file for the `mastodon` user by running `sudo crontab -e mastodon` (outside of the mastodon user).
  151. ## Things to look out for when upgrading Mastodon
  152. You can upgrade Mastodon with a `git pull` from the repository directory. You may need to run:
  153. - `RAILS_ENV=production bundle exec rails db:migrate`
  154. - `RAILS_ENV=production bundle exec rails assets:precompile`
  155. 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:
  156. sudo systemctl restart mastodon-*.service