VPS(Arch Linux)でNon DockerなMastodonを立てたときのメモ(アップデート方法、証明書自動化を追記)

自分でいじれる丼が欲しかったので雑にやりました。 なにか問題が起きたときに追跡しづらいみたいなことを聞いたのと、RAMが小さいVPSなので、Docker化はしていません。

下準備

  1. Linodeでも何でも良いので、VPSにArch Linuxのイメージをバッとインストールする。前回Misskeyを立てたときに契約してたやつを使うのでRAM2GB, SSD50GBのやつを使う。
  2. Webコンソールでrootにログインしたあと、# pacman -Syuしてパッケージの更新をする。
  3. useradd -m -G wheelmastodonユーザーを作成する。
  4. visudoしてwheelにsudoつかうのを許可する(ローカルユーザー visudoとかで検索してね!)。
  5. 手元のPCのターミナルでssh-keygen -t ed25519 -C "hogehoge"した後、公開鍵とかsshの設定をよしなにする(公開鍵をサーバーのauthorized_keysに貼り付けたり、パーミッションとかを確認する)(Authorized_keysとかポート番号とか気をつけてね)。
  6. VPSを再起動する。

    ここからsshをつかってやります。

    基本はjoinmastodonを見ながらやります。

  7. 手元のPCからssh XXXX(サーバーの宛先)した後、$ sudo pacman -S fail2banでfail2banをインストールする。
[DEFAULT]
destemail = your@email.here
sendername = Fail2Ban

[sshd]
enabled = true
port = 22

[sshd-ddos]
enabled = true
port = 22
  1. 上の内容を/etc/fail2ban/jail.localに保存する。
  2. fail2banを再起動する。
  3. $ sudo pacman -S iptablesiptablesを入れて
*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

と書いて設定したあと、$ sudo iptables-restore < /etc/iptables/rules.v4で有効化する。
11. $ sudo pacman -S nodejs-lts-dubnium make imagemagick ffmpeg file git libpqxx libxml2 libxslt protobuf pkg-config gcc autoconf bison libyaml readline zlib nginx redis postgresql postgresql-docs postgresql-libs certbot certbot-nginx yarn libidn11 jemalloc libidnで依存ライブラリをねじ込む。
12. rbenvの設定をよしなにして2.6.1に合わせたあと(2019年6月現在)、gem install bundler --no-documentでbundlerをインストールする。

PostgreSQLの設定

  1. $ sudo su - postgresでユーザーを切り替え、$ initdb --locale jp_JP.UTF-8 -E UTF-8 -D /var/lib/postgres/dataでデータベースの初期化を行う。
    2019年6月26日追記 : 初期化を行ったあと、$ sudo systemctl (start|enable) postgresql.serviceで起動・有効化してください。
  2. $ psqlと打ってPostgreSQLに接続したあと
> CREATE USER mastodon CREATEDB;
> \q

と打ってデータベースを作成。
15. $ exitmastodonユーザーに戻る。

Redisの設定

  1. これを参考にしながらredisの設定をして動かす(enableもしておくと吉?)。

Mastodonの設定

  1. $ git clone https://github.com/tootsuite/mastodon ~/liveMastodonの引っ張ってくるその後$ cd ~/liveMastodon直下に移動する。
  2. $ git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)で最新版のMastodonに切り替える。
bundle install \
  -j$(getconf _NPROCESSORS_ONLN) \
  --deployment --without development test

20.yarn global add node-gypした後、yarn install --pure-lockfileRubyJavascriptの依存ライブラリをプロジェクトに突っ込む。
21. RAILS_ENV=production bundle exec rake mastodon:setupで設定をする。
22. PostgreSQLとかRedisは特にいじってないので # Enter連打でオッケー
23. メールに関してはsmtpサーバーとメールアドレス、パスワードが分かってればなんかできた(お一人丼ならあまり必要性がない)。
24. そのままDBのセットアップとかプリコンパイルをするか聞かれるのでYesを選択。
25. 管理者アカウントの作成。なんでも良いけど吐き出されるパスワードは控えておこう。
26. 設定の出力が終わったらrootになり、/etc/nginx/conf.dディレクトリを作る。
27. cp /home/mastodon/live/dist/nginx.conf /etc/nginx/conf/mstdn.confでコピーしたあと、自分のドメイン用に書き換える。
適当に暗号スイートをいじったりして楽しむ。
28. nginx -tで設定ファイルのテストをする(SSLの認証を取るときのために、443番ポートのsslの記述を外しておく)。
29. certbot --nginx -d mstdn.jnsk.info -w /home/mastodon/liveSSL証明書を発行する。
32. 証明書を発行出来たらconfのsslの設定を直す。
30. /home/mastodon/live/dist/以下にあるserviceファイルを/etc/systemd/system/にコピーする。
31. 3つあるうち全部enableする。
$ sudo systemctl enable mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
33. /home/mastodon以下に$ chmod +x -R ~/で実行権限を付ける(ここはちょっと危ないかもしれない)。
34. $ sudo systemctl start nginxでnginxをスタートさせた後、各サービスもstartさせる。
$ sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service

## 完走した感想
死ぬほど面倒くさい。
証明書はLet's Encryptを使っているので自動更新の設定をしないと行けないんですが、これまた面倒で後回しになってる → 追記でやりました。

※追記 アップデート方法

参考1, 参考2

$ sudo pacman -Syu or $ yay (yayの入れ方はググったら5番目以内に出てくるやつを参考にしてほしい)  
$ cd ~/live  
$ git fetch && git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)※なんかmodifiedが大量に出てきたので$ git stashしたりした  
$ bundle install  
$ yarn install  
$ RAILS_ENV=production bundle exec rails db:migrate  
$ RAILS_ENV=production bundle exec rails assets:precompile  
$ sudo systemctl restart mastodon-*.service  
$ RAILS_ENV=production bin/tootctl cache clear  

追記の追記 証明書更新の自動化

Arch Wikiを信じよ ということでArchWikiを参考にしています。
Arch LinuxなのでSytemd Timerを使います。SysVinit?知らない子ですね...
基本は/etc/systemd/system/に更新手順を書いた設定ファイルを2つ作成(serviceとtimer)する→それを有効化・起動
するで自動化の設定は終わりです。
1. root権限で/etc/systemd/system/certbot.serviceを開いて以下の内容を書いて保存。

[Unit]
Description=Let's Encrypt renewal

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --agree-tos --deploy-hook "systemctl reload nginx.service"
  1. 同様に、root権限で/etc/systemd/system/certbot.timerを以下のように書いて保存(参考記事ではデイリーだけどウィークリーでも問題ないかなって思って、参考記事から少し改変してます)。
[Unit]  
Description=Weekly renewal of Let's Encrypt's certificates  
  
[Timer]  
OnCalendar=weekly  
RandomizedDelaySec=1h  
Persistent=true  
  
[Install]  
WantedBy=timers.target  
  1. root権限でcertbot.timerを有効化・起動する。
# systemctl enable certbot.timer  
# systemctl start certbot.timer