Bash on Ubuntu on WindowsでMastodonを動かす方法
Bash on Ubuntu on WindowsでMastodonを動かします。
Mastodonを動かすついでにRedisとPostgreSQLも動かすので、興味のある方は是非読んでみて下さい。
セットアップ
セットアップの手順については、公式のドキュメント(Production guide)1を参考にします。
必要なパッケージのインストール
$ sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git curl
Node.jsのインストール
$ wget https://deb.nodesource.com/setup_7.x
$ sudo bash setup_7.x
$ sudo apt-get install nodejs
$ sudo npm install -g yarn
Redisのインストール
$ sudo apt-get install redis-server redis-tools
PostgreSQLのインストールとDBの設定
パッケージのインストール
$ sudo apt-get install postgresql postgresql-contrib
設定のためにサービスを起動
$ sudo service postgresql start
データベースの設定
$ sudo su - postgres $ psql
$ CREATE USER mastodon CREATEDB; $ \q
(このタイミングで一度、適当なSQLクライアントで接続確認をしておくと安心です)
サービスの停止
$ sudo service postgresql stop
Rbenv
mastodon
ユーザになって、rbenvと ruby 2.4.1
をインストールします。
ユーザーの作成
$ useradd -m mastodon $ sudo su - mastodon $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
mastodonユーザでrbenvのインストール
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ source ~/.bash_profile
$ rbenv install 2.4.1
mastodonユーザでmastodonリポジトリの取得
$ git clone https://github.com/tootsuite/mastodon.git live $ cd live $ git checkout $(git tag | tail -n 1)
依存パッケージのインストール
$ gem install bundler $ bundle install --deployment --without development test $ yarn install --pure-lockfile
設定ファイルの編集
設定用シークレットキーの生成
$ rake secret $ rake secret $ rake secret
設定ファイルの書き換え
$ cp .env.production.sample .env.production $ vim .env.production
書き換えた項目は以下の分だけです。 (今回は手元で動かすだけなので、メールの設定などは省略)
REDIS_HOST=localhost DB_HOST=localhost LOCAL_DOMAIN=localhost LOCAL_HTTPS=false PAPERCLIP_SECRET=<rake secretで生成したシークレットキーの値> SECRET_KEY_BASE=<rake secretで生成したシークレットキーの値> OTP_SECRET=<rake secretで生成したシークレットキーの値>
データベースの初期化
$ RAILS_ENV=production bundle exec rails db:setup
アセットのプリコンパイル
$ RAILS_ENV=production bundle exec rails assets:precompile
サービス起動スクリプトの作成
公式ドキュメントではmastodonの各種サービスは systemd
を使って動かすようになっています。しかしながら、BoWでは systemd
が使用できません。
(おそらく、systemdで利用しているudevが、devtmpfs未実装のために動かないため?)
systemd
が使えないので、代わりに普通の起動スクリプトを作成します。
mastodon-web.sh
#!/bin/bash user="mastodon" working_directory="/home/mastodon/live" environment="RAILS_ENV=production PORT=3000" exec_start="/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb" sudo -i -u $user sh -c "cd $working_directory && $environment $exec_start"
mastodon-sidekiq.sh
#!/bin/bash user=mastodon working_directory=/home/mastodon/live environment="RAILS_ENV=production DB_POOL=5" exec_start="/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push" sudo -i -u $user sh -c "cd $working_directory && $environment $exec_start"
mastodon-streaming.sh
#!/bin/bash user=mastodon working_directory="/home/mastodon/live" environment="NODE_ENV=production PORT=4000" exec_start="/usr/bin/npm run start" sudo -i -u $user sh -c "cd $working_directory && $environment $exec_start"
アカウントを承認するコマンドの準備
今回はメールの設定をしないため、アカウントを承認するためにコマンドを実行する必要があります。ユーザーを切り替えたりすると面倒なので、起動スクリプトと同様、シェルスクリプトにまとめておきます。
confirm.sh
#!/bin/bash user="mastodon" working_directory="/home/mastodon/live" environment="RAILS_ENV=production" exec_start="/home/mastodon/.rbenv/shims/bundle exec rails mastodon:confirm_email USER_EMAIL=$1" sudo -i -u $user sh -c "cd $working_directory && $environment $exec_start"
mastodonの起動
準備ができたので、サービスを順番に立ち上げていきます。
Redis
$ sudo service redis-server start
PostgreSQL
$ sudo service postgresql start
Mastodon (ターミナルを3枚開き、それぞれで起動すると挙動が見えて面白いです)
$ sudo ./mastodon-web.sh & $ sudo ./mastodon-streaming.sh & $ sudo ./mastodon-sidekiq.sh &
注意: ターミナルを全て閉じるとサービスが停止してしまうため、最低1枚はBoWの画面を開いておいて下さい
表示確認
http://localhost:3000
へアクセスすると、おなじみのmastodonのサインアップ画面が表示されます。
アカウント登録の後、アカウントを承認するコマンドで承認します。(インスタンスは立ち上げたままで大丈夫)
$ sudo ./confirm.sh <登録したメールアドレス>
これでログインできるようになりました。
ログイン後、サムネイル画像を設定したりトゥート!してみた後の画像はこちらです。
以上、こんな感じでした。