Mastodon 生產部署指南

本問翻譯自: https://github.com/tootsuite/... 因爲版本迭代更新,本文檔可能會落後, 有能力的話推薦閱讀英文原文

Mastodon 生產指南

免責聲明:javascript

本指南是針對 Ubuntu Server 16.04 編寫的, 若是使用其餘操做系統,則可能會遇到問題。咱們歡迎對其餘發行版的指南做出貢獻。css

此文檔但願你有足夠高的技術管理 Linux 服務器。html

這本指南是什麼?

本指南介紹了 Mastodon 實例的部署過程。前端

咱們用 example.com 表示域名或者子域名。 example.com 應該替換成你本身的域名或者子域名。java

先決條件

本指南須要如下內容:node

DNS

在服務器上完成任何操做以前,應先添加 DNS 記錄。nginx

添加的記錄:git

  • A 記錄 (IPv4 地址) 解析到 example.com
  • AAAA 記錄 (IPv6 地址) 解析到 example.com

一個有用可是非必須的提醒

使用 tmux 將會對本指南有所幫助。github

不只如此, 若是您斷開鏈接, 這不只能夠幫助您找回失去的位置, 還能夠打開多個終端窗口, 用來切換上下文 (root 用戶和 mastodon 用戶).web

你能夠從包管理器安裝 tmux :

apt -y install tmux

依賴安裝

全部的依賴項都應該以 root 身份安裝.

node.js 倉庫

您須要添加一個外部存儲庫,以便咱們可使用須要的 node.js 版本。

咱們運行此腳原本添加存儲庫:

apt -y install curl
curl -sL https://deb.nodesource.com/setup_6.x | bash -

這個 node.js 存儲庫已經添加完成.

Yarn 倉庫

須要添加另外一個存儲庫, 以便咱們能夠得到 Mastodon 使用的 Yarn 版本.

你應該這樣添加存儲庫:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt update

各類其餘依賴

如今你須要安裝 Yarn 加上一些更多的軟件.

依賴關係說明

  • imagemagick - Mastodon 使用 imagemagick 進行圖像相關操做
  • ffmpeg - Mastodon 使用 ffmpeg 將 GIF 轉換爲 MP4
  • libprotobuf-dev 和 protobuf-compiler - Mastodon 用他們進行語言檢測
  • nginx - nginx 是咱們的前端 Web 服務器
  • redis-* - Mastodon 使用 redis 所謂其內存數據結構存儲
  • postgresql-* - Mastodon 使用 PostgreSQL 做爲 SQL 數據庫
  • nodejs - Node 用於 Mastodon 的 流式 API
  • yarn - Yarn 是 Node.js 的包管理器
  • 其餘的 -dev 包, g++ - 這些是使用 ruby-build 編譯 Ruby 所須要的.
apt -y install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev nginx redis-server redis-tools postgresql postgresql-contrib letsencrypt yarn libidn11-dev libicu-dev

須要非 root 用戶添加的依賴關係

首先建立這個用戶:

adduser mastodon

mastodon 用戶身份登陸:

sudo su - mastodon

咱們須要設置 rbenvruby-build:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
# 重啓 shell
exec bash
# 檢查 rbenv 是否正確安裝
type rbenv
# 安裝 ruby-build 爲 rbenv 插件
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

如今 rbenvruby-build 已經安裝成功, 咱們須要安裝
Mastodon 使用的 Ruby 版本。這個版本也須要安裝啓用。

要啓用 Ruby, 請運行:

rbenv install 2.5.0
rbenv global 2.5.0

這將須要一些時間. 在命令運行時, 伸展一下, 喝點水.

node.js 和 Ruby 依賴

如今 Ruby 已啓用, 咱們將克隆 Mastodon git 倉庫 並安裝 Rubynode.js 依賴.

運行如下命令克隆並安裝:

# 返回到 mastodon 用戶的家目錄
cd ~
# 克隆 mastodon git 倉庫到 ~/live
git clone https://github.com/tootsuite/mastodon.git live
# 改變當前目錄到 ~live
cd ~/live
# 遷出 mastodon 最後一個穩定 tag 版本
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
# 安裝 bundler
gem install bundler
# 使用 bundler 安裝其他的 Ruby 依賴項
bundle install --deployment --without development test
# 使用 yarn 安裝 node.js 依賴項
yarn install --pure-lockfile

以上就是 mastodon 用戶如今所須要作的, 你能夠 exit 返回到 root 用戶.

建立 PostgreSQL 數據庫

Mastodon 須要訪問 PostgreSQL 實例.

PostgreSQL 實例建立一個用戶:

# 用 postgres 用戶啓動 psql
sudo -u postgres psql

# 跟從提示
CREATE USER mastodon CREATEDB;
\q

請注意 咱們不設置任務形式的密碼, 這是由於咱們將使用 ident 身份驗證. 容許本地在沒有密碼的狀況下訪問數據庫.

nginx 配置

你須要配置 nginx 來訪問 Mastodon 實例.

