搭建 Mastodon 实例 | How to install a Mastodon instance

搭建 Mastodon 实例比较耗内存以及宽带,要想好哦 ~

这里有几个推荐的中文 Mastodon 实例

一步一步安装 Mastodon 实例,Ubuntu 16.04 LTS,root 环境。推荐内存大于 2G,若为 KVM 内存小于 2G,可以设置个 SWAP

搭建 Mastodon

更新系统并安装 Nodejs

apt-get update && apt-get upgrade -y && apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev git curl wget zip vim -y && curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install nodejs && npm install -g yarn

安装 PostgresSQL 及 Redis 数据库

apt-get install redis-server redis-tools postgresql postgresql-contrib -y

创建数据库用户

su - postgres
psql
CREATE USER mastodon CREATEDB;
\q
exit

使用户可以免密码登陆

sed -i '/^local.*postgres.*peer$/a host all     all     127.0.0.1/32    ident' \
/etc/postgresql/9.?/main/pg_hba.conf

安装 ident daemon

apt-get install pidentd
systemctl enable pidentd
systemctl start pidentd
systemctl restart postgresql

安装 Ruby

安装依赖

apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev libidn11-dev pkg-config libprotobuf-dev protobuf-compiler -y

新建 Mastodon 用户并切换过去

adduser --disabled-password --disabled-login mastodon
su - mastodon

安装 rbenv and rbenv-build

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

退出以生效

exit

重新切换回去

su - mastodon
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

然后为 Mastodon 用户安装 ruby 2.5.1, install 的时候大概会卡 10 分钟

rbenv install 2.5.1
rbenv global 2.5.1

检测是否安装成功

ruby -v

开始安装 Mastodon (v2.4.2)

cd ~
git clone https://github.com/tootsuite/mastodon.git live
cd live
git checkout v2.4.2

安装 bundler

echo "gem: --no-document" > ~/.gemrc
gem install bundler --no-ri

开始安装

bundle install --deployment --without development test
yarn install

复制配置文件并编辑

cp .env.production.sample .env.production
vim .env.production

然后编辑以下内容

# 数据库
REDIS_HOST=localhost
REDIS_PORT=6379
DB_HOST=/var/run/postgresql
DB_USER=mastodon
DB_NAME=mastodon_production
DB_PASS=
DB_PORT=5432

# 域名

LOCAL_DOMAIN=yourdomain.com
LOCAL_HTTPS=true

# 发件设置,推荐使用 Mailgun
# E-mail configuration
SMTP_SERVER=mail.yourdomain.com
SMTP_PORT=587
SMTP_LOGIN=noreply@yourdomain.com
SMTP_PASSWORD=YourPassword
SMTP_FROM_ADDRESS=noreply@yourdomain.com

# 保存退出后,执行 3 次 'RAILS_ENV=production bundle exec rake secret' 并将结果分别填入下方
PAPERCLIP_SECRET=
SECRET_KEY_BASE=
OTP_SECRET=

# 保存退出后,执行 1 次 `RAILS_ENV=production bundle exec rake mastodon:webpush:generate_vapid_key` 并将结果填入下方内容
VAPID_PRIVATE_KEY=
VAPID_PUBLIC_KEY=

同步数据库

RAILS_ENV=production bundle exec rails db:setup

同步静态资源

RAILS_ENV=production bundle exec rails assets:precompile

退出 Mastodon 用户

exit

将 Mastodon 加入系统服务

Web service

vim /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
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Background service

vim /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

API service

vim /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

运行 Mastodon

systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service

为 Mastodon 加入定时任务

crontab -u mastodon -e
RAILS_ENV=production
@daily cd /home/mastodon/live && RAILS_ENV=production /home/mastodon/.rbenv/shims/bundle exec rails mastodon:media:remove_remote

安装 Nginx 及配置 SSL

安装 acme.sh 以生成 SSL 证书

curl -L get.acme.sh | bash - && cp /root/.acme.sh/acme.sh /usr/local/bin/ && chmod +x /usr/local/bin/acme.sh

安装 Nginx

wget -O - https://nginx.org/keys/nginx_signing.key | apt-key add - && echo "deb http://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx" > /etc/apt/sources.list.d/nginx.list && apt-get update && apt-get install nginx -y && service nginx restart

将域名解析到 VPS 所在 IP,能访问了以后,签发证书

acme.sh --issue -d example.com -w /usr/share/nginx/html

生成的

/root/.acme.sh/example.com/fullchain.cer
/root/.acme.sh/example.com/example.com.key

是我们需要的,

创建配置文件

vim /etc/nginx/conf.d/mastodon.conf
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  
  root /usr/share/nginx/html;
  
  # 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_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate /root/.acme.sh/example.com/example.com.cer;
  ssl_certificate_key /root/.acme.sh/example.com/example.com.key;

  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 @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

service nginx restart

然后注册一个账号,设置为管理员:

su - mastodon
cd ~/live
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=your-user-name

更新 Mastodon

su - mastodon
cd ~/live
gem install bundler --no-ri
git fetch
git checkout v1.X.X
bundle install --deployment --without development test
NODE_ENV=production npm upgrade --global yarn
yarn install
RAILS_ENV=production bundle exec rails assets:clean
RAILS_ENV=production bundle exec rails assets:precompile
RAILS_ENV=production bundle exec rails db:migrate
exit

然后重启

systemctl restart mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service

ALL I NEED IS YOUR MONEY

5 条评论

  1. 这个千万别删了,哈哈

    有没有搬家视频+文字教程啊

    全网都没有!

  2. BINARIZATION

    呃,这么多配置步骤的吗。可以用Docker啊

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.