LoginSignup
17
18

More than 3 years have passed since last update.

Mastodon構築手順・非Docker版・Fedora

Last updated at Posted at 2017-09-21

前提条件

  • DNSが設定済みであること
  • サーバがインターネットに接続できること
  • 自分以外の第三者にも解放する場合、電気事業者の届け出が済んでいること
  • Fedora Server 32 x64、Mastodon v3.2.0 にて動作確認

はじめに

必ず公式ガイドを読んでください。

Mastodon documentation - Installing from source

これは上記ドキュメントをFedora(他のRHEL系にも使える?)に読み替えたもの+αです。

★移行作業でこの手順を使用する場合のTipsが記載されています。新規インストールを行っている場合は無視してください。

サーバの初期設定

sudo timedatectl set-timezone Asia/Tokyo
sudo yum -y update
sudo vim /etc/selinux/config
sudo shutdown -r now
  1. タイムゾーンをJSTに
  2. サーバの最新化
  3. SELinuxを無効にする
  4. このタイミングで再起動しておく

Mastodon専用ユーザの追加

sudo useradd mastodon

公式ガイドの通り、 /home/mastodon/live 以下にMastodonをインストールします

PostgreSQLのインストールと初期設定

PostgreSQL は v9.6以降、もしくは v10.x以降をインストールする。

installing_and_initializing_postgresql
sudo yum install -y postgresql-server
sudo postgresql-setup --initdb

PostgreSQLをインストールし、初期化する

tuning_postgresql
$ sudo vim /var/lib/pgsql/data/postgresql.conf

