harumakidon管理メモ (original) (raw)

Mastodon自鯖harumakidon)を立てた時のメモです。

自鯖設立記事なんかは結構あるのですが、最新版を見かけなかったのと
参考にした記事ではエラーが出てその通りにやっても上手くいかない!とかがあったので、
知識ゼロ初心者目線でサーバー設立までのメモを残しておきます。

知識ある方が見たら余計なことしてたりするかもですし説明も足りない部分あると思いますが、あくまで知識ゼロ初心者目線なので許してください。

ではいきます!

サーバースペック

Xserver VPSXServer VPS | 圧倒的な性能・コスパのVPS)で借りました。

・OS Ubuntu24.04
・メモリ4GB
・CPU4コア
・SSD100GB

事前準備

前提条件として:今回オブジェクトストレージは使用しておらず、メールサーバーはGmailを使用しています。

ドメイン名決め、ドメイン取得

私はお名前.com(ドメイン取得は最安値0円~|ドメイン取るならお名前.com)でlayp.netを取得しました。
サブドメインを使用したい場合は○○.layp.netの○に入る文字も考えておきましょう。
弊サーバーの場合はdon.layp.netです。

VPS契約

上記の通り私はXserver VPSの4GBプランを契約しました。
SSH鍵を利用する設定にして鍵は保存しておきましょう。SSH接続の際に必要です。

DNS設定

反映に時間がかかるので、IPアドレスが分かった時点で設定しておくのがおすすめです。
xxx〜の部分はVPSIPアドレスを入れてください。

ドメインをそのまま使用する場合

ホスト名 種別 内容 TTL
なし A xxx.xxx.xxx.xxx 3600
なし TXT v=spf1 +ip4:xxx.xxx.xxx.xxx ~all 3600

サブドメインを使用する場合(ホスト名は弊サーバーの場合)

ホスト名 種別 内容 TTL
don A xxx.xxx.xxx.xxx 3600
don TXT v=spf1 +ip4:xxx.xxx.xxx.xxx ~all 3600

サービスによって設定画面は変わってくると思うのですが設定する場所は変わらないと思います(恐らく……)

メールサーバー準備

今記事ではGmailを使用するので、Gmailでアプリパスワードを取得しておきましょう。(アプリ パスワードでログインする - Gmail ヘルプ
私はついでに新規Gmailアカウントを作成し自鯖専用のメールアドレスを作成しました。

Windowsの場合)ターミナルソフトインストール

私の場合はTera Termを使いました。
他にもあるみたいですがここからはTera Termでの作業を前提として説明していきます。
とはいえやることはどれでもほぼ同じだと思います。

作業用ユーザーセットアップ

ターミナルソフトを使いSSH接続をします。
接続出来たら作業用ユーザーセットアップに入ります。

adduser ユーザー名 ←パスワード設定 usermod -aG sudo ユーザー名 su - ユーザー名 ssh-keygen ←パスフレーズ設定 mv ~/.ssh/id_ed25519.pub ~/.ssh/authorized_keys chmod 600 ~/ssh/authorized_keys

id_ed25519はid_rsaの場合もあります。確認しながら進めてもらえれば……

サーバー初期セットアップ

公式手順:Preparing your machine - Mastodon documentation

・システムアップデート

sudo apt update sudo apt upgrade

・fail2banインストール、設定

sudo apt install fail2ban sudo vim /etc/fail2ban/jail.local ←vimを使います。

[DEFAULT] destemail = your@email.here sendername = Fail2Ban

[sshd] enabled = true port = 22 mode = aggressive

Mastodon公式からコピペ 、 保存して終了

sudo systemctl restart fail2ban

iptablesインストール、設定

Mastodon公式そのままだと使えないみたいなので、こちらの記事(Mastodonインスタンスを立てたときの諸々メモ | ぽけ手帳)を参考に設定していきます。

sudo apt install iptables-persistent

ウインドウが開いたらNoを選択

