本问翻译自: https://github.com/tootsuite/... 因为版本迭代更新,本文档可能会落后, 有能力的话推荐阅读英文原文
免责声明:javascript
本指南是针对 Ubuntu Server 16.04 编写的, 若是使用其余操做系统,则可能会遇到问题。咱们欢迎对其余发行版的指南做出贡献。css
此文档但愿你有足够高的技术管理 Linux 服务器。html
本指南介绍了 Mastodon 实例的部署过程。前端
咱们用 example.com 表示域名或者子域名。 example.com 应该替换成你本身的域名或者子域名。java
本指南须要如下内容:node
在服务器上完成任何操做以前,应先添加 DNS 记录。nginx
添加的记录:git
一个有用可是非必须的提醒
使用
tmux
将会对本指南有所帮助。github不只如此, 若是您断开链接, 这不只能够帮助您找回失去的位置, 还能够打开多个终端窗口, 用来切换上下文 (root 用户和 mastodon 用户).web
你能够从包管理器安装 tmux :
apt -y install tmux
全部的依赖项都应该以 root 身份安装.
您须要添加一个外部存储库,以便咱们可使用须要的 node.js 版本。
咱们运行此脚原本添加存储库:
apt -y install curl curl -sL https://deb.nodesource.com/setup_6.x | bash -
这个 node.js 存储库已经添加完成.
须要添加另外一个存储库, 以便咱们能够得到 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 加上一些更多的软件.
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
首先建立这个用户:
adduser mastodon
以 mastodon
用户身份登陆:
sudo su - mastodon
咱们须要设置 rbenv
和 ruby-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
如今 rbenv
和 ruby-build
已经安装成功, 咱们须要安装
Mastodon 使用的 Ruby 版本。这个版本也须要安装启用。
要启用 Ruby, 请运行:
rbenv install 2.5.0 rbenv global 2.5.0
这将须要一些时间. 在命令运行时, 伸展一下, 喝点水.
如今 Ruby 已启用, 咱们将克隆 Mastodon git 仓库 并安装 Ruby 和 node.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 用户.
Mastodon 须要访问 PostgreSQL 实例.
为 PostgreSQL 实例建立一个用户:
# 用 postgres 用户启动 psql sudo -u postgres psql # 跟从提示 CREATE USER mastodon CREATEDB; \q
请注意 咱们不设置任务形式的密码, 这是由于咱们将使用 ident 身份验证. 容许本地在没有密码的状况下访问数据库.
提醒: 将出现的 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_certificate
和 ssl_certificate_key
为相应的值.
若是您使用 Let's Encrypt 做为您的 TLS 证书提供者, 则此部分与您相关.
咱们须要生成 Let's Encrypt 证书.
确保 'example.com' 替换为 Mastodon 实例的域名.
确保此时 nginx 已中止:
systemctl stop nginx
咱们将建立证书, 一次在独立模式下使用 TLS SNI 验证, 第二次使用 webroot 方法. 因为
nginx 和 Let'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 证书的有效期为 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
系统用户:
sudo su - mastodon
将当前目录更改成 ~live
并编辑 Mastodon 应用配置:
cd ~/live cp .env.production.sample .env.production RAILS_ENV=production bundle exec rake mastodon:setup
会有一个交互的向导引导你完成基本和必要的选项设置, 生成新的应用 secrets, 设置数据库并编译资源.
资源预编译须要几分钟, 因此这是休息一下的好时机.
咱们须要为每一个 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 实例.
祝贺你并欢迎来宾!