Mastodon实例安装指南
浏览量:1203
翻译:https://github.com/tootsuite/documentation/blob/master/Running-Mastodon/Production-guide.md
Mastodon 生产指南
免责声明:
本指南是针对 Ubuntu Server 16.04 编写的, 如果使用其他操作系统,则可能会遇到问题。我们欢迎对其他发行版的指南作出贡献。
此文档希望你有足够高的技术管理 Linux 服务器。
这本指南是什么?
本指南介绍了 Mastodon 实例的部署过程。
我们用 example.com 表示域名或者子域名。 example.com 应该替换成你自己的域名或者子域名。
先决条件
本指南需要以下内容:
Ubuntu Server 16.04 服务器.
服务器的 ROOT 访问权限.
解析到实例的域名.
DNS
在服务器上完成任何操作之前,应先添加 DNS 记录。
添加的记录:
A 记录 (IPv4 地址) 解析到 example.com
AAAA 记录 (IPv6 地址) 解析到 example.com
一个有用但是非必须的提醒
使用
tmux
将会对本指南有所帮助。不仅如此, 如果您断开连接, 这不仅可以帮助您找回失去的位置, 还可以打开多个终端窗口, 用来切换上下文 (root 用户和 mastodon 用户).
你可以从包管理器安装 tmux :
apt -y install tmux1
依赖安装
所有的依赖项都应该以 root 身份安装.
node.js 仓库
您需要添加一个外部存储库,以便我们可以使用需要的 node.js 版本。
我们运行此脚本来添加存储库:
apt -y install curl curl -sL https://deb.nodesource.com/setup_6.x | bash -12
这个 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.listapt update123
各种其他依赖
现在你需要安装 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-dev1
需要非 root 用户添加的依赖关系
首先创建这个用户:
adduser mastodon1
以 mastodon
用户身份登录:
sudo su - mastodon1
我们需要设置 rbenv
和 ruby-build
:
git clone https://github.com/rbenv/rbenv.git ~/.rbenvcd ~/.rbenv && src/configure && make -C srcecho 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrc# 重启 shellexec bash# 检查 rbenv 是否正确安装type rbenv# 安装 ruby-build 为 rbenv 插件git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build12345678910
现在 rbenv
和 ruby-build
已经安装成功, 我们需要安装
Mastodon 使用的 Ruby 版本。这个版本也需要安装启用。
要启用 Ruby, 请运行:
rbenv install 2.4.2rbenv global 2.4.212
这将需要一些时间. 在命令运行时, 伸展一下, 喝点水.
node.js 和 Ruby 依赖
现在 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-lockfile1234567891011121314
以上就是 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.conf12
此配置假设您正在使用 Let’s Encrypt 作为您的 TLS 证书提供程序.
如您要使用 Let’s Encrypt 作为您的 TLS 证书提供者, 请参阅下一个子部分. 否则请编辑 ssl_certificate
和 ssl_certificate_key
为相应的值.
Let’s Encrypt
如果您使用 Let’s Encrypt 作为您的 TLS 证书提供者, 则此部分与您相关.
生成证书
我们需要生成 Let’s Encrypt 证书.
确保 ‘example.com’ 替换为 Mastodon 实例的域名.
确保此时 nginx 已停止:
systemctl stop nginx1
我们将创建证书, 一次在独立模式下使用 TLS SNI 验证, 第二次使用 webroot 方法. 由于
nginx 和 Let’s Encrypt 的工作原理, 这是必须的.
letsencrypt certonly --standalone -d example.com1
成功完成后, 我们将使用 webroot 方法. nginx 需要处于运行状态:
systemctl start nginx # letsencrypt 工具将询问您是否要发出新的证书, 请选择该选项 letsencrypt certonly --webroot -d example.com -w /home/mastodon/live/public/123
自动更新 Let’s Encrypt 证书
Let’s Encrypt 证书的有效期为 90 天.
您需要在到期日期之前更新证书. 不这样做会使您实例的用户无法访问其他与您联合的实例.
我们可以创建一个每天运行的 cron 作业:
nano /etc/cron.daily/letsencrypt-renew1
将此脚本复制并粘贴到该文件中:
#!/usr/bin/env bashletsencrypt renew systemctl reload nginx123
保存文件并推出.
该脚本添加执行权限并重新启动 cron 守护程序, 以便脚本每天运行:
chmod +x /etc/cron.daily/letsencrypt-renew systemctl restart cron12
就是这样. 您的服务器将续订您的 Let’s Encrypt 证书.
Mastodon 应用配置
我们将配置 Mastodon 应用程序.
为此我们切换到 mastodon
系统用户:
sudo su - mastodon1
将当前目录更改为 ~live
并编辑 Mastodon 应用配置:
cd ~/live cp .env.production.sample .env.productionnano .env.production123
为了本指南的目录, 这些是要编辑的值:
# Your Redis host REDIS_HOST=127.0.0.1 # Your Redis port REDIS_PORT=6379 # Your PostgreSQL host DB_HOST=/var/run/postgresql # Your PostgreSQL user DB_USER=mastodon # Your PostgreSQL DB name DB_NAME=mastodon_production # Leave DB password empty DB_PASS= # Your DB_PORT DB_PORT=5432 # Your instance's domain LOCAL_DOMAIN=example.com # We have HTTPS enabled LOCAL_HTTPS=true # Application secrets # Generate each with `RAILS_ENV=production bundle exec rake secret` PAPERCLIP_SECRET= SECRET_KEY_BASE= OTP_SECRET= # Web Push VAPID keys # Generate with `RAILS_ENV=production bundle exec rake mastodon:webpush:generate_vapid_key` VAPID_PRIVATE_KEY= VAPID_PUBLIC_KEY= # All SMTP details, Mailgun and Sparkpost have free tiers SMTP_SERVER= SMTP_PORT= SMTP_LOGIN= SMTP_PASSWORD= SMTP_FROM_ADDRESS= 我们现在需要首次设置 PostgreSQL 数据库:
RAILS_ENV=production bundle exec rails db:setup1
我们需要预编译所有的 CSS 和 JavaScript 文件:
RAILS_ENV=production bundle exec rails assets:precompile1
资源预编译需要几分钟, 所以这是休息一下的好时机.
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 后台队列服务, 将以下内容放在
[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 服务, 将以下内容放在
[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-*.service1
现在启动服务:
systemctl start mastodon-*.service1
检查它们是否正常运行:
systemctl status mastodon-*.service1
就这些! 如果一切都正常完成, 当您在网络浏览器中访问 https://example.com
时, 会出现一个 Mastodon 实例.
祝贺你并欢迎来宾!
神回复
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。