sudo update-alternatives --config iptables

1を選択(/usr/sbin/iptables-legacy) ここから公式手順に戻る

sudo vim /etc/iptables/rules.v4

*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

(optional) Allow HTTP/3 connections from anywhere.

-A INPUT -p udp --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

Allow destination unreachable messages, especially code 4 (fragmentation required) is required or PMTUD breaks

-A INPUT -p icmp -m icmp --icmp-type 3 -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

Mastodon公式からコピペ、保存して終了

sudo iptables-restore < /etc/iptables/rules.v4 sudo vim /etc/iptables/rules.v6

*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 ::1/128 -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

(optional) Allow HTTP/3 connections from anywhere.

-A INPUT -p udp --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 icmpv6 -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

Mastodon公式からコピペ、保存して終了

sudo ip6tables-restore < /etc/iptables/rules.v6

Mastodonインストール

公式手順:Installing from source - Mastodon documentation

sudo su -

これ以降rootユーザで作業

apt install -y curl wget gnupg apt-transport-https lsb-release ca-certificates

node.jsインストール

Mastodon公式手順は非推奨なので、こちらも上記記事を参考に設定

mkdir -p etc/apt/keyrings curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg NODE_MAJOR=20 echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list

PostgreSQLインストール

よくわからんのでこちらも上記記事を参考に設定

sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

システムパッケージインストール

apt update apt install -y
imagemagick ffmpeg libvips-tools libpq-dev libxml2-dev libxslt1-dev file git-core
g++ libprotobuf-dev protobuf-compiler pkg-config gcc autoconf
bison build-essential libssl-dev libyaml-dev libreadline6-dev
zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev
nginx nodejs redis-server redis-tools postgresql postgresql-contrib
certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev

apt installは公式からコピペするのが確実です。
間違えたり抜けがあると今後の設定でパッケージがインストールされていない状態になってしまうのでめんどくさくなります。

corepack enable yarn set version classic apt install git

Rubyインストール

Mastodonシステム用ユーザーを作成、切り替え

adduser --disabled-login mastodon →すべて空欄でEnter su - mastodon

ここからRubyインストール

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 exec bash git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Rubyのインストールできるバージョンを調べるコマンドを入力
リストの中で最新のバージョンをメモしておきます。(今回の場合は3.3.5)
その後インストールに進みます。

rbenv install --list RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.3.5

インストール中ターミナルが止まっているように見えますが、そのまま待ちます。

rbenv global 3.3.5 gem install bundler --no-document exit

rootユーザーに戻ります。

Mastodonインストール

sudo -u postgres psql CREATE USER mastodon CREATEDB; exit su - mastodon

mastodonユーザーに戻ります。

git clone https://github.com/mastodon/mastodon.git live && cd live git checkout (gittag−l∣grep−v′rc[0−9]∗(git tag -l | grep -v 'rc[0-9](gittaglgrepvrc[09]' | grep -v 'beta[0-9]$' | sort -V | tail -n 1) bundle config deployment 'true' bundle config without 'development test' bundle install -j$(getconf _NPROCESSORS_ONLN) yarn install --pure-lockfile

オブジェクトストレージは使用しないのでこのままセットアップコマンドを実行します。

RAILS_ENV=production bundle exec rake mastodon:setup

セットアップで聞かれること

