KUSANAGIでmastodon動かしてみた
ますとどん!
今流行の分散型のSNSmastodonを、KUSANAGIで入れてみました。
手順は、さくらのクラウドに追加されたスタートアップスクリプトを参考にしています。
kusanagi設定
インスタンスとして2GBメモリのものを用意して、普通にkusanagi initとkusanagi provision までを実行します。
これで、Let’s Encrypt SSL証明書取得とNGINXの設定ができました。
provision は、LAMPを指定しますが、使用するのはNGINXの設定だけで、PHP7とmysqlは停止します。
yum update -y kusanagi init --tz tokyo --lang en --keyboard en --passwd passw0rd --no-phrase --nginx --php7 kusanagi provision --lamp --fqdn mstdn.myzkstr.tech --email EMAIL@example.com --dbname dummy --dbuser dummy --dbpasswd passw0d mstdn kusanagi ssl --http redirect --hsts weak systemctl stop mysql php7-fpm systemctl disable mysql php-fpm
追加パッケージインストール
以下のように、パッケージを追加インストールします。
nux-desktopは、ffmpeg関連のインストールに、nodesouce はnode.jsのインストールに必要なようです。
yum install -y http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
curl -sL https://rpm.nodesource.com/setup_6.x | bash -
yum install -y ImageMagick ffmpeg redis postgresql-{server,devel} authd nodejs {openssl,readline,zlib}-devel nginx jq bzip2
npm install -g yarn
Postgresql/Redisの設定
Postgresql の初期設定と起動をします。KVSのRedisもここで起動しておきます。
export PGSETUP_INITDB_OPTIONS="--encoding=UTF-8 --no-locale" postgresql-setup initdb sed -i "s/ident/trust/" /var/lib/pgsql/data/pg_hba.conf systemctl enable postgresql redis systemctl start postgresql redis su - postgres -c "createuser --createdb mastodon"
Ruby on Rails と Mastodonの設定
ユーザ mastdon を作成し、ここにRubyをインストールし、RailsとMastdonの構築を行います。
以下の手順では、renv でRuby2.4.1をインストールし、依存ライブラリをインストールします。
また、さくらのクラウドのスタートアップスクリプトでは、localhostのpostfixの設定をしますが、ここではGmailのメールサーバを使用しています。
事前に、Gmailアカウントの設定でアプリパスワードの発行が必要で、ここで発行したパスワードをSMTP_PASSWORDに設定します。また、SMTP_LOGINとSMTP_PASSWORDはパスワードを発行した際のGmailのメールアドレスを指定します。
この手順は、http://qiita.com/ymmtmdk/items/aa0d300450d370a1eca0の記事を参考にしています
useradd -g www mastodon
su - mastodon
※以下mastdonユーザ権限での動作
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
rbenv init - >> ~/.bash_profile
source ~/.bash_profile
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.4.1
rbenv global 2.4.1
rbenv rehash
git clone https://github.com/tootsuite/mastodon.git live
cd live
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
gem install bundler
bundle install --deployment --without development test
yarn install
cp .env.production.sample .env.production
sed -i "s/_HOST=[rd].*/_HOST=localhost/" .env.production
sed -i "s/=postgres$/=mastodon/" .env.production
sed -i "s/^LOCAL_DOMAIN=.*/LOCAL_DOMAIN=mstdn.myzkstr.tech/" .env.production
sed -i "s/^LOCAL_HTTPS.*/LOCAL_HTTPS=true/" .env.production
sed -i "s/^SMTP_SERVER.*/SMTP_SERVER=smtp.gmail.com/" .env.production
sed -i "s/^SMTP_LOGIN.*/SMTP_LOGIN=EMAILADDRESS/" .env.production
sed -i "s/^SMTP_PASSWORD.*/SMTP_PASSWORD=PASSWORD/" .env.production
sed -i "s/^SMTP_FROM_ADDRESS=.*/SMTP_FROM_ADDRESS=EMAILADDRESS/" .env.production
echo 'SMTP_OPENSSL_VERIVFY_MODE=none' >> .env.production
export SECRET_KEY_BASE=$(bundle exec rake secret)
sed -i "s/^SECRET_KEY_BASE=/SECRET_KEY_BASE=$(printf ${SECRET_KEY_BASE})/" .env.production
RAILS_ENV=production bundle exec rails db:setup
RAILS_ENV=production bundle exec rails assets:precompile
chown -R g+rx /home/mastodon
Mastodon サービスの定義と起動
以下のように、mastodonのサービスを定義し、起動・有効化します。Rails、Sidekick、Streamingの3つのサービスが必要になります。
SDIR=/etc/systemd/system
cat << "_EOF_" > ${SDIR}/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
_EOF_
cat << "_EOF_" > ${SDIR}/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
_EOF_
cat << "_EOF_" > ${SDIR}/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
_EOF_
systemctl enable mastodon-{web,sidekiq,streaming}
systemctl start mastodon-{web,sidekiq,streaming}
NGINX設定
KUSANAGIで生成したNGINXの設定は、LAMPつまりPHPを使うことを想定しています。そこで、上記で作成したサービスを使うように設定を変更します。
cat /etc/nginx/conf.d/mstdn_ssl.conf
#=======================================
# mstdn.myzkstr.tech SSL
#---------------------------------------
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl http2;
server_name mstdn.myzkstr.tech;
ssl_certificate /etc/letsencrypt/live/mstdn.myzkstr.tech/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mstdn.myzkstr.tech/privkey.pem;
ssl_dhparam /etc/kusanagi.d/ssl/dhparam.key;
ssl_session_tickets on;
ssl_session_ticket_key /etc/kusanagi.d/ssl_sess_ticket.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ct on;
ssl_ct_static_scts /etc/letsencrypt/live/mstdn.myzkstr.tech/scts;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-HA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK:!RC4";
ssl_prefer_server_ciphers on;
## OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
access_log /home/kusanagi/mstdn/log/nginx/ssl_access.log main;
error_log /home/kusanagi/mstdn/log/nginx/ssl_error.log warn;
charset UTF-8;
client_max_body_size 16M;
#root /home/kusanagi/mstdn/DocumentRoot;
root /home/mastodon/live/public;
index index.php index.html index.htm;
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_set_header Proxy "";
proxy_pass_header Server;
proxy_pass http://127.0.0.1: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;
add_header X-Signature KUSANAGI;
add_header Strict-Transport-Security "max-age=31536000";
}
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_set_header Proxy "";
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;
add_header X-Signature KUSANAGI;
add_header Strict-Transport-Security "max-age=31536000";
}
error_page 500 501 502 503 504 /500.html;
}
設定変更後、NGINXの再起動を行います。
systemctl restart nginx
マストドン!
これで、mastdon が起動しました。
KUSANAGIのSSLの設定をそのまま活かし、http/2で接続可能な状態です。

あとは mastodon ユーザの権限で以下のcron 実行を仕掛けています。
@daily cd /home/mastodon/live && RAILS_ENV=production /home/mastodon/.rbenv/shim s/bundle exec rake mastodon:daily > /dev/null
mackerelでの監視も行っています。nginx、postgresql、redis あたりはプラグインがあるのですが、Railなどはどうやって監視するのがいいのか検討中です。

