5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7.3にMastodonの構築(Dockerなし)

Last updated at Posted at 2017-07-22

環境

CentOS 7.3をminimal インストールしたOSで構築
参考資料:Mastodon Production Guide

事前準備

Mastodonユーザを作成し作業する。

依存関係

$ sudo yum install libxml2-devel ImageMagick libxslt-devel git curl file g++ protobuf-compiler protobuf-devel

リポジトリのインストール

$ sudo yum -y install epel-release
$ sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
$ sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

FFmpegのインストール

$ sudo yum -y install ffmpeg ffmpeg-devel

開発ツールをインストール

$ sudo yum group install "Development tools"

Node.jsのインストール

$ curl -sL https://rpm.nodesource.com/setup_6.x | sudo bash -
$ sudo yum install nodejs

yarnのインストール

$ sudo npm install -g yarn

Redisのインストール

$ sudo yum install redis rubygem-redis

PostgreSQLのインストール

$ sudo yum install postgresql-server postgresql postgresql-contrib postgresql-devel
$ sudo postgresql-setup initdb
$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql
  • postgresユーザ(スーパーユーザ)でデータベースにログイン
$ sudo -u postgres psql
  • Mastodon用のデータベースを作成
postgres=# CREATE USER mastodon CREATEDB;
postgres=# \q

rbenvのインストール

  • GitHubからクローニング
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  • コンパイル
$ cd ~/.rbenv && src/configure && make -C src
  • PATHを通す
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
  • rbenvのプラグインをインストール
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
  • 依存関係のインストール
$ sudo yum install openssl-devel readline-devel
  • rbenvをインストール
$ rbenv install 2.4.1
  • デフォルトで使用するrubyのバージョンを指定
$ rbenv global 2.4.1

Mastodonのインストール

$ sudo -sHu mastodon
$ cd ~
$ git clone https://github.com/tootsuite/mastodon.git live
$ cd live
  • 依存関係のインストール
$ gem install bundler
$ bundle install --deployment --without development test
$ yarn install --pure-lockfile
  • Mastodonの設定ファイルをバックアップして編集1
$ cp .env.production.sample .env.production
$ vi .env.production
.env.production
=begin
6-10行目を以下のように変更
redisのホスト/ポート、postgresのホスト/ポート/ユーザ名/パスワード、ドメイン名の設定
=end

REDIS_HOST=redis
REDIS_PORT=6379
DB_HOST=db
DB_USER=postgres
DB_NAME=postgres

REDIS_HOST=localhost
REDIS_PORT=6379
DB_HOST=/var/run/postgresql
DB_USER=mastodon
DB_NAME=mastodon_production

=begin
50-55行目・59行目を以下のように変更
SMTPの設定 for Gmail
=end

SMTP_SERVER=smtp.mailgun.org
SMTP_PORT=587
SMTP_LOGIN=
SMTP_PASSWORD=
SMTP_FROM_ADDRESS=notifications@example.com
#SMTP_DOMAIN= # defaults to LOCAL_DOMAIN

SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_LOGIN=****@gmail.com
SMTP_PASSWORD=****(Gmailにログイン時のパスワード)
SMTP_FROM_ADDRESS=****@gmail.com
SMTP_DOMAIN=gmail.com

#SMTP_OPENSSL_VERIFY_MODE=peer

SMTP_OPENSSL_VERIFY_MODE=none

secret key を自動生成する(3回)2

$ bundle exec rake secret

生成されたkeyを.env.production の以下の3箇所に貼り付ける

.env.production
=begin
30-32行目にkeyを貼り付ける
=end

PAPERCLIP_SECRET=****
SECRET_KEY_BASE=****
OTP_SECRET=****

Nginxのインストールと設定

$ sudo yum install nginx
  • 設定ファイルを作成3 (公式のコピペ)
$ vi /etc/nginx/conf.d/mastodon.conf
mastodon.conf
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  # 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 example.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/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 0;

  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 ~ ^/(packs|system/media_attachments/files|system/accounts/avatars) {
    add_header Cache-Control "public, max-age=31536000, immutable";
    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 start nginx
$ systemctl enable nginx

Mastodonのセットアップ

$ cd ~/live
$ RAILS_ENV=production bundle exec rails db:setup
$ RAILS_ENV=production bundle exec rails assets:precompile
  • システムのユニットファイルを作成(公式のコピペ)
$ sudo vi /etc/systemd/system/mastodon-web.service
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

$ sudo vi /etc/systemd/system/mastodon-sidekiq.service
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

$ sudo vi /etc/systemd/system/mastodon-streaming.service
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
  • Mastodonを起動し、自動起動するように設定
$ sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
$ sudo systemctl enable /etc/systemd/system/mastodon-*.service

オレオレ証明書の作成

$ cd /home/mastodon
$ openssl genrsa -out server.key 2048
$ openssl req -new -key server.key -out server.csr		
	You are about to be asked to enter information that will be incorporated	
	into your certificate request.	
	What you are about to enter is what is called a Distinguished Name or a DN.	
	There are quite a few fields but you can leave some blank	
	For some fields there will be a default value,	
	If you enter '.', the field will be left blank.	
	-----	
	Country Name (2 letter code) [XX]:JP	
	State or Province Name (full name) []:PaoState	
	Locality Name (eg, city) [Default City]:PaoCity	
	Organization Name (eg, company) [Default Company Ltd]:PaOrganization	
	Organizational Unit Name (eg, section) []:	
	Common Name (eg, your name or your server's hostname) []:mastodon.example.jp
	Email Address []:	
		
	Please enter the following 'extra' attributes	
	to be sent with your certificate request	
	A challenge password []:	
	An optional company name []:
$ openssl x509 -req -in server.csr -signkey server.key -sha256 -days 3650 -out server.crt	
$ openssl x509 -in server.crt -out server.der -outform DER	
$ openssl x509 -in server.der -inform DER -out server.pem -outform pem	
  • 作成した証明書をMastodonの設定ファイルに指定
$ sudo vi /etc/nginx/conf.d/mastodon.conf
mastodon.conf
# 25~26行目を変更
ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

ssl_certificate     /home/mastodon/server.pem;
ssl_certificate_key /home/mastodon/server.key;
  • Nginxを再起動
$ sudo nginx -s reload

ファイアウォールの開放

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

最後に

https://{mastodon.example.jp}/aboutにアクセスし、Mastodonのログイン画面が表示されることを確認

  1. シングルユーザモード(SINGLE_USER_MODE)を有効にすると、2ユーザ以上登録できないようにできる。

  2. bundle exec rake secretを実行すると、NameError: uninitialized constant Annotateが出るためこちらを参考。

  3. conf.d/配下はドロップインコンフィグというらしい。

5
9
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
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?