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.

188 lines
5.7 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. error_page 500 501 502 503 504 /500.html;
  39. }
  40. ```
  41. ## Running in production without Docker
  42. 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`.
  43. ## General dependencies
  44. curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
  45. sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs
  46. sudo npm install -g yarn
  47. ## Redis
  48. sudo apt-get install redis-server redis-tools
  49. ## Postgres
  50. sudo apt-get install postgresql postgresql-contrib
  51. ## Rbenv
  52. 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)
  53. [1]: https://github.com/rbenv/rbenv#installation
  54. [2]: https://github.com/rbenv/ruby-build#installation
  55. [3]: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
  56. Then once `rbenv` is ready, run `rbenv install 2.3.1` to install the Ruby version for Mastodon.
  57. ## Git
  58. You need the `git-core` package installed on your system. If it is so, from the `mastodon` user:
  59. cd ~
  60. git clone https://github.com/Gargron/mastodon.git live
  61. cd live
  62. Then you can proceed to install project dependencies:
  63. gem install bundler
  64. bundle install --deployment --without development test
  65. yarn install
  66. ## Configuration
  67. Then you have to configure your instance:
  68. cp .env.production.sample .env.production
  69. nano .env.production
  70. 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:
  71. rake secret
  72. To get a random string.
  73. ## Setup
  74. And setup the database for the first time, this will create the tables and basic data:
  75. RAILS_ENV=production bundle exec rails db:setup
  76. Finally, pre-compile all CSS and JavaScript files:
  77. RAILS_ENV=production bundle exec rails assets:precompile
  78. ## Systemd
  79. Example systemd configuration for the web workers, to be placed in `/etc/systemd/system/mastodon-web.service`:
  80. ```systemd
  81. [Unit]
  82. Description=mastodon-web
  83. After=network.target
  84. [Service]
  85. Type=simple
  86. User=mastodon
  87. WorkingDirectory=/home/mastodon/live
  88. Environment="RAILS_ENV=production"
  89. Environment="PORT=3000"
  90. ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
  91. TimeoutSec=15
  92. Restart=always
  93. [Install]
  94. WantedBy=multi-user.target
  95. ```
  96. Example systemd configuration for the background workers, to be placed in `/etc/systemd/system/mastodon-sidekiq.service`:
  97. ```systemd
  98. [Unit]
  99. Description=mastodon-sidekiq
  100. After=network.target
  101. [Service]
  102. Type=simple
  103. User=mastodon
  104. WorkingDirectory=/home/mastodon/live
  105. Environment="RAILS_ENV=production"
  106. Environment="DB_POOL=5"
  107. ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q push
  108. TimeoutSec=15
  109. Restart=always
  110. [Install]
  111. WantedBy=multi-user.target
  112. ```
  113. This allows you to `sudo systemctl enable mastodon-*.service` and `sudo systemctl start mastodon-*.service` to get things going.
  114. ## Cronjobs
  115. I recommend creating a couple cronjobs for the following tasks:
  116. - `RAILS_ENV=production bundle exec rake mastodon:media:clear`
  117. - `RAILS_ENV=production bundle exec rake mastodon:push:refresh`
  118. - `RAILS_ENV=production bundle exec rake mastodon:feeds:clear`
  119. 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.
  120. You can edit the cronjob file for the `mastodon` user by running `sudo crontab -e mastodon` (outside of the mastodon user).
  121. ## Things to look out for when upgrading Mastodon
  122. You can upgrade Mastodon with a `git pull` from the repository directory. You may need to run:
  123. - `RAILS_ENV=production bundle exec rails db:migrate`
  124. - `RAILS_ENV=production bundle exec rails assets:precompile`
  125. 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:
  126. sudo systemctl restart mastodon-*.service