MastodonがとてもよくできたSNS風ツールで普段私が使わない技術で作られていてなかなか面白そう、ということで構築してみました。
個人的にはよくある構築例で紹介されているDockerは少し触ってみてどうしても好きになれなかったこともあり、結局ESXi上にUbuntu Serverを立ててそこに構築しています。ほとんどの内容はこちらを参考にしていますが、nginxやpostgres、Ruby、node.jsなど普段LAMPを使用している私にはあまりなじみのない部分で一部うまくいかなかった点などを少し補足しています。
構築したmastdonインスタンスはpow.n-bahn.clubです。
1.事前準備
インターネットに公開する場合はドメイン名およびSSLサーバー証明書、中間証明書、鍵をPEM形式で準備しておき、自前で利用可能なメールサーバーも用意しておきます。
nginxではApacheと異なりSSLサーバー証明書の設定カ所に証明書と中間証明書が分かれていません。あらかじめ
$ cat server.crt ca-bundle.crt > server_chained.crt
のように全てのチェインを繋げた証明書ファイルにして準備しておきます。
VMはメモリ4096MB、ストレージは余裕があったので2TB確保しました。
Ubuntu Server 16.04 LTSをインストールします。外からssh接続できるようにする目的でOpenSSH serverのみ追加しインストールすると楽です。

インストール後は下記のコマンドで最新の状態にしておきます。
# apt-get update; apt-get -y upgrade
ユーザーmastodonを作成しておきます。
# adduser mastodon
2.必要なパッケージのインストールと設定
a) パッケージもろもろ
# apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs file git curl redis-server redis-tools postgresql postgresql-contrib autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev git-core letsencrypt nginx ruby-dev rbenv
のように実行して、あらかじめ必要なパッケージをまとめてインストールします。
b) postgresの設定
postgresデータベースにmastodonユーザーを作成し、パスワードも設定します。
# su - postgres postgres@mastodon:~$ psql psql (9.5.6) Type "help" for help. postgres=# CREATE USER mastodon CREATEDB; CREATE ROLE postgres=# alter role mastodon with password '<パスワード>'; ALTER ROLE postgres=# \q
c) node.js
rootに戻りnode.jsやyarnをインストールします。
# curl -sL https://deb.nodesource.com/setup_4.x | bash - # apt-get install nodejs # npm install -g yarn
d) redisの起動
redisを起動します。
# service redis-server start
e) nginxの設定
nginxの設定では/etc/nginx/sites-availableに新しくmastodon用の設定を追加します。
# cd /etc/nginx/sites-available # vi mastodon
下記の内容のうちexample.comの部分を自分のホスト名設定に置き換え、SSLサーバー証明書と鍵のパスをフルパスで指定します。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_protocols TLSv1.2;
ssl_ciphers EECDH+AESGCM:EECDH+AES;
ssl_ecdh_curve prime256v1;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
keepalive_timeout 70;
sendfile on;
client_max_body_size 0;
gzip off;
root /home/mastodon/live/public;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
location / {
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_pass_header Server;
proxy_pass http://localhost: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_pass http://localhost: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 *********** 503 504 /500.html;
}
その後、
# ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
で設定の有効化をしてnginxを再起動します。
# service nginx restart
f) systemdの設定
/etc/systemd/system/に3つのサービス起動用ファイルを作成します。
[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
[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
[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
g) rbenv環境のインストール
# su - mastodon
でmastodonユーザーになります。rbenvを.rbenvにインストールし、PATH環境変数への追記と実行を設定します。
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Rubyプラグインのインストール
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install 2.4.1
このコマンドは実行完了までしばらくかかります。
3.mastodonのインストール
/home/mastodon/liveにmastodonをインストールします。
$ cd $ git clone https://github.com/tootsuite/mastodon.git live $ cd live
この
$ cd live
を忘れると次のコマンドの実行がうまくいかないので要注意です。
$ gem install bundler $ bundle install --deployment --without development test $ yarn install
上記コマンドも完了までしばらくかかります。(このあたりは何をやっているのか深くは理解していないのでほとんど呪文です)
4.mastodonの初期設定
下記のコマンドを3回実行して3つのキーを作成します。
$ bundle exec rake secret
サンプル設定ファイルを自分用にコピーして修正します。
$ cp .env.production.sample .env.production
$ vi .env.production
主な変更箇所は下記の通りです
REDIS_HOST=localhost
DB_HOST=localhost
DB_USER=mastodon
DB_PASS=<2のb)で設定したもの>
DB_NAME=mastodon_production
LOCAL_DOMAIN=ホスト名
PAPERCLIP_SECRET=上の手順で生成した3つのキーのうち1つ目
SECRET_KEY_BASE=上の手順で生成した3つのキーのうち2つ目
OTP_SECRET=上の手順で生成した3つのキーのうち3つ目
SMTP_SERVER=<メールサーバーのIPアドレス>
SMTP_PORT=<メールサーバーの動作ポート>
SMTP_LOGIN=<メールサーバーログイン名>
SMTP_PASSWORD=<メールサーバーのログインパスワード>
SMTP_FROM_ADDRESS=<mastodonからのメールの送信元アドレス>
25番ポートで動作するSMTPサーバーに認証なしでの設定を試したところ、うまくいかなかったりしたので結局認証付きのメールサーバーを指定しました。
引き続き、データベースの初期設定とAssetのprecompile(←よくわかっていない)を行います。
$ RAILS_ENV=production bundle exec rails db:setup
$ RAILS_ENV=production bundle exec rails assets:precompile
mastodonユーザーのままcrontabジョブに下記のような行を追加します。bundleコマンドのフルパスは環境に合わせて適宜変更します。
5 0 * * * RAILS_ENV=production /home/mastodon/.rbenv/shims/bundle exec rake mastodon:daily
rootに戻り、mastodonサービスを登録・起動します。
# systemctl enable /etc/systemd/system/mastodon-*.service
# systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
5.管理者ユーザーへの昇格
まず、自分自身のアカウントを登録してから昇格します。再びmastodonユーザーになり~/live/に移動してから下記のコマンドを実行することで登録したユーザーを管理者に昇格できます。
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=<ユーザー名>
その後、
https://<mastodonのFQDN>/admin/settings
へアクセスすれば管理者画面へアクセスできるようになります。

コメント