2017
04.30

Mastodon インストール手順(Dockerを使わないバージョン)

テクノロジー

fluffy-elephant-friend.png
昨今のオープン技術には詳しくない筆者が各々の情報を元にMastodonを導入してみました!
日夜、対応?しているMastodonですがいくつかインスタンスを手順を変えながら立ち上げてみました。
以下の手順はDockerを使わない方法です。Dockerを使った方が情報がいっぱいあるので、あえてそれを使わない方法での手順をクリップしておきます。ある意味素人なので、よろしくお願いいたします。

■更新履歴

Version 1:2017-04-30

■ 動作環境

「ConohaVSP」の最小構成でスタートしてます。
  • CPU:1core相当
  • メモリ:512MB
  • ディスク:SSD 20GB

■まず、最初

(#で始まるものは、rootから、それ以外はUSER=mastodonにて実施)
まずはお約束の綺麗にする作業。
# yum clean all && yum -y update && reboot
色々必要なものを入れていきます。
# yum install ImageMagick
# yum install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
# yum install ffmpeg
「redis」は常駐起動状態が必要です。
# yum -y install epel-release && yum install redis
# systemctl start redis && systemctl enable $_
他にもどんどん入れていきますよ。
# yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
# yum install yum-utils

■データベースをインストール

# yum-config-manager --enablerepo=pgdg96
# yum install postgresql96-{contrib,devel,server}
# ln -s /usr/pgsql-9.6/bin/* /usr/local/bin/

# postgresql96-setup initdb
# systemctl start postgresql-9.6 && systemctl enable $_
# sudo su - postgres -c 'psql -c "CREATE USER mastodon CREATEDB;"'
# sed -i.org '/shared_preload_libraries/ s/^#//' /var/lib/pgsql/9.6/data/postgresql.conf
# sed -i "/shared_preload_libraries/ s/''/'pg_stat_statements'/" /var/lib/pgsql/9.6/data/postgresql.conf
インストールしたデータベースを再起動しておく。
# systemctl restart postgresql-9.6

■ウェブサーバー

ウェブサーバーをインストールします。今後何回か出てきますが、「_EOF_」の形で記述しておくと、インストールする際に資料として使う際にコピー&ペーストで出来て便利。
cat << "_EOF_" > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
_EOF_
さて、実際にインストールします。
yum install nginx
さて、他にもどんどん入れていきます。
curl -sL https://rpm.nodesource.com/setup_6.x | bash
yum install nodejs
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo
yum install bzip2 gcc-c++ git {openssl,readline,zlib}-devel
useradd mastodon
su - mastodon
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src && cd ~
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile && source ~/.bash_profile
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.4.1 && rbenv global $_ && rbenv rehash
gem install bundler

■ いよいよ、 Mastodonのインストール

ここからはユーザーを作って実行していく感じです。
cd ~ && git clone https://github.com/tootsuite/mastodon.git live && cd live
git checkout $(git tag | tail -n 1)
bundle install --deployment --without development test
yarn install
ここでしばらく待ちます。時間かかるみたい。
さて、ここからは自分の環境に合わせて実行していきます。直接、viでやるのでは無く、sedで変更点だけを入れていきます。僕にとっては新鮮な方法でした。
特に「sed -i “/^PAPERCLIP_SECRET=$/ s/$/`rake secret`/” .env.production」からの3行は一発で暗号キーを作ってそのまま入れるという手順は素晴らしい。
cp .env.production.sample .env.production
sed -i '/^REDIS_HOST=/ s/redis/localhost/' .env.production
sed -i '/^DB_HOST=/ s/db//' .env.production
sed -i '/^DB_USER=/ s/postgres/mastodon/' .env.production
sed -i '/^DB_NAME=/ s/postgres/mastodon/' .env.production
sed -i '/^LOCAL_DOMAIN=/ s/example.com/shigadon.com:3000/' .env.production
sed -i '/^LOCAL_HTTPS=/ s/true/false/' .env.production
sed -i "/^PAPERCLIP_SECRET=$/ s/$/`rake secret`/" .env.production
sed -i "/^SECRET_KEY_BASE=$/ s/$/`rake secret`/" .env.production
sed -i "/^OTP_SECRET=$/ s/$/`rake secret`/" .env.production
構築します。
bundle exec rails db:setup RAILS_ENV=production
bundle exec rails assets:precompile RAILS_ENV=production
Mastodonをサービスとして運用するために3つのファイルを作成します。

web.service

cat << "_EOF_" > /etc/systemd/system/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_
sidekiq.service
cat << "_EOF_" > /etc/systemd/system/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_

streaming.service

cat << "_EOF_" > /etc/systemd/system/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_
Mastodonを実際に実行し、実行用のサービスの登録です。
systemctl start mastodon-{web,sidekiq,streaming} && systemctl enable $_
ひとまず、僕はここでMastodonを止めておきました。
systemctl stop mastodon-{web,sidekiq,streaming}
これは、検証も含めてお約束だそうです(説明不足、すみません)。
setenforce 0
外部に対してhttpとhttpsを使うので穴を空けます。
firewall-cmd --permanent --add-service={http,https} && firewall-cmd --reload
データベース内の構築です。
sudo su - postgres -c 'psql -d mastodon -c "CREATE extension pg_stat_statements;"'
sudo su - postgres -c 'psql -d mastodon -c "SELECT pg_stat_statements_reset();"'
Let’s EncryptでSSLの認証キーを作成します。これは認証保証期間が短いので別途Cron等で更新をする必要があります。
yum install certbot
certbot certonly --standalone -d shigadon.com
nginxで使うMastodon専用のconfファイルを作成します。
ここ Production guide からひな形を持ってきて変更します。
cat << "_EOF_" > /etc/nginx/conf.d/mastodon.conf
map $http_upgrade $connection_upgrade {
 default upgrade;
''      close;
}

server {
 listen 80;
 listen [::]:80;
 server_name shigadon.com;
return 301 https://$host$request_uri;
}

server {
 listen 443 ssl;
 listen [::]:443 ssl;
 server_name shigadon.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/shigadon.com/fullchain.pem;
 ssl_certificate_key     /etc/letsencrypt/live/shigadon.com/privkey.pem;
 ssl_trusted_certificate /etc/letsencrypt/live/shigadon.com/chain.pem;
 ssl_stapling            on;
 ssl_stapling_verify     on;

 keepalive_timeout    70;
 sendfile             on;
 client_max_body_size 0;

 root /home/mastodon/live/public;

 gzip on;
 gzip_disable "msie6";
 gzip_vary on;
 gzip_proxied any;
 gzip_comp_level 6;
 gzip_buffers 16 8k;
 gzip_http_version 1.1;
 gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
#  add_header Content-Security-Policy "default-src 'none'; style-src 'self'; script-src 'self' https://example.com; img-src 'self' https://example.com; connect-src 'self' wss://example.com; font-src 'self'; frame-ancestors 'none'; media-src 'self';";

 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;
}

 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;
}

 error_page 500 501 502 503 504 /500.html;
}
_EOF_

■メール設定

受信は必要ないので「sendmail」で設定。

SMTP_SERVER=127.0.0.1
SMTP_PORT=25
SMTP_LOGIN=
SMTP_PASSWORD=
[email protected]
#SMTP_DOMAIN= # defaults to LOCAL_DOMAIN
SMTP_DELIVERY_METHOD=sendmail
SMTP_AUTH_METHOD=plain
SMTP_OPENSSL_VERIFY_MODE=none
SMTP_ENABLE_STARTTLS_AUTO=true

詳細を後日書きます。

■日次処理の登録

後日書きます。

■作業完了、最終起動

systemctl restart mastodon-{web,sidekiq,streaming}
systemctl restart nginx && systemctl enable $_

■バージョンアップ手順

後日書きます。

■謝辞

以下のURL先の方々の情報を参考にさせていただきました。本当にお世話になりました。

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。