Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active November 8, 2022 02:17
Show Gist options
  • Save mamemomonga/41e56f349279f6d6db25f43073f51c96 to your computer and use it in GitHub Desktop.
Save mamemomonga/41e56f349279f6d6db25f43073f51c96 to your computer and use it in GitHub Desktop.
mastodon.artritelacy.jp 構築メモ

artritelacy.jp 構築メモ

現在更新中です

参考URL

前提条件

  • mastodon v2.6.1 非Docker版
  • CloudFlare DNS, CDNを使用
  • SMTPサーバはmailgun
  • オブジェクトストレージはAmazon S3
  • ConoHa 3Core 2GB Ubuntu 18.04 x86_64
  • 作業用マシンはmacOS

このドキュメントの使い方

シェルコマンドは基本コピペで動きますが、以下の点に注意してください。

  • # から始まるのはrootでの作業
  • $ から始まるのは一般ユーザ(mastodonユーザ)での作業
  • 文頭に始まる#, $ はコピペしないでください。

ヒアドキュメント

以下の様になっている部分は cat から最後のEOSまでをコピペすることで一気に実行することができます。 いちばん先頭にある # や $ は取り除いてください。

# cat > filename.txt << 'EOS'
Hello World!
EOS

UNIXシェルの備考

  • ファイルパスがチルダ(~) から始まっているものは、ホームディレクトリ以下を表します。たとえば、mastodonユーザでログインしている場合での~/live は /home/mastodon/live と同じものです。
  • 末尾が / で終わっている行は、次の行も含めてコピペしてください。取り除く必要はありません。これは長い一行を折り返すのに使われます。

rootユーザとmastodonユーザ

Ubuntuは基本一般ユーザからrootユーザになって作業するのが通例ですが、ConoHaの場合 rootで直接ログインできます。そのためこのドキュメントではrootユーザでのログインを前提にして、必要に応じてmastodonユーザになるようにしています。

rootからmastodon ユーザになるには

$ su - mastodon

でなれます。逆に、sudo可能な他の一般ユーザ(ubuntuユーザなど)の場合

$ sudo su -

で、rootになることができ

$ sudo su - mastodon

で、mastodonユーザになることができます。

rootユーザによる直接ログインの禁止とrootになれるユーザの作成

ConoHaの場合、Ubuntuでも初期の設定では直接rootユーザでログインする形になっています。 このドキュメントもそれに準じて直接rootユーザでログインする形をとっています。 しかし、rootユーザでの直接ログインを禁止し、一般ユーザでログインしたあとsudoでrootになるほうがより安全です。 以下はrootユーザになれる一般ユーザの作成方法と、SSHによるrootログインの禁止の設定方法です。

  • mamemomonga は任意のユーザ名に変更してください
  • GID, UIDは必要に応じて変更してください

ConoHaへのログイン

[Mac Terminal]$ ssh root@[ConoHaのIPアドレス]

ユーザの作成

# groupadd -g 1100 mamemomonga
# useradd -g 1100 -u 1100 -s /bin/bash -m mamemomonga

sudoの設定

# echo 'mamemomonga ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/mamemomonga
# chmod 600 /etc/sudoers.d/mamemomonga

mamemomongaユーザになり、SSH公開鍵を設定する

# sudo su - mamemomonga
$ mkdir -m 0700 ~/.ssh
$ cat > ~/.ssh/authorized_keys

自分の公開鍵(id_ed25519.pubやid_rsa.pub)をペーストしてEnter, CTRL+C

次にきちんと書き込まれたかチェックする

$ cat ~/.ssh/authorized_keys
ssh-ed25519(略)

$ chmod 0600 ~/.ssh/authorized_keys

別ターミナルを開き、SSHで mamemomongaユーザでログインできることを確認する

[Mac Terminal]$ ssh mamemomonga@[ConoHaのIPアドレス]

ログインできたことが完了したら、rootになることができるか確認する。

$ sudo su -
# whoami
root

ConoHaから離脱し、ターミナルを閉じる。

# exit
$ exit

先ほどのターミナルに戻り、rootユーザでログインできないようにする。

現在 mamemomonga ユーザなので、一度exitして、rootユーザに戻る。

$ exit

# vim /etc/ssh/sshd_config

