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.

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