・Domain name
ドメイン名を入力します。
弊サーバーの場合、don.layp.net
ここで決めたドメイン名はサーバー爆破以外変更手段がないので慎重に決めましょう。
・Do you want to enable single user mode?
シングルユーザーモード(おひとり様)にするかどうかを選択します。 弊サーバーは人を呼びたかったのでNを選択しました。
人を呼ぶかどうか決めていない場合はNでいいと思います。絶対に一人運用!って方はyで。
・Are you using Docker to run Mastodon?
Docker上で動かすかどうかを聞かれています。
今記事ではDockerは使用していないので、Nを選択。
・PostgresSQL、Redis
デフォルト設定のままでいいので、
Redis configuration works! 🎆
の表示が出るまでEnter押しとけば大丈夫です。
・Do you want to store uploaded files on the cloud?
画像などのデータをクラウドストレージに保存するかどうかを聞かれています。
今記事では使わないので、Nを選択。
・Do you want to send e-mails from localhost?
メールをローカルホストから送るかどうかを聞かれています。
今記事ではGmailを使用するので、Nを選択して設定に進みます。
SMTP server:smtp.gmail.com
SMTP port:587(デフォルトでOK)
SMTP username:(自分のGmailアドレス)
SMTP password:(取得しておいたアプリパスワードをスペースなしで入力)
SMTP authentication:plain
SMTP OpenSSL verify mode:none
・Email address to send e-mails "from":デフォルト
・この設定の後、テストメールを送信するか聞かれるのでYを選択。
送ってほしいメールアドレスを入力して、送信出来たら成功。
送信できなかったらやり直すかどうか聞かれるのでやり直す。
テストメールが来ているかどうか確認して次に進みます。
Mastodonからの重要な更新を自動確認してお知らせしてほしいかどうか聞かれるのでYを選択。
・ここまでの設定を .env.production に保存するか聞かれるので、Yを選択。
nを選択してしまうとすべてやり直しになってしまうので気をつけてください。
・当該データベースがある場合は消去するがこのままデータベースの設定に進んでいいかどうか聞かれます。
Yを選択。
・メモリ食うけどCSSとJSのアセットをコンパイルするかどうか聞かれています。
Yを選択。これでAll done! You can now power on the Mastodon server 🐘と表示されればサーバーは立っています。おそらく
・管理ユーザーを作るか聞かれます。Yを選択。
・Username:弊サーバーの場合はharumaki2000。
・Email:アカウントに使う自分のメールアドレスを入力。
・パスワードがここで自動生成されて表示されるのでメモしておきましょう。後で設定で変えられます。

nginx、証明書の設定

ここからrootユーザーでの作業になります。
mastodonユーザーの場合はexitで抜けてrootユーザーに戻ってください。
公式の手順だと失敗するみたいなので、上記記事を参考に設定していきます。

systemctl stop nginx cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon vim /etc/nginx/sites-available/mastodon

ここでexample.comをすべて新しいドメイン名に書き換えて、下記のように設定

Uncomment these lines once you acquire a certificate:

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ←コメントアウトを外す ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ←コメントアウトを外す

certbot certonly --standalone -d 新しいドメイン nginx -t systemctl start nginx certbot --nginx -d 新しいドメイン

ここでは2を選択して上書き。

certbot renew --dry-run certbot renew /usr/bin/certbot renew --force-renewal --deploy-hook "systemctl restart nginx" vim /etc/cron.daily/letsencrypt-renew

以下コピペして保存

#!/bin/sh /usr/bin/certbot renew --deploy-hook "systemctl restart nginx"

cronを読み込む

systemctl restart cron

Mastodonをデーモンとして登録する

cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/ systemctl daemon-reload systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

以上!と思いきや……

WebUIが開かない

ロゴしか表示されない。

ここで、

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

を入力してあげることで無事にWebUI表示成功!!

お疲れさまでした!!

数日後kmyblueフォークを導入したのでまた別記事で覚書しておこうと思います。

メモ的に。

harumakidon
kmyblueフォーク実装Mastodonサーバー。
登録承認制、少人数サーバーです。don.layp.net

harumakidon管理人アカウント
基本ここにいます。don.layp.net

mstdn.jpアカウント
自鯖が落ちたときとかにいます。mstdn.jp

りずみすきーアカウント
音ゲーのリザルト貼りに行ったり音ゲーの話しに行ったりしてます。rhythmisskey.games

Twitterアカウント
Mastodon自鯖のお知らせだったりなんか自由に。あんまりいません。
https://twitter.com/satouchang_