# ではじまっている行はコメントなので、無効になっている。

PermitRootLogin yes の行を PermitRootLogin no に変更する。

念のため、 PasswordAuthentication no となっているか確認しておく。

設定が終わったら sshd を再起動する。

# systemctl restart sshd
# systemctl status sshd

Active: active (running) となっていることを確認する。

再度別のターミナルを開き、再度 mamemomongaユーザでログインできることを確認する。 こちらが確認できるまで、元のターミナルは閉じないこと。(設定をミスっている場合再度SSHでのログインができなくなるため。)

[Mac Terminal]$ ssh mamemomonga@[ConoHaのIPアドレス]

ログインすることが出来れば問題なしなので、exitしてMacに戻る。

$ exit

rootユーザでSSHログイン出来なくなっているか確認する。

[Mac Terminal]$ ssh root@[ConoHaのIPアドレス]
root@[ConoHaのIPアドレス]: Permission denied (publickey).

以上で設定完了です。

マストドン構築時の注意点

ドメイン名に大文字を混ぜてはいけない

最初 https://mastodon.artritelacy.jp/ で構築していましたが、LOCAL_DOMAIN を mastodon.Artritelacy.jp と設定してしまったため、Chromeでストリーミングが取得できないという問題が発生しました。その問題に気づき LOCAL_DOMAIN を mastodon.artritelacy.jp にすると、今度は外部のインスタンスにトゥートが送れないという問題が発生。また、大文字を含んだ状態では一部クライアントではうまく動作しないという報告もありました。

この経験から、ドメイン名には大文字を使わない という大きな教訓を得ています。ドメイン名には大文字を使わないようにしましょう。

設定と構築

Mailgunの設定

[Domains]

[Add New Domain]

[SMTPを選択]

Domain Name: smtp.artritelacy.jp

[Add Domain]

Mailgun指定のDNSレコードをCloudFlareに登録

基本的には画面に表示されている通りです

CloudFlareの [artritelacy.jp] の DNSページで

[TXT] [Name: HostNameの値] [Enter This Value の値] [Automatic TTL] [Add Record]
[TXT] [Name: HostNameの値] [Enter This Value の値] [Automatic TTL] [Add Record]

[MX] [Name: smtp.artritelacy.jp] [Server: mxa.mailgun.org / Priority: 10] [Automatic TTL] [Add Record]
[MX] [Name: smtp.artritelacy.jp] [Server: mxb.mailgun.org / Priority: 10] [Automatic TTL] [Add Record]

[CNAME] [Name: email.smtp.artritelacy.jp] [mailgun.org] [Automatic TTL] [灰色雲] [Add Record]

こちらのMailGunの「Domain Verification & DNS」の内容を参照しながら設定します

CNAMEのところのオレンジ雲をクリックして灰色雲にするのは重要です。

[Domain Verification & DNS]を選択し、[Check DNS Records Now]を押します。オールグリーンになっていることを確認します。

設定が終わったら、[Get Started Sending]を押します

AWS S3の設定

AWSコンソールにログイン

右上のリージョンを「東京」に変更

S3バケットの作成とアップロードテスト

バケットの作成

[+バケットを作成する] -> [名前とリージョン]

  • バケット名: media.artritelacy.jp
  • [次へ]

[オプションの設定] ->[次へ] -> [アクセス許可の設定] -> [次へ] -> [確認] -> [バケット作成]

ウェブホスティングの有効化

media.artritelacy.jpの文字ではなくブルーの部分をクリック

[プロパティー] -> [Static website hosting]

  • このバケットを使用してウェブサイトをホストする
  • インデックスドキュメント: index.html
  • [保存]

アップロード

[概要] -> [アップロード] -> [ファイルを選択]

*「ファイルを追加」

  • 任意の画像を選ぶ
  • [次へ]

[アクセス許可を設定する] -> [次へ] -> [プロパティーを設定する] -> [次へ] -> [確認] -> [アップロード]

画像の公開

画像ファイル名をクリック

[概要] -> [公開する]

[リンク] を新しいウィンドウで開く。画像が表示されればOK

IAMの用意

マストドンとS3を接続するために、アクセスキーとアクセスシークレットを作成します。 特定バケットのみ操作できるように設定します。

サービスから[IAM]を選択

