【Mastodon】さくらVPSやConohaばっかりなのでEX-CLOUDでMastodonしてみた

2017年6月11日

さくらのVPSやConohaでばっかりやられているので、Ex-CLOUD のVPSプランでMastodonを構築してみた。OSはCentOS7。

[amazonjs asin="B071RBSLH6″ locale="JP" title="Mastodonとは: 脅威の分散型SNS"]

前提

以下、ドメインをmastodon.ex-cloud.bizで説明する。

Mastodon

Rootに昇格

sudo -s

mastodonユーザーを作成

useradd mastodon

EPEL追加して必要なパッケージをインストール

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install ImageMagick libxml2 libxslt1 git curl ruby openssl-devel readline-devel zlib-devel pidentd gcc-c++ make protobuf-lite-devel

ffmpegをインストール

rpm --import https://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh https://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel

Node.jsをインストール

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum install nodejs --disablerepo=epel
npm install -g yarn

Redisをインストール

yum install redis
systemctl start redis
systemctl enable redis

PostgreSQLをインストール・設定

yum install  postgresql-server postgresql postgresql-contrib postgresql-devel -y
su - postgres
initdb
exit
systemctl start postgresql
su - postgres
psql
     CREATE USER mastodon CREATEDB;
     \q
exit

以下からしばらくmastodonユーザーでの作業

rbenv をインストール

sudo su - mastodon
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
cd ~/.rbenv/plugins/ruby-build

Ruby をインストール

rbenv install 2.4.1
rbenv rehash
rbenv global 2.4.1

Mastodon をインストール・設定


cd
git clone https://github.com/tootsuite/mastodon.git live
cd live
gem install bundler
bundle install --deployment --without development test
yarn install

cp .env.production.sample .env.production
RAILS_ENV=production rake secret
    abcdefg・・・・・・・・・・・・・・・・xyz
    # ↑を控えておく

vi .env.production
    # Service dependencies
    REDIS_HOST=localhost
    REDIS_PORT=6379
    # REDIS_DB=0
    DB_HOST=localhost
    DB_USER=mastodon
    DB_NAME=mastodon
    DB_PASS=
    DB_PORT=5432

    # Federation
    LOCAL_DOMAIN=mastodon.ex-cloud.biz
    LOCAL_HTTPS=true

    # Use this only if you need to run mastodon on a different domain than the one used for federation.
    # Do not use this unless you know exactly what you are doing.
    # WEB_DOMAIN=mastodon.example.com

    # Application secrets
    # Generate each with the `rake secret` task (`docker-compose run --rm web rake secret` if you use docker compose)
    PAPERCLIP_SECRET=
    SECRET_KEY_BASE="abcdefg・・・・・・・・・・・・・・・・xyz" #上で控えたキーを""で挟んで貼り付ける
RAILS_ENV=production bundle exec rails db:setup
RAILS_ENV=production bundle exec rails assets:precompile
exit

Mastodonの起動ファイルを作成・起動

以下からrootでの作業。

vi /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
vi /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
vi /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

起動。

systemctl enable /etc/systemd/system/mastodon-*.service 

Nginxをインストール、設定

systemctl stop httpd
systemctl disable httpd

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
iptables-save

systemctl restart iptables 
systemctl enable nginx

cd
git clone https://github.com/certbot/certbot
cd certbot/
./certbot-auto  certonly --standalone  -d mastodon.ex-cloud.biz
#メールアドレス入力したりなんだり

sudo vi /etc/nginx/conf.d/mastodon.conf
server{
    server_name    mastodon.ex-cloud.biz;
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/mastodon.ex-cloud.biz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mastodon.ex-cloud.biz/privkey.pem;

    location / {

        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 https://127.0.0.1:3000;
    }
}
sudo systemctl start nginx

ブラウザで Mastodon
https://mastodon.ex-cloud.biz にアクセスできれば成功。