mastodonをローカルに構築(非docker版)
mastodonサーバを公式サイトを参考にしながら構築してみます。
基本的には以下の公式サイトに記載されている通りに進めれば構築することができます。
今回はテスト実行用のためにローカルに構築します。そのためセキュリティの設定などは行いません。
公式サイトではサーバ証明書をLet’s Encryptから取得してきていますが、ここではオレオレ証明書で代用しています。
■公式サイト
https://docs.joinmastodon.org/administration/installation/#basic-server-setup-optional
構築環境の確認
rootユーザに切り替えます。
$ su root
password:***********←rootユーザのパスワードを入力
OSのバージョンを確認します。
今回はUbuntu Server 18.0.4で構築します。
# cat /etc/issue
Ubuntu 18.04.2 LTS \n \l
OSのアップデートを行います。
# apt update && apt upgrade -y
mastodonに必要な依存関係のインストール
rootユーザのデフォルトのシェルをログインシェルとして実行します。
# sudo -i
Ubuntuリポジトリを追加します。
依存関係にあるファイルをインストールするためにmultiverse、universeのリポジトリを追加します。
# add-apt-repository multiverse
# add-apt-repository restricted
# apt update
node.jsリポジトリ
# apt -y install cur
# curl -sL https://deb.nodesource.com/setup_8.x | bash -
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
その他の依存関係
# 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
mastodonを実行するユーザ(mastodon)を作成
# adduser mastodon
ユーザ(mastodon)としてログイン
# sudo su - mastodon
Ruby実行環境の構築
mastodonはRubyで作成されています。そのためRubyの実行環境を整えます。
Rubyのバージョン管理ツールであるrbenvをインストールします。
$ 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
$ exec bash
rbenvがインストールされたことを確認します。
下記のコマンドを実行して「rbenvは関数です(rbenv is a function)」と表示されることを確認します。
$ type rbenv
Rubyをインストールするために必要なrbenvのプラグインのruby-buildをインストールします。
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
mastodonの実行に必要なRubyをインストールします。
$ rbenv install 2.5.1
$ rbenv global 2.5.1
// インストールしたRubyのバージョンを確認
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
node.jsとRubyの依存関係をインストールします。
# cd ~
# git clone https://github.com/tootsuite/mastodon.git live
# git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
# gem install bundler
# bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test
# yarn install --pure-lockfile
※「gem install bundler」を実行したときに
rbenv: version `2.6.0′ is not installed (set by /home/mastodon/live/.ruby-version)
と表示された場合、mastodonの実行に必要なRubyのバージョンとインストールされたRubyのバージョンが異なっています。
以下のコマンドを実行して適切なバージョンをインストールします。
# rbenv install 2.6.0
# rbenv global 2.6.0
データベースサーバをインストール
mastodonで使用するデータベースを作成します。
ここではPostgreSQLを使用しています。
$ sudo -u postgres psql
postgres=# CREATE USER mastodon CREATEDB;
postgres=\q
Nginxを設定
$ cd /etc/nginx/sites-available
$ sudo vi /etc/nginx/sites-available/example.com.conf
以下の設定を「example.com.conf」に記載します。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name example.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 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 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の設定を反映させます。
$ cd /etc/nginx/sites-enabled
$ ln -s ../sites-available/example.com.conf
サーバ証明書を設定
今回はmastodonをローカルで実行するので自己署名証明書を使用することにします。
OpenSSLで証明書を作成する際に情報を聞かれるので適当に入力します。
$ cd ~/live
$ mkdir key
$ openssl genrsa -des3 -out server.key 2048
$ openssl req -new -key server.key -out server.csr
$ cp server.key server.key.org
$ openssl rsa -in server.key.org -out server.key
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
nginxの設定ファイルに先ほど作成した証明書を設定します。
中略…
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_certificate /home/mastodon/live/key/server.crt; ←サーバ証明書のディレクトリを設定します。
ssl_certificate_key /home/mastodon/live/key/server.key; ←サーバ証明書の秘密鍵のディレクトリを設定します。
keepalive_timeout 70;
sendfile on;
client_max_body_size 80m;
後略…
mastodonの設定
mastodonを実行するための設定情報を聞かれるので適当に入力します。
$ sudo su - mastodon
$ cd ~/live
$ RAILS_ENV=production bundle exec rake mastodon:setup
Domain name:localhost
Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? (y/N)y
Are you using Docker to run Mastodon? (Y/n)n
PostgreSQL host:(/var/run/postgresql)
PostgreSQL port:(5432)
Name of PostgreSQL database:(mastodon_production)
Name of PostgreSQL user:(mastodon)
Password of PostgreSQL user:
Redis host:(localhost)
Redis port:(6379)
Redis password:
Do you want to store uploaded files on the cloud? (y/N)N
Do you want to send e-mails from localhost? (y/N)N
SMTP server:smtp.gmail.com
SMTP port:465
SMTP username:******@gmail.com
SMTP password:********
SMTP authentication:(plain)
SMTP OpenSSL verify mode:
E-mail address to send e-mails "from":
Send a test e-mail with this configuration right now? y
send test e-mail to:
This configuration will be written to .env.production
Save configuration is saved, the database schema must be loaded.
Prepare the database now? (Y/n)Y
The final step is compiling CSS/JS assets.
This may take a while and consume a lot of RAM.
Compile the assets now? (Y/n)Y
Do you want to create an admin user straight away? (Y/n)Y
Username:(admin)
E-mail:
mastodonのシステムサービスファイルを作成
$ sudo 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
$ sudo 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
$ sudo 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
mastodonのサービスを有効にします。
$ sudo systemctl enable /etc/systemd/system/mastodon-*.service
mastodonのサービスを起動
$ sudo systemctl start mastodon-*.service
mastodonのサービスが起動していることを確認します。
$ sudo systemctl status mastodon-*.service
● mastodon-sidekiq.service - mastodon-sidekiq
Loaded: loaded (/etc/systemd/system/mastodon-sidekiq.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-02-11 19:35:18 JST; 6s ago
Main PID: 15049 (bundle)
Tasks: 2 (limit: 4663)
CGroup: /system.slice/mastodon-sidekiq.service
mq15049 /home/mastodon/live/vendor/bundle/ruby/2.6.0/bin/sidekiq -c 5 -q default -q push -q mailers -q pull
2月 11 19:35:18 mastodon-VirtualBox systemd[1]: Started mastodon-sidekiq.
2月 11 19:35:24 mastodon-VirtualBox bundle[15049]: 2019-02-11T10:35:24.194Z 15049 TID-gpc8ct3l9 INFO: Booting Sidekiq 5.2.5 wi
● mastodon-streaming.service - mastodon-streaming
Loaded: loaded (/etc/systemd/system/mastodon-streaming.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-02-11 19:35:18 JST; 6s ago
Main PID: 15053 (npm)
Tasks: 27 (limit: 4663)
CGroup: /system.slice/mastodon-streaming.service
tq15053 npm
tq15087 sh -c node ./streaming/index.js
tq15088 node ./streaming/index.js
mq15094 /usr/bin/node /home/mastodon/live/streaming/index.js
2月 11 19:35:18 mastodon-VirtualBox systemd[1]: Started mastodon-streaming.
2月 11 19:35:19 mastodon-VirtualBox npm[15053]: > mastodon@ start /home/mastodon/live
2月 11 19:35:19 mastodon-VirtualBox npm[15053]: > node ./streaming/index.js
2月 11 19:35:20 mastodon-VirtualBox npm[15053]: info Starting streaming API server master with 1 workers
2月 11 19:35:20 mastodon-VirtualBox npm[15053]: info Starting worker 1
2月 11 19:35:20 mastodon-VirtualBox npm[15053]: info Worker 1 now listening on 0.0.0.0:4000
2月 11 19:35:20 mastodon-VirtualBox npm[15053]: node_redis: Warning: Redis server does not require a password, but a password
2月 11 19:35:20 mastodon-VirtualBox npm[15053]: node_redis: Warning: Redis server does not require a password, but a password
● mastodon-web.service - mastodon-web
Loaded: loaded (/etc/systemd/system/mastodon-web.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-02-11 19:35:18 JST; 6s ago
Main PID: 15054 (bundle)
Tasks: 2 (limit: 4663)
CGroup: /system.slice/mastodon-web.service
mq15054 puma 3.12.0 (tcp://0.0.0.0:3000) [live]
2月 11 19:35:18 mastodon-VirtualBox systemd[1]: Started mastodon-web.
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] Puma starting in cluster mode...
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] * Version 3.12.0 (ruby 2.6.0-p0), codename: Llamas in Pajamas
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] * Min threads: 5, max threads: 5
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] * Environment: production
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] * Process workers: 2
2月 11 19:35:19 mastodon-VirtualBox bundle[15054]: [15054] * Preloading application
mastodonにアクセス
Webブラウザでmastodonにアクセスしてみます。
https://localhost
以下のような画面が表示されれば成功です。

Discussion
New Comments
No comments yet. Be the first one!