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.

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