LoginSignup
5
4

More than 5 years have passed since last update.

Ubuntu18.04でMastodonの構築がしたい(超手抜きβ版)

Last updated at Posted at 2018-11-09

Mastodon Production GuideInstallation
を元に独自解釈てんこ盛りで記述しております。
本記事はDockerを用いずにスタンドアロンでMastodonを構築する手順になります。

前提条件
・root権限を持つUbuntu18.04環境
・Mastodonサーバーのドメイン名
・電子メール配信サービスまたは他のSMTPサーバーの用意
↑(MailGunとSparkPostの設定は要望があれば追記します…)
・ミスってもなるべく挫けない、煮詰まったら休憩

1.VSP等でUbuntu18.04環境を用意

とりあえず最新に
apt -y update
apt -y upgrade

2.日本語環境の設定

英語環境で良いなら省略
英語環境でセットアップしてしまったUbuntuで日本語を使えるようにする
WSLのUbuntu環境を日本語化する

# 1.パッケージ情報の更新
sudo apt update
sudo apt upgrade

# 2.日本語言語パックのインストール
sudo apt -y install language-pack-ja
もしくは
apt install $(check-language-support -l ja)

# 3.ロケールを日本語に設定
sudo update-locale LANG=ja_JP.UTF8

# 4.ここでいったん終了してから、Ubuntuを再起動

# 5.タイムゾーンをJSTに設定
sudo dpkg-reconfigure tzdata
アジア→東京を選択

# 6.日本語マニュアルのインストール
sudo apt -y install manpages-ja manpages-ja-dev

3.スワップファイル作成

Ubuntu/CentOSサーバのスワップ領域の割り当て
RAMが少ない(~1GB程度)場合、後でprecompile出来ないためスワップを作成

# 1.管理者権限になる
sudo su -

# 2.スワップ用フォルダの作成
mkdir /var/swap

# 3.2Gのスワップ用のファイル作成
dd if=/dev/zero of=/var/swap/swap0 bs=1M count=2048

# 4.パーミッションの設定
chmod 600 /var/swap/swap0

# 5.スワップ割り当て
mkswap /var/swap/swap0
swapon /var/swap/swap0

# 6.起動時に自動でスワップが割り当てられるように設定
echo '/var/swap/swap0 swap swap defaults 0 0' >> /etc/fstab

4.Mastodonのインストール

# 1.念の為
add-apt-repository multiverse
add-apt-repository restricted
apt update

# 2.node.jsのリポジトリ追加
apt -y install curl
curl -sL https://deb.nodesource.com/setup_8.x | bash -

# 3.Yarnのリポジトリ追加
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt update

# 4.その他依存ソフトウェア追加
apt -y install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev nginx redis-server redis-tools postgresql postgresql-contrib certbot yarn libidn11-dev libicu-dev
# 非rootユーザーでインストールする依存ソフトウェアの追加
adduser --disabled-login mastodon
gpasswd -a mastodon sudo
sudo su - mastodon
# 1.rbenvとruby-buildをセットアップ
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

# 2.shellを再起動
exec bash

# 3.rbenvがインストールされているか確認する
type rbenv

# 4.ruby-buildをrbenvのプラグインとしてインストールする
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
基本的には最新版
rbenv install 2.5.3
※めっちゃ時間かかる
rbenv global 2.5.3
# 1.mastodonユーザーのホームディレクトリに戻る
cd ~

# 2.mastodonのGitリポジトリを~/liveにクローン
git clone https://github.com/tootsuite/mastodon.git live

# 3.~/liveに移動
cd ~/live

# 4.最新の安定版にcheckout
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)

# 5.bundlerをインストール
gem install bundler

# 6.bundlerを利用してRubyの依存ソフトウェアをインストール
bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test

# 7.yarnを利用してnode.jsの依存ソフトウェアをインストール
yarn install --pure-lockfile

# 8.rootに戻る
exit
# 1.PostgreSQLターミナルに接続
sudo -u postgres psql

# 2.DBの作成
CREATE USER mastodon CREATEDB;

# 3.ターミナルから抜ける
\q

nginxの設定

cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon

/etc/nginx/sites-available/mastodonを編集してexample.comを自分のドメイン名に置き換え、必要に応じて他の調整を行う

nginx.confの内容例

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name cironnup.com;
  root /home/mastodon/live/public;
  # Useful for Let's Encrypt
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name cironnup.com;

  ssl_protocols TLSv1.2;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/letsencrypt/live/cironnup.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/cironnup.com/privkey.pem;

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 80m;

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

  location / {
    try_files $uri @proxy;
  }

  location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
    add_header Cache-Control "public, max-age=31536000, immutable";
    try_files $uri @proxy;
  }

  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    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://127.0.0.1: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;
}
# 設定を有効にするためにnginxをリロード
systemctl reload nginx

SSL証明書の作成&設定

certbot certonly --webroot -d cironnup.com -w /home/mastodon/live/public/
# Let's Encrypt証明書の更新の自動化
vi /etc/cron.daily/letsencrypt-renew

↓以下をコピペして保存

#!/usr/bin/env bash
certbot renew
systemctl reload nginx

# 設定を有効にするためにnginxをリロード
systemctl reload nginx
# スクリプトを実行可能にして、cronデーモンを再起動
chmod +x /etc/cron.daily/letsencrypt-renew
systemctl restart cron

5.Mastodonのセットアップ

sudo su - mastodon
cd ~/live
RAILS_ENV=production bundle exec rake mastodon:setup

※セットアップ内容は編集中…

この時点で、ブラウザでドメインにアクセスして、象がコンピュータ画面のエラーページを表示するのを確認できる。ただし、まだマストドンのプロセスは開始していない。

6.Mastodonのサービス設定

cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/

↑だけで行けるっぽい?
ファイルを編集して、内容が正しいことを確認
・/etc/systemd/system/mastodon-web.service
・/etc/systemd/system/mastodon-sidekiq.service
・/etc/systemd/system/mastodon-streaming.service

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
ExecReload=/bin/kill -SIGUSR1 $MAINPID
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 push -q mailers -q pull
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 start mastodon-web mastodon-sidekiq mastodon-streaming
systemctl enable mastodon-*

7.さて動いたかしら?

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4