多分いろんなタイトルと被っちゃってると思う。でも伝わる文言がこれなんだ。許してほしい。
いちWebServiceインスタンスのためにOS入れ替えなんてしてられるか!
所詮Ruby on Rails、依存をかっさばきゃあいいんでしょう!
Production-guide.mdに沿ってすすめる。
基本的にPackage Managerを使って解決していきたい。
環境
TL;DR
sudo useradd mastodon
sudo passwd mastodon
外部からsshでパスワードログインさせない、だとかは割愛。
Software Collectionを導入しておく
CentOS6.9の標準ではバージョンの足りないパッケージがちらほらあるので、Software Collectionを導入しておく。
sudo yum install centos-release-scl
sudo yum-config-manager --enable rhel-server-rhscl-6-rpms
WebSocket対応のリバースプロキシを提供するmod_proxy_wstunnel
が必要だが、Apache 2.4.5
以降で有効とのこと。
この件に関しては他でも流用できそうなので別記事にした。
yajamon.hatenablog.com
General Depenciesを解決する
資料を元に解決する。
sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs file git curl
curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
sudo apt-get install nodejs
sudo npm install -g yarn
https://www.imagemagick.org/script/index.php
画像操作周りをMastodonで取り扱う際に使ってるのかな。
base
リポジトリの6.7.2.7
を使う。
導入方法
sudo yum install ImageMagick
https://ffmpeg.org/
動画周りの処理を任せてるのかな。
ubuntuでの導入方法しか書いてない理由はこいつのせいじゃないかなと思う。
導入方法
公式ページのDownloadを見るとRPMFusionを案内しているのでそちらから導入してみる。
RPMFusion
http://qiita.com/m2wasabi/items/6d6a68da2e9dada92369#rpmfusion
を参考にした。
--enablerepo
前提でインストールするので元記事のyum-plugin-*
は除外
まず自身の環境チェック
cat /etc/redhat-release
arch
RPM Fusionのページから、必要なリポジトリがどれか判断する。
- Browse available packagesからffmpegを探してみる
CentOS 6.9
でx86_64
free
にありそう
applications/multimedia
にありそう
- あった
PGP-Keyのインストール
sudo gpg --keyserver pgp.mit.edu --recv-keys 849C449F
リポジトリを追加する
sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-6.noarch.rpm
rpmfusionは初期状態無効とする。
sudo vi /etc/yum.repo.d/rpmfusion-free-updates.repo
[rpmfusion-free-updates]
# enabled=1
enabled=0
yum-config-manager
使えっても良かったのかな。
sudo yum --enablerepo=rpmfusion-free-updates install ffmpeg
PostgreSQLのライブラリらしい。この文言のままではyumで見つからない。
- yumで対応するのは
postgresql-devel
らしい
- これが無いことで発生するエラーを見ると、必要なヘッダーが無いと
gem pg
を導入する際ビルドが失敗するらしい
yum search postgresql
して出てきた結果を見ると、これでよさそう
postgresql-devel.x86_64 : PostgreSQL development header files and libraries
導入方法
sudo yum install postgresql-devel
libxml2-dev
yumだとlibxml2-devel
が対応するのかな。URLは同じ場所を示している。
The XML C parser and toolkit of Gnome
http://xmlsoft.org/
導入方法
sudo yum install libxml2-devel
libxslt1-dev
yumだとlibxslt-devel
が対応するのかな。URLは同じ場所を示している。
なぜ1が無いのだろう…
導入方法
sudo yum install libxslt-devel
nodejs and yarn
anyenv -> nodenv 経由で入れる。
ミーハーなので現時点(2017/04/19)の最新版7.9.0
をインストールする。
yarn install
でなんか死んだ。
Production-Guideに従って、4.xの最新版である4.8.2
を入れた。
原因は別件だった: 下記トラブルシュート参照
とくに考えもなくndenv使ってたけど、コミットが静かなので浮気。(**envに活発性を求めてもしょうがないけど)
nodenvはnodenvでndenv OwnerにJoin持ちかけたり、anyenvから他のnode.js系env排斥を持ちかけたりするコミュニティなのでどうなのと思わなくもない。
導入方法
su - mastodon
git clone https://github.com/riywo/anyenv ~/.anyenv
echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
exec $SHELL -l
anyenv install nodenv
exec $SHELL -l
nodenv install 4.8.2
nodenv global 4.8.2
npm install -g yarn
nodenv rehash
file, git, curl
特にすり合わせるポイントはない。
導入方法
sudo yum install file git curl
Redis
Sidekiqの要求バージョンが>= 2.8.0
とのこと。
epelから入手したRedisでは2.4.x
のため、終盤になって問題が発覚して非常に悲しい気持ちになる。
SoftwareCollectionからRedis 3.2
を導入する。
導入方法
sudo yum install rh-redis32-redis
sudo mkdir /var/opt/rh/rh-redis32/lock
sudo mkdir /var/opt/rh/rh-redis32/lock/subsys
sudo service rh-redis32-redis start
sudo chkconfig rh-redis32-redis on
Ruby on Rails というか ActiveRecordsの要求バージョンが>= 9.1
とのことなので、SoftwareCollectionからPostgreSQL 9.5
を導入する。
導入方法
sudo yum install rh-postgresql95-postgresql-server
sudo yum install rh-postgresql95-postgresql-contrib
scl enable rh-postgresql95 bash
su -
echo 'source /opt/rh/rh-postgresql95/enable' >> /etc/profile.d/rh-postgresql95.sh
exit
sudo /opt/rh/rh-postgresql95/root/usr/bin/postgresql-setup --initdb
sudo service rh-postgresql95-postgresql start
sudo chkconfig rh-postgresql95-postgresql on
sudo passwd postgres
su - postgres
psql
CREATE USER mastodon CREATEDB;
\q
exit
rbenvを使ってrubyを導入する
su - mastodon
anyenv install rbenv
exec $SHELL -l
rbenv install 2.4.1
rbenv global 2.4.1
cd ~
git clone https://github.com/tootsuite/mastodon.git live
cd live
gem install bundler
bundle install --deployment --without development test
yarn install
.env.productionを設定していく。
pwd
cp .env.production.sample .env.production
redis
以下を.env.production
の項目で編集する。
REDIS_HOST=localhost
REDIS_PORT=6379
以下を.env.production
の項目で編集する。
DB_HOST=
DB_USER=mastodon
DB_NAME=mastodon
DB_PASS=
DB_PORT=5432
RAILS_ENV=production bundle exec rails db:setup
RAILS_ENV=production bundle exec rails assets:precompile
サービスの作成
CentOS6.9だし、Systemd
は使ってないので/etc/init.d/
に配置するほうのサービスとして書き出す。
手探りでstart-stop-deamon
無いどころかdeamon
使ってないし、というかstart
とstop
しかない。
mastodon-web
prog="mastodon-web"
username="mastodon"
root="/home/mastodon/live"
pidfile="${root}/web.pid"
start() {
if [ -f $pidfile ]; then
echo "Mastodon webservice running..."
exit 1
fi
su - $username -c "cd ${root} && RAILS_ENV=production PORT=3000 bundle exec puma -C config/puma.rb -d --pidfile ${pidfile}"
return 0
}
stop() {
if [ -f $pidfile ]; then
su - $username -c "kill $(cat ${pidfile}) && rm ${pidfile}"
else
echo "Mastodon webservice dos'nt running..."
exit 1
fi
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage $prog {start|stop}"
exit 2
esac
exit 0
mastodon-sidekiq
prog="mastodon-sidekiq"
username="mastodon"
root="/home/mastodon/live"
pidfile="${root}/worker.pid"
logfile="${root}/sidekiq.conf"
start() {
if [ -f $pidfile ]; then
echo "Mastodon worker sidekiq running..."
exit 1
fi
su - $username -c "cd ${root} && RAILS_ENV=production DB_POOL=5 bundle exec sidekiq -d -P ${pidfile} -L ${logfile} -c 5 -q default -q mailers -q pull -q push"
return 0
}
stop() {
if [ -f $pidfile ]; then
su - $username -c "kill $(cat ${pidfile}) && rm ${pidfile}"
else
echo "Mastodon worker sidekiq dos'nt running..."
exit 1
fi
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage $prog {start|stop}"
exit 2
esac
exit 0
mastodon-streaming
prog="mastodon-streaming-api"
username="mastodon"
root="/home/mastodon/live"
pidfile="${root}/streaming.pid"
start() {
if [ -f $pidfile ]; then
echo "Mastodon streaming API running..."
exit 1
fi
su - $username -c "cd ${root} && NODE_ENV=production PORT=4000 nohup npm run start & echo \$! > ${pidfile}"
return 0
}
stop() {
if [ -f $pidfile ]; then
local pid=$(cat ${pidfile})
local pgid=$(ps ho pgid -p $pid | sed 's/\s//g')
for target in $(ps h -g $pgid | sed 's/^\s\+//' | sed 's/\s\+/\t/g' | cut -f1)
do
kill $target
done
su - $username -c "${pidfile}"
else
echo "Mastodon streaming API dos'nt running..."
exit 1
fi
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage $prog {start|stop}"
exit 2
esac
exit 0
上記を/etc/init.d/
に配置したら、以下を実行する。
sudo service mastodon-web start
sudo chkconfig mastodon-web on
sudo service mastodon-sidekiq start
sudo chkconfig mastodon-sidekiq on
sudo service mastodon-streaming start
sudo chkconfig mastodon-streaming on
参考資料
CronJobの作成
RAILS_ENV=production
@daily cd /home/mastodon/live && /home/mastodon/.rbenv/shims/bundle exec rake mastodon:daily > /dev/null
トラブルシュート
yarn installでなんか死んでる
bufferutil
なるパッケージがビルドできずに死んでた。死ぬまで眼を凝らしていたらC++11コンパイラが見つかってなさそうだった。
gcc --version
g++ --version
Software Collectionから最新のDevToolset3を導入した。
sudo yum install devtoolset-3-gcc devtoolset-3-binutils
sudo yum install devtoolset-3-gcc-c++ devtoolset-3-gcc-gfortran
scl enable devtoolset-3 bash
パッケージを導入するたびにscl enable
を実行することで有効化できる。
これをシェル起動時毎回呼び出すと手軽らしい。
su -
echo 'source /opt/rh/devtoolset-3/enable' >> /etc/profile.d/devtools.sh
exit
binutils
とgfortran
がよくわからん。
参考資料
CentOSに新しいバージョンのGCC/G++(4.8/4.9)をインストールする - TASK NOTES
http://www.task-notes.com/entry/20151114/1447492231
Devtoolset-3 — Software Collections
https://www.softwarecollections.org/en/scls/rhscl/devtoolset-3/
SMTPサーバ用意するのしんどい
.env.production.sampleにも書いてあったけど、SparkPost使えば?とのこと。
SendGridを使用することにした。
さくらの VPS + CentOS7 で 俺専用 Mastodon インスタンスを立ててみた話 | WWW WATCH
https://hyper-text.org/archives/2017/04/mastodon-instance-single-user.shtml
これのせいでDB作成ができずに2日ほど作業が止まった。
mastodon userでpsql
を叩くとPostgreSQLは起動する。
psql -h localhost
とすると、ident認証が通らずうまくいかない。
local trust
host ::128 ident
host 127.0.0.1/32 ident
。。。別にlocalhostのために一旦ネットワーク出る必要なくない?
ということで.env.productionからDB_HOST
の値がなくなった。
S3にうまくアップロードできない
参考資料を元に進めたところ、S3へのアップロードで500エラーがかえってきたりした。
ポイントは多分以下だと思う。
S3_HOSTNAME=s3-ap-northeast-1.amazonaws.com
S3_ENDPOINT=https://s3-ap-northeast-1.amazonaws.com
情報源ごとに情報がブレるのは、Mastodonのバージョンによって挙動が違ったのかもしれない。
この情報は、Mastodon 1.2.2
で確認している。
参考資料
自分でアップロードしたのはS3に上がるが、リモートフォローした人のアイコンとかS3に上がらない
S3設定の更新をしたら、Sidekiqの再起動も必要だった。
リモートフォロー周りはSidekiq Workerが取り持ってくれてるが、S3設定後に再起動を忘れると、ローカルに保存する設定のままになっている。
所感