サーバーの用意
VirtualBoxでCentOS Stream 9の最小構成を作成する。作業手順の確認が目的なのでドメインを取得していないので証明書はオレオレ証明書になる。すなわち他のサーバーとの会話は出来ない。
VPSに合わせてメモリ少な目にしているがVirtualBoxは仮想マシンの作り方でI/Oのパフォーマンスにかなり差が出るがその話はまた別の機会に。
OSセットアップ後の作業は
## 必要に応じてSWAPを増やしておく
# dd if=/dev/zero of=/swapfile bs=1M count=2048
# chmod 0600 /swapfile
# mkswap /swapfile
# swapon /swapfile
## 永続化する
# /etc/fstab
## 末尾に追加する
/swapfile swap swap defaults 0 0
# swapon --show
## 443を通す
# firewall-cmd --permanent --zone=public --add-port=443/tcp
## selinuxの無効化。本当はもっとちゃんとやるべきだがこれでも動く
# vi /etc/selinux/config
SELINUX=disabled
## selinuxの設定変更を適用
# reboot
## tarのインストール。昔は最小構成でも入っていたと思うけど
# dnf install tar
## 後で必要にリポジトリを入れてしまう
## libvips使うにはremiが必要でremiを使うにはepelが必要らしい
# dnf install epel-release
# dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
# dnf install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-9.noarch.rpm
## dnfで入るnode.jsはバージョンが古いのでnで管理出来るように入れ替える
# dnf install nodejs
# npm install -g n
# n stable
# dnf remove nodejs
# exec $SHELL -l
## その他、必要な諸々をインストール
# npm install -g yarn
## 最新はImageMagickではなくlibvispを使用する
# dnf install vips
# dnf install --enablerepo=crb ffmpeg
# dnf install libpq-devel
# dnf install libxml2-devel
# dnf install libxslt-devel
# dnf install git-core
# dnf install gcc-c++
# dnf install --enablerepo=crb protobuf-devel
# dnf install autoconf
# dnf install bison
# dnf groupinstall "Development Tools"
# dnf install --enablerepo=crb libyaml-devel
# dnf install readline-devel
# dnf install libffi-devel
# dnf install --enablerepo=crb gdbm-devel
## 普通にやると1.20なので切替
# dnf module enable nginx:1.26
# dnf install nginx
## 普通にやると6.2なので切替
# dnf module enable redis:remi-8.4
# dnf install redis
## 普通にやると13なので切替
# dnf module enable postgresql:16
# dnf install postgresql-server
# dnf install postgresql-contrib
# dnf install --enablerepo=crb libidn-devel
# dnf install libicu-devel
# dnf install jemalloc-devel
## インストールはこれくらい。後は作業
# corepack enable
# yarn set version classic
## mastodonユーザーの作成
# useradd -m -s /bin/bash mastodon
# su - mastodon
$ 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/plugins/ruby-build
$ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.4.7
$ rbenv global 3.4.7
$ gem install bundler --no-document
$ exit
## postgresqlのセットアップ
# export 'PGSETUP_INITDB_OPTIONS=--encoding=UTF-8 --no-locale'
# postgresql-setup initdb
# systemctl start postgresql
# su - postgres
$ psql
## setting列がCであることを確認する
postgres=# SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
postgres=# CREATE USER mastodon CREATEDB;
## \はバックスラッシュ
postgres=# \q
$ exit
## Mastodonのセットアップ
## Redisを起動しておかないとSETUPでエラーになる
# systemctl start redis
# su - mastodon
## Node.jsが使用するヒープメモリの確保
$ export NODE_OPTIONS="--max-old-space-size=4096"
$ git clone https://github.com/mastodon/mastodon.git live && cd live
[live]$ git checkout $(git tag -l | grep '^v[0-9.]*$' | sort -V | tail -n 1)
[live]$ bundle config set deployment true
[live]$ bundle config set without development test
[live]$ bundle install
[live]$ yarn install
[live]$ RAILS_ENV=production bin/rails mastodon:setup
Domain name:サーバーのIPアドレス
single user mode:y
Docker:n
PostgreSQL host:(enter)
PostgreSQL port:(enter)
PostgreSQL database:(enter)
PostgreSQL user:(enter)
Password of PostgreSQL user:(enter)
Resis host:(enter)
Redis port:(enter)
Redis password:(enter)
Do you want to store uploaded files on the cloud?:n
Do you want to send e-mails from localhost?:n
SMTP server:(enter)
SMTP port:(enter)
SMTP username:(enter)
SMTP password:(enter)
SMTP authentication:(enter)
SMTP OpenSSL verify mode:(enter)
Enable STARTTLS:(enter)
E-mail address to send e-mails "from":(enter)
Send a test e-mail with this configuration right now?:n
Do you want Mastodon to periodically check for important updates and notify you?:y
This configuration will be written to .env.production
Save configuration?:y
Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? (Y/n):y
Compile the assets now?:y
## メモリが少ないとこの処理が終わるまでとても時間がかかる
## お一人様なのでここでユーザーを作成
Do you want to create an admin user straight away?:n → y
Username: ユーザー名
E-mail: メールアドレス(ログイン時に必要)
## You can login with the password: でパスワードが返されるのでメモする(ログイン時に必要)
## ユーザーの有効化
[live]$ RAILS_ENV=production bundle exec bin/tootctl accounts modify ユーザー名 --approve
[live]$ exit
## オレオレ証明書の作成と設定
# mkdir /etc/nginx/ssl
# openssl genrsa -out /etc/nginx/ssl/server.key 2048
# openssl req -new -key /etc/nginx/ssl/server.key -out /etc/nginx/ssl/server.csr
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:(enter)
Organization Name (eg, company) [Default Company Ltd]:(enter)
Organizational Unit Name (eg, section) []:(enter)
Common Name (eg, your name or your server's hostname) []:(enter)
Email Address []:(enter)
A challenge password []:(enter)
An optional company name []:(enter)
# openssl x509 -days 3650 -req -signkey /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr -out /etc/nginx/ssl/server.crt
# cp /home/mastodon/live/dist/nginx.conf /etc/nginx/conf.d
# vi /etc/nginx/conf.d/nginx.conf
## 2箇所
server_name サーバーのIPアドレス;
## さっき作成した証明書
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
## nginxの設定
## Redhat系はuserを変更する必要がある
# vi /etc/nginx/nginx.conf
user mastodon;
## あちこちの権限も変更
# chown -R mastodon:root /var/lib/nginx
# cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
## mastodonの設定ファイルがNode.jsのパスを持っているので
# ln -s /usr/local/bin/node /usr/bin/node
## 各種サービスの起動
# systemctl daemon-reload
# systemctl start nginx
# systemctl start mastodon-web.service
# systemctl start mastodon-sidekiq.service
# systemctl start mastodon-streaming.service
## これで動くはず
## サーバー再起動後に自動で起動するように
# systemctl enable nginx
# systemctl enable redis
# systemctl enable postgresql
# systemctl enable mastodon-web.service
# systemctl enable mastodon-sidekiq.service
# systemctl enable mastodon-streaming.service
