闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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