提醒: 將出現的 example.com 替換爲你本身實例的域名或者子域名

cd/etc/nginx/sites-available 並打開一個新的文件:

nano /etc/nginx/sites-available/example.com.conf

複製粘貼一下內容並進行必要的修改:

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  # Useful for Let's Encrypt
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name example.com;

  ssl_protocols TLSv1.2;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 0;

  root /home/mastodon/live/public;

  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  add_header Strict-Transport-Security "max-age=31536000";

  location / {
    try_files $uri @proxy;
  }

  location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
    add_header Cache-Control "public, max-age=31536000, immutable";
    try_files $uri @proxy;
  }
  
  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    try_files $uri @proxy;
  }

  location @proxy {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";
    proxy_pass_header Server;

    proxy_pass http://127.0.0.1:3000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    tcp_nodelay on;
  }

  location /api/v1/streaming {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";

    proxy_pass http://127.0.0.1:4000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    tcp_nodelay on;
  }

  error_page 500 501 502 503 504 /500.html;
}

激活添加的 nginx 配置:

cd /etc/nginx/sites-enabled
ln -s ../sites-available/example.com.conf

此配置假設您正在使用 Let's Encrypt 做爲您的 TLS 證書提供程序.

如您要使用 Let's Encrypt 做爲您的 TLS 證書提供者, 請參閱下一個子部分. 不然請編輯 ssl_certificatessl_certificate_key 爲相應的值.

Let's Encrypt

若是您使用 Let's Encrypt 做爲您的 TLS 證書提供者, 則此部分與您相關.

生成證書

咱們須要生成 Let's Encrypt 證書.

確保 'example.com' 替換爲 Mastodon 實例的域名.

確保此時 nginx 已中止:

systemctl stop nginx

咱們將建立證書, 一次在獨立模式下使用 TLS SNI 驗證, 第二次使用 webroot 方法. 因爲
nginxLet's Encrypt 的工做原理, 這是必須的.

letsencrypt certonly --standalone -d example.com

成功完成後, 咱們將使用 webroot 方法. nginx 須要處於運行狀態:

systemctl start nginx
# letsencrypt 工具將詢問您是否要發出新的證書, 請選擇該選項
letsencrypt certonly --webroot -d example.com -w /home/mastodon/live/public/

自動更新 Let's Encrypt 證書

Let's Encrypt 證書的有效期爲 90 天.

您須要在到期日期以前更新證書. 不這樣作會使您實例的用戶沒法訪問其餘與您聯合的實例.

咱們能夠建立一個天天運行的 cron 做業:

nano /etc/cron.daily/letsencrypt-renew

將此腳本複製並粘貼到該文件中:

#!/usr/bin/env bash
letsencrypt renew
systemctl reload nginx

保存文件並推出.

該腳本添加執行權限並從新啓動 cron 守護程序, 以便腳本天天運行:

chmod +x /etc/cron.daily/letsencrypt-renew
systemctl restart cron

就是這樣. 您的服務器將續訂您的 Let's Encrypt 證書.

Mastodon 應用配置

咱們將配置 Mastodon 應用程序.

爲此咱們切換到 mastodon 系統用戶:

sudo su - mastodon

將當前目錄更改成 ~live 並編輯 Mastodon 應用配置:

cd ~/live
cp .env.production.sample .env.production
RAILS_ENV=production bundle exec rake mastodon:setup

會有一個交互的嚮導引導你完成基本和必要的選項設置, 生成新的應用 secrets, 設置數據庫並編譯資源.

資源預編譯須要幾分鐘, 因此這是休息一下的好時機.

Mastodon 系統服務文件

咱們須要爲每一個 Mastodon 服務提供三個 systemd 服務文件.

如今切換回 root 用戶.

對於 Mastodon web 服務, 將如下內容放在 /etc/systemd/system/mastodon-web.service:

[Unit]
Description=mastodon-web
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="RAILS_ENV=production"
Environment="PORT=3000"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
ExecReload=/bin/kill -SIGUSR1 $MAINPID
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

對於 Mastodon 後臺隊列服務, 將如下內容放在 /etc/systemd/system/mastodon-sidekiq.service:

[Unit]
Description=mastodon-sidekiq
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="RAILS_ENV=production"
Environment="DB_POOL=5"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

對於 Mastodon 流 API 服務, 將如下內容放在 /etc/systemd/system/mastodon-streaming.service:

[Unit]
Description=mastodon-streaming
After=network.target

[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="NODE_ENV=production"
Environment="PORT=4000"
ExecStart=/usr/bin/npm run start
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

如今您須要啓用這些服務:

systemctl enable /etc/systemd/system/mastodon-*.service

如今啓動服務:

systemctl start mastodon-*.service

檢查它們是否正常運行:

systemctl status mastodon-*.service

就這些! 若是一切都正常完成, 當您在網絡瀏覽器中訪問 https://example.com 時, 會出現一個 Mastodon 實例.

祝賀你並歡迎來賓!

相關文章
相關標籤/搜索