公式のページ( https://docs.joinmastodon.org/admin/install/ )もよく確認の上で作業を進めましょう。英語ではありますが、説明はとても丁寧です。

 なお、作業用ユーザーではなくrootで作業している前提のページをまとめたため、場合によって作業用ユーザーでは権限が足りないとのエラーが出る可能性があります。その場合は、失敗したコマンドの頭に sudo を足してやり直してください。

1.Mastodonの前提条件(もしくは必要な環境)と言われる、背後で動いているプログラムたちをインストールしていきます。

 全体の下準備をします。


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

 Node.jsの下準備もします。


curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

  

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list

 PostgreSQLの下準備も必要です。


wget -O /usr/share/keyrings/postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc

  

echo "deb [signed-by=/usr/share/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/postgresql.list

  apt で入れることのできるシステムパッケージのインストールをします。


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

 Yarnを自動的に必要なバージョンでインストールしてくれるツールとして、corepackのインストールを行います。

 Yarnは、Node.jsで使われる大量のプログラム群を取りまとめたパッケージを管理するツールです。


corepack enable

2.Mastodonユーザーの作成と、データベースの準備を行います。

 VPSサーバー上でMastodonを動かすユーザーとしてmastodonを作成します。


adduser --disabled-password mastodon

 PostgreSQLにデータベースを作ります。


sudo -u postgres psql

  

CREATE USER mastodon CREATEDB;

  

¥q

3.Mastodonなどをインストールしていきます。

 mastodonユーザーにログインし、Mastodonのプログラムを持ってきます。


su - mastodon

  

git clone https://github.com/mastodon/mastodon.git live && cd live

  

git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)

 Rubyのバージョン管理をしてくれるrbenvなどインストールします。


git clone https://github.com/rbenv/rbenv.git ~/.rbenv

  

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 root)"/plugins/ruby-build

 Ruby本体をインストールします。


RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install

 RubyやJavaScriptのパッケージ群をインストールします。


bundle config deployment 'true'

  

bundle config without 'development test'

  

bundle install -j$(getconf _NPROCESSORS_ONLN)

  

yarn install

4.Mastodonの初期設定を行います。


RAILS_ENV=production bundle exec rake mastodon:setup

 この操作により設定ファイルが .env.production として作成されます。また、データベースの設定などもここで行われます。オブジェクトストレージなど一部オプションの設定も可能です。

 また、この初期設定時にMastodonのアカウントを作成するか問われます。 admin など、いくつかの特殊なアカウントはこの時点でしか作成できませんので、必要な場合はここで作成しておいてください。

5.httpsで接続できるようにするための設定を行います。

 作業用ユーザーへ戻ります。


exit

 Certbotで証明書を確保します。


certbot certonly --nginx -d example.com(←Mastodonのドメイン)

 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

  

rm /etc/nginx/sites-enabled/default

  

sudo nano /etc/nginx/sites-available/mastodon

 ここで /etc/nginx/sites-available/mastodon の中の example.com を全て自分のMastodonのドメインに書き換えてください。

 また、ssl_certificatessl_certificate_keyを含む行の先頭に//などコメントであることを示す記号があると思うので、それを削除してください。


ssl_certificate     /etc/letsencrypt/live/example.com(←Mastodonのドメイン)/fullchain.pem;

ssl_certificate_key /etc/letsencrypt/live/example.com(←Mastodonのドメイン)/privkey.pem;;

保存できたら、NGINXにもMastodonのデータに触れる権限を渡し、再起動します。


chmod o+x /home/mastodon

  

systemctl reload nginx

6.systemdの設定を行い、Mastodonを起動させます。


cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/

  

systemctl daemon-reload

  

systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

03-04-02・kmyblueを建てる

03-03-06・セキュリティ対策について