pgTune( http://pgtune.leopard.in.ua/ )を参考に、サーバのスペックに合ったパラメータを設定する。
バージョンよって設定値に多少差があるので、導入したバージョンを確認すること。

setting_security
$ sudo vim /var/lib/pgsql/data/pg_hba.conf

必要に応じて、アクセス権限の設定を行う。

$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql

サービスの自動起動設定と起動

$ sudo su - postgres
$ createuser -d mastodon
$ exit

postgres(PostgreSQL管理者ユーザ)で、Mastodon用ユーザーをDB作成権限付きで作成する

リバースプロキシ(Nginx)のインストールと設定

ファイアウォールの開放

HTTP(80)およびHTTPS(443)を開放する。
以下は firewalld がインストールされている環境での手順
ゾーン名等は適宜読み替えてください

現在のファイアウォールの状態を表示

$ sudo firewall-cmd --list-all

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: ssh mdns dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

上記例では、public ゾーンがアクティブになっており、ssh mdns dhcpv6-client が許可されている。

HTTPとHTTPSを許可する

$ sudo firewall-cmd --add-service=http --zone=public --permanent
success

$ sudo firewall-cmd --add-service=https --zone=public --permanent
success

$ sudo firewall-cmd --reload
success


$ sudo firewall-cmd --list-all

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens3
  sources: 
  services: ssh mdns dhcpv6-client http https
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

public ゾーンにて、http https が追加で許可された。

Let's EncryptによるSSL証明書の発行と登録

先にやっておかないとnginxを起動できません

sudo yum install -y certbot
sudo certbot certonly --standalone -d yourdomain.tld

実行中にいろいろ聞かれます

メールアドレスを入力してください(更新に係る緊急連絡、セキュリティ通知に使われます)
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
サービス利用規約を読んで同意してください。
(A)gree/(C)ancel: 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
あなたのメールアドレスをEEF(電子フロンティア財団; Let's Encryptのパートナーの非営利団体)と共有しますか?EEFニュース、キャンペーン、Digital freedom を支援するための方法をお送りします。
(Y)es/(N)o: 

(オプション)Nginxのリポジトリを手動で追加する必要がある場合

sudo vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Nginxのインストールと動作確認

sudo yum install -y nginx
sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.old

Nginxをインストールし、セキュリティのためデフォルトの設定ファイルを無効化する
(デフォルトの設定ファイルは無い場合もあるし、違う名前のこともある)

sudo vim /etc/nginx/conf.d/mastodon.conf

公式リポジトリにあるNginx用サンプル設定ファイルを書き込む
https://github.com/tootsuite/mastodon/blob/master/dist/nginx.conf

vim等で example.com を 使用するドメインに置換

:%s/example.com/yourdomain.tld/g

nginxの実行ユーザを「mastodon」(マストドン実行ユーザ)に変更する。

sudo vim /etc/nginx/nginx.conf
user mastodon;

Nginxのテンポラリディレクトリの所有者を mastodon ユーザにする。

$ sudo chown -R mastodon:mastodon /var/lib/nginx/

Nginxの起動と自動起動設定

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

この状態で、ブラウザから http://yourdomain.tld にアクセスしてみて、Nginxのスタートページが表示されればOK
https://yourdomain.tld に転送された上で 502 Bad Gateway 表示になれば、ここまで問題なく構築できている。

依存性パッケージのインストール

Git, OpenSSL, ffmpeg, node.js, yarn, redis, ImageMagick, 等々…
develパッケージを入れておかないと、後々(bundle installする時とか)怒られる。

RPM Fusion リポジトリの導入

公式サイトのインストールコマンドを使用する。
「free」 を使用する。

Node.js リポジトリの導入

(デフォルトで新しめのNode.jsがインストールできる環境であれば不要かもしれない)

NodeSource Node.js Binary Distributions - Enterprise Linux based distributions

v3.1.0 より、Node.js v10が必須となった。

curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -

yarn リポジトリの導入

sudo curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo

各種パッケージのインストール

$ sudo yum install -y redis nodejs yarn ffmpeg ImageMagick bzip2 tar make gcc-c++ git protobuf-compiler {openssl,readline,zlib,postgresql,protobuf,libicu,libidn}-devel 

Redisのスタートアップ起動設定と手動起動

$ sudo systemctl start redis
$ sudo systemctl enable redis

Mastodon本体の取得とインストール

rbenv(Ruby環境自動構築ツール)のインストールとRuby環境の構築

先に、必要なRubyのバージョンをチェックしておく
tootsuite/mastodon README.md Deployment

下記例は v2.6.5をインストールするものだが、適宜読み替えること。

$ sudo su - mastodon

$ pwd
/home/mastodon

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ cd ~/.rbenv && src/configure && make -C src && cd ~
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile && source ~/.bash_profile
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

$ rbenv install 2.6.5 && rbenv global $_ && rbenv rehash

割と時間かかる。

終わったら動作確認

$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]

補足:rbenv install時に/tmpの容量が足りなくてインストールできないときは

環境変数 TMPDIR に適当なディレクトリを設定してやることで、そこをテンポラリディレクトリとして使用するようになる

TMPDIR="/home/mastodon/rbenv_tmp" rbenv install x.x.x
rbenv global x.x.x && rbenv rehash

Mastodon本体の取得と依存Gemパッケージのインストール

$ cd ~ && git clone https://github.com/tootsuite/mastodon.git live && cd live

インストールできるバージョン一覧を取得する

git tag
・
・
・
v2.4.0rc5
v2.4.1
v2.4.1rc1
v2.4.1rc2
v2.4.1rc3
v2.4.1rc4
v2.4.2
v2.4.2rc1
v2.4.2rc2
v2.4.2rc3
v2.4.3
v2.4.3rc1
v2.4.3rc2
v2.4.3rc3
v2.4.4
v2.4.5
v2.5.0
v2.5.0rc1
v2.5.0rc2

rc (Release Candidate: リリース候補版) は、メジャーアップデート前のテスト版。
rcのついていない最新バージョン(上記例では v2.5.0) がオススメ。

git checkout vX.X.X ← 選んだバージョンを指定する
gem install bundler
bundle install --deployment --without development test
yarn install

補足: checkout したら You are in 'detached HEAD' state. って出たけどいいの?

Note: switching to 'v3.1.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

のようなメッセージが出る。
これは「あなたのHEAD(現在の状態)はmaster(最新の状態)から外れてるよ」というお知らせでありエラーではない。

Mastodonの設定

設定ウィザードの起動

v2.4.x から? インタラクティブに設定できるRakeタスクが追加された。

(移行作業Tips: 旧サーバの.env.productionファイルを持っているならば必要ない。シンプルに~/liveにコピーすればよい)

cd ~/live
RAILS_ENV=production bundle exec rake mastodon:setup
Domain Name: (Mastodonが動いているサーバのドメイン名)

Do you want to enable single user mode? (お一人様モードにするかどうか)

Are you using Docker to run Mastodon? (Dockerを使ってMastodonを動かしているかどうか。ここはNo。)

PostgreSQL host: (PostgreSQLが動作しているホスト。ここはlocalhost)
PostgreSQL port: (PostgreSQLが動作しているポート。変更してなければ 5432)
Name of PostgreSQL database: (データベース名。任意。mastodon とか。)
Name of PostgreSQL user: (PostgreSQLのユーザ名。 mastodon。)
Password of PostgreSQL user: (PostgreSQLのユーザのパスワード。設定してなければ空白)

Redis host: (Redisのホスト。ここはlocalhost)
Redis port: (Redisのポート。変更してなければ 6379)
Redis password: (Redisのパスワード。設定してなければ空白)

Do you wait to store uploaded files on the cloud? (画像や動画をクラウドのオブジェクトストレージ等に保存しますか? 任意。)

Do you want to send e-mails from localhost? (ローカルホストからメールを送りますか?任意。Nを選ぶとメールサーバの設定が出る)

Save configuration? (設定を保存しますか?)


Prepare Database now? (データベースの初期化をしますか?)

Compile the assets now? (アセットのコンパイルをしますか?)

Mastodonのサービス化

サービス設定ファイルの作成

Mastodonのdistディレクトリに推奨値入りのファイルが存在するので、それをそのまま使用する。

sudo cp /home/mastodon/live/dist/mastodon-{web,sidekiq,streaming}.service /etc/systemd/system/
sudo systemctl daemon-reload

各サービスの起動と自動起動設定

sudo systemctl enable mastodon-{web,sidekiq,streaming}
sudo systemctl start mastodon-{web,sidekiq,streaming}

(移行作業Tips: 起動確認が終わったら、上記3サービスをstopさせてください。移行作業で使う手順はここまでです。)

Mastodonの動作確認と初期設定

ブラウザから、http://yourdomain.tld へアクセスしてみてください。
httpsへ転送され、about画面がきれいに表示されれば、インストールは成功しています。おめでとう!!

ユーザの追加と管理者設定

ユーザー登録フォームから、自身のアカウントを登録する。
(いつからか「admin」というIDは予約語として拒否されるようになった)

前述のメールサービスが正常に設定されていれば、入力したメールアドレス宛に登録確認メールが飛んでいく。
メール内の「メールアドレスを確認」リンクを辿れば、メール認証が完了する。
登録したメールアドレス・パスワードでログインを行う。

メールがいつまでたっても届かない、入力するメールアドレスを間違えた、そもそもメールサービスを使わない、等
メール周りに不具合が発生している場合は、コマンドラインよりユーザ作成を行うこともできる。

参考 → 勝手 Mastodon tootctl リファレンス - tootctl account create

古い情報(Rakeタスクを使用するもの)
sudo su - mastodon
cd live/
RAILS_ENV=production bundle exec rails mastodon:confirm_email USER_EMAIL=yourname@yourmail.tld

特定のユーザを管理者にする

sudo su - mastodon
cd live/
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=yourname

他の管理タスクについては、他ページを参照のこと。

あとがき

  • 自分用にメモした手順です。必要に応じて読み替えてください。
  • こうしたほうがいいよ的なアドバイスを頂けると大変助かります
  • 主に不安な点: PostgreSQLの権限設定周り
  • わたしはこのあたりに生息しています: https://nagoyadon.jp/@kumasun

以上

17
18
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
17
18