[ポリシーの作成] -> [JSON]

全部消して以下をペースト

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::media.artritelacy.jp",
                "arn:aws:s3:::media.artritelacy.jp/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "s3:ListAllMyBuckets",
            "Resource": "*"
        }
    ]
}

[Review Policy] -> [ポリシーの確認]

  • 名前: S3-media.artritelacy.jp
  • [Create policy]

左側の「ユーザ」をクリック

[ユーザを追加]

  • ユーザ名: S3-media.artritelacy.jp
  • プログラムによるアクセスにチェック
  • 次のステップ[アクセス許可の設定]

アクセス許可の設定

  • [既存ポリシーを直接アタッチ]
  • 検索のところに S3- といれると、末尾に先ほど作成したポリシーがでてくるのでチェックボックスにチェック
  • 次のステップ[確認]

確認

  • [ユーザの作成]

作成後

  • アクセスキーIDとシークレットアクセスキー(表示を押す)をコピペしてメモる

シークレットアクセスキーは「閉じる」を押すと二度と再表示できません。再度参照したいときは、アクセスキーIDとペアで作り直します。

Cyberduckなどで接続とアップロードの確認

新規接続 -> Amazon S3 -> アクセスキーとシークレットアクセスキーを設定して接続

media.artritelacy.jp にファイルのアップ、削除ができるか確認。

参考資料

https://dev.classmethod.jp/cloud/aws/s3-iam-restrict-resource/

SSH秘密鍵の読込

すでに id_rsa, id_rsa.pub としてキーペアをもっている場合、 macを起動した際に、以下のコマンドを都度1回だけ実行します。

$ ssh-add ~/.ssh/id_rsa

~/.ssh/id_rsa がロードされます

備考

  • macOSの場合、ssh-agentはデフォルトで動作しています。
  • 新規にキーを作成する場合は、ed25519 で作成するほうがいいらしいです。

新規SSHキーペアの作成方法

$ ssh-keygen -t ed25519

id_ed25519, id_ed25519.pub のキーペアが生成されます。

以下の例では id_rsa, id_rsa.pub で生成された例で進めたいと思います。

ConoHaでVPS作成

ConoHa VPS管理画面にログインしてSSH Keyの設定、サーバを起動します

SSH Key の登録

ここにキーを登録すると、このキーでrootアクセスすることができます

[セキュリティー] -> [SSH Key] -> [+パブリックキー]

  • リージョン: 東京
  • 登録方法: インポート
  • ネームタグ: 任意の名前
  • パブリックキー: ~/.ssh/id_rsa.pub の内容をペーストします

macでは以下の方法でターミナルからクリップボードにコピーできます

$ cat ~/.ssh/id_rsa.pub | pbcopy

サーバの起動

[+サーバ追加]

  • リージョン: 東京
  • サービス: VPS
  • 2GB 1,750円/月
  • イメージタイプ: Ubuntu 18.04 64bit
  • rootパスワード: 任意のパスワード
  • ネームタグ: artritelacy-jp

オプションを見る を押します

  • 追加ディスク: 使用しない
  • 接続許可ポートIPv4: 指定ポートのみ許可

以下の項目にだけチェックを入れて、その他はすべて外します。

SSH(22), Web(20/21/80/443)

  • 接続許可ポートIPv6: 全て拒否
  • SSH Key: [キーを選択] -> [パブリックキーを選択] -> [先ほど設定したキー]
  • スタートアップスクリプト: [使用しない]

[追加] を押します

ステータスが[起動中]になったら、ネームタグ列の artritelacy-jp をクリックします

[ネットワーク情報] にある [IPアドレス] をメモしてください。

備考

  • ConoHaでSSH Key設定を利用すると、sshd_configのPasswordAuthenticationがnoに設定され、パスワードのログインができなくなるようです。他のサービスの場合は /etc/ssh/sshd_config の設定を確認し、必要に応じて PasswordAuthentication no にしてセキュリティーを高めてください。

CloudFlare DNSの設定

artritelacy.jp で接続できるように、CloudFlareのDNSを設定します。

CloudFlare 管理ページにログインします。

[ artritelacy.jp ] -> [ DNS ]

[A] [Name: @] [IPv4 address: ConoHaのIPアドレス] [Automatic TTL] [灰色雲] [Add Record]

オレンジ雲をクリックすることで灰色雲になります。灰色になるとCloudFlareを通過しなくなります。

[Add Record] を押すのを忘れずに

これでAレコードの追加が完了し、artritelacy.jp でサーバに接続できます。

DNS CAA レコードの追加

CloudFlareの共有SSL証明書を使う場合のDNS CAAを設定します。

[CAA] [Name: @] [Tag: Only allow specific / Value: comodoca.com]    [Automatic TTL] [Add Record]
[CAA] [Name: @] [Tag: Only allow specific / Value: digicert.com]    [Automatic TTL] [Add Record]
[CAA] [Name: @] [Tag: Only allow specific / Value: globalsign.com]  [Automatic TTL] [Add Record]
[CAA] [Name: @] [Tag: Only allow wildcards / Value: comodoca.com]   [Automatic TTL] [Add Record]
[CAA] [Name: @] [Tag: Only allow wildcards / Value: digicert.com]   [Automatic TTL] [Add Record]
[CAA] [Name: @] [Tag: Only allow wildcards / Value: globalsign.com] [Automatic TTL] [Add Record]

https://support.cloudflare.com/hc/en-us/articles/115000310832-Certification-Authority-Authorization-CAA-FAQ

VPSにSSHで接続

ターミナルから

$ ssh root@artritelacy.jp

を実行してください。

Are you sure you want to continue connecting (yes/no)?

と聞かれたら、yesと入力。

Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-38-generic x86_64) とでれば成功です。

アップデートと再起動

以下の作業を行います

  • 初期のアップデートが裏で動作しているので、その終了を待ちます。
  • システムのアップデート
  • curl, vim, tmux のインストール
  • vimとtmuxの設定
  • マストドンに必要なソフトウェアの導入
  • マストドンユーザの作成

スクリプトはこちら(最初の#は除外してください)

# bash -eu << 'EOS'

export DEBIAN_FRONTEND=noninteractive

echo -n "wainting apt"
while true; do
  if [ -z "$(ps ax | grep -v grep | grep apt)" ]; then
    break
  fi
  echo -n "."
  sleep 1
done
echo

set -x
apt-get update
apt-get -y upgrade
apt-get install -y curl vim tmux

echo '3' | update-alternatives --config editor

cat > /etc/vim/vimrc.local << 'EOD'
syntax on
set wildmenu
set history=100
set number
set scrolloff=5
set autowrite
set tabstop=4
set shiftwidth=4
set softtabstop=0
set termencoding=utf-8
set encoding=utf-8
set fileencodings=iso-2022-jp,utf-8,cp932,euc-jp,ucs2le,ucs-2
set fenc=utf-8
set enc=utf-8
EOD

cat > /root/.tmux.conf << 'EOD'
set -g default-terminal "screen-256color"
set -g terminal-overrides 'xterm:colors=256'
set -g status-fg white
set -g status-bg "#000066"
set-window-option -g window-status-format "#I:#W"
set-window-option -g window-status-current-format "#[fg=colour255,bg=colour27,bold] #I:#W #[default]"
setw -g window-status-fg cyan
setw -g window-status-bg default
setw -g window-status-attr dim
setw -g window-status-current-fg   white
setw -g window-status-current-bg   "#0000FF"
setw -g window-status-current-attr bright
set -g pane-border-fg green
set -g pane-border-bg black
set -g pane-active-border-fg white
set -g pane-active-border-bg yellow
set -g message-fg  white
set -g message-bg  black
set -g message-attr bright
set -g status-left-length 40
set -g status-left "#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P "

set -g status-interval 30
set -g status-justify left
setw -g monitor-activity on
set -g visual-activity on
set -g status-position top
set-option -g allow-rename off

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

setw -g mode-keys vi
EOD

curl -sL https://deb.nodesource.com/setup_8.x | bash -
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

apt install -y \
  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 libjemalloc-dev

groupadd -g 2000 mastodon
useradd -g 2000 -u 2000 -s /bin/bash -m mastodon

EOS

再起動を行います

# reboot

しばらくまって、再度SSHで接続します。

tmuxの起動

共同作業者とターミナル共有する場合はtmuxを利用するのが便利です。

互いに root でログインしているとして

開始

# tmux -S /tmp/tmux-shared

参加

# tmux -S /tmp/tmux-shared attach

です。tmuxは他にも便利な使い方があるので検索して調べてみてください。

iptablesの設定

ConoHaのファイアーウォールでも制限していますが、念のためiptablesでも制限します。 なお、Docker版を利用している場合は、このままではブロックできないのでご注意を。

# DEBIAN_FRONTEND=noninteractive apt install -y iptables-persistent

# cat > /etc/iptables/rules.v4 << 'EOS'
*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT
EOS

# iptables-restore < /etc/iptables/rules.v4

SMTP送信テスト

# cd
# curl http://www.jetmore.org/john/code/swaks/files/swaks-20130209.0/swaks -o swaks
# chmod +x swaks

以下、一部書き換えてください。

# ./swaks --auth \
 --server smtp.mailgun.org \
 --au postmaster@smtp.artritelacy.jp \
 --ap [この値を使用: Mailgun -> Domain Information -> Default Password] \
 --to [Mailgunを申し込んだメールアドレス] \
 --h-Subject: "Hello" \
 --body 'Testing some Mailgun awesomness!'

メールが届けばテスト完了です。

nginxセットアップ

CloudFlare経由でのSSL有効化も一緒に行います。

CloudFlareが提供するオレオレ証明書( Origin Certificates )をnginxにいれて、その証明書でCloudFlareとConoHa間の通信をhttpsで行います。 これでサイトはCloudFlareで保護されます。ただし、接続元のIPアドレスがサーバ側からはわからないという弱点もあります。

まず、 http://artritelacy.jp/ が見えるか確認します。

次に、CloudFlareの管理画面のログインし artritelacy.jp へ移動します。

CloudFlareの Crypto ページの Origin Certificates で Create Certificate を押します

各項目デフォルトのまま Next を押します。

Origin Certificate Installation に各キーが表示されます。

# mkdir -p /etc/nginx/cloudflare
# cat > /etc/nginx/cloudflare/artritelacy.jp.pem

Origin Certificate をコピーしてペーストしてEnterをおしてCTRL+C

# cat > /etc/nginx/cloudflare/artritelacy.jp.private.pem

Private key をコピーしてペーストしてEnterをおしてCTRL+C

内容を確認します。

# cat /etc/nginx/cloudflare/artritelacy.jp.pem
# cat /etc/nginx/cloudflare/artritelacy.jp.private.pem

特に漏れやすい最初と最後がきちんとペースト出来ているか確認してください。

問題なければ、CloudFlareのダイアログをOKを押して閉じます。 OKを押してウィンドウを閉じると、「Private Key」は再度参照することができなくなります。

artritelacy.jp のnginx設定を投入(Welcomeページのみ)

# cat > /etc/nginx/sites-available/artritelacy.jp.conf << 'EOS'
server {
listen 443 ssl http2;
  root /var/www/html;
  server_name artritelacy.jp;
  index index.html index.htm index.nginx-debian.html;
  location / {
    ry_files $uri $uri/ =404;
  }
  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/nginx/cloudflare/artritelacy.jp.pem;
  ssl_certificate_key /etc/nginx/cloudflare/artritelacy.jp.private.pem;

  keepalive_timeout    30;
  sendfile             on;
  client_max_body_size 12m;

  add_header Strict-Transport-Security "max-age=31536000";
}
EOS

有効化

# ln -s /etc/nginx/sites-available/artritelacy.jp.conf /etc/nginx/sites-enabled/artritelacy.jp.conf

nginx再起動

# systemctl restart nginx

CloudFlareのDNSページを開き、 A artritelacy.jp の項目にある Status列の灰色の雲をクリックしてオレンジの雲にします。

5分ほどまって、 https://artritelacy.jp/ がエラーなく見えれば成功。

PostgreSQLのユーザ作成

# cd /tmp
# echo "CREATE USER mastodon CREATEDB;" | sudo -u postgres psql
# cd

マストドンの準備

マストドンユーザになる

# sudo su - mastodon

初回取得

  • マストドンアプリケーションの初回取得
  • rbenv, ruby-buildの導入

スクリプト

$ cd

$ bash -eux << 'EOS'
git clone https://github.com/tootsuite/mastodon.git live && cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
git status
cd
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
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
EOS

rbenv を有効にします。

$ exec bash

rubyのビルドとbundlerの導入

$ cd

$ bash -eux << 'EOS'
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install $(cat ~/live/.ruby-version)
rbenv global $(cat ~/live/.ruby-version)
gem install bundler --no-ri --no-rdoc
EOS

マストドンに必要なRuby, node.jsの依存関係を導入

$ cd ~/live

$ bash -eux << 'EOS'
bundle install \
  -j$(getconf _NPROCESSORS_ONLN) \
  --deployment --without development test
yarn install --pure-lockfile
EOS

mastodon セットアップ

$ cd ~/live

$ RAILS_ENV=production bundle exec rake mastodon:setup

各種設定の例です。

これは一気にコピペせず、内容を確認しながら設定してください。

設定中断は CTRL+C です。

Your instance is identified by its domain name. Changing it afterward will break things.
Domain name: artritelacy.jp

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No

Are you using Docker to run Mastodon? no

PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon_production
Name of PostgreSQL user: mastodon
Password of PostgreSQL user:
Database configuration works! 🎆

Redis host: localhost
Redis port: 6379
Redis password:
Redis configuration works! 🎆

Do you want to store uploaded files on the cloud? yes
Provider Amazon S3
S3 bucket name: media.artritelacy.jp
S3 region: ap-northeast-1
S3 hostname: s3-ap-northeast-1.amazonaws.com
S3 access key: [AWS S3 AMIで取得したアクセスキー]
S3 secret key: [AWS S3 AMIで取得したシークレットキー]
Do you want to access the uploaded files from your own domain? no

Do you want to send e-mails from localhost? No
SMTP server: smtp.mailgun.org
SMTP port: 587
SMTP username: postmaster@smtp.artritelacy.jp
SMTP password: [smtp.artritelacy.jp の Domain Information の Default Password]
SMTP authentication: plain
SMTP OpenSSL verify mode: none
E-mail address to send e-mails "from": Mastodon <notifications@artritelacy.jp>
Send a test e-mail with this configuration right now? Yes
Send test e-mail to: [自分のメールアドレス]

This configuration will be written to .env.production
Save configuration? Yes

Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? (Y/n)

中略

Compile the assets now? (Y/n)

中略(時間かかります)

All done! You can now power on the Mastodon server 🐘

Username: [adminになるID]
E-mail:  [adminになりたいユーザのメールアドレス]
You can login with the password: [初回ログイン時のパスワード]
You can change your password once you login.

mastodon ユーザを離脱、rootになる

$ exit

nginxの設定

自動生成されたnginxの設定サンプルは dist/nginx.conf にあります

# cat > /etc/nginx/sites-available/artritelacy.jp.conf << 'EOS'
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=1g;

server {
  listen 80;
  listen [::]:80;
  server_name artritelacy.jp;
  root /home/mastodon/live/public;
  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 artritelacy.jp;

  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/nginx/cloudflare/artritelacy.jp.pem;
  ssl_certificate_key /etc/nginx/cloudflare/artritelacy.jp.private.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";
    add_header Strict-Transport-Security "max-age=31536000";
    try_files $uri @proxy;
  }

  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    add_header Strict-Transport-Security "max-age=31536000";
    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 on;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    proxy_cache CACHE;
    proxy_cache_valid 200 7d;
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
    add_header X-Cached $upstream_cache_status;
    add_header Strict-Transport-Security "max-age=31536000";

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

nginxの再起動

# systemctl restart nginx

スタートアップスクリプトの設定とマストドンの起動

# cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
# systemctl start mastodon-web mastodon-sidekiq mastodon-streaming
# systemctl enable mastodon-web mastodon-sidekiq mastodon-streaming

これでマストドンの起動作業は完了です。

以下は参考資料です。

ログの参照方法

トラブルシュートのときはログを確認します。

nginx

アクセスログ

tail -n 300 /var/log/nginx/access.log | less

エラーログ

tail -n 300 /var/log/nginx/error.log  | less

その他のログ

# journalctl -u nginx

mastodon-web, mastodon-sidekiq, mastodon-streaming

参照

# journalctl -u mastodon-web

末尾100行だけ表示

# journalctl -n 100 -u mastodon-web

監視

# journalctl -f -u mastodon-web
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment