# Mastodon構築 手順書 # サーバー - [ ] EC2インスタンスの作成 素のUbuntuを使用 - [ ] スワップの設定 t2.nanoだとメモリ容量がたりない可能性があるため ``` $ sudo fallocate -l 4G /swapfile $ sudo chmod 600 /swapfile $ sudo mkswap /swapfile $ sudo swapon /swapfile $ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab ``` - [ ] システムパッケージの更新 `$ apt update && apt upgrade -y` # fail2ban - [ ] fail2banをインストール 繰り返しログイン試行をブロック `$ apt install fail2ban` confファイルを編集する。 ``` $ vi /etc/fail2ban/jail.conf [DEFAULT] destemail = your@email.here sendername = Fail2Ban [sshd] enabled = true port = 22 [sshd-ddos] enabled = true port = 22 ``` - [ ] fail2banを再起動 `$ systemctl restart fail2ban` # ファイアウォールの設定 - [ ] ファイアウォールをインストールし、SSH、HTTP、HTTPSポートのみをホワイトリストに登録 `$ apt install -y iptables-persistent` - [ ] rules.v4ファイルを編集 ``` $ vim /etc/iptables/rules.v4 *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 ``` - [ ] 手動で読み込み `$ iptables-restore < /etc/iptables/rules.v4` # ミドルウェア等のインストール - [ ] Node.jsをインストール `$ curl -sL https://deb.nodesource.com/setup_8.x | bash - ` - [ ] Yarnをインストール Facebookが出しているnpmと互換性があるNodeパッケージマネージャー (Pythonでいうpip) ``` $ 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 python-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev ``` # Mastodonユーザーの作成 - [ ] Mastodonユーザーを作成 `$ adduser --disabled-login mastodon` - [ ] ユーザーを切り替え `$ su - mastodon` # インストール続き - [ ] Rubyのインストール rbenvを使用してRubyのバージョンを管理 ``` $ 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 $ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build ``` - [ ] 正しいRubyバージョンをインストール ``` $ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.6.5 $ rbenv global 2.6.5 ``` - [ ] gemを更新 Rubyのパッケージ ruby2.6.0に同梱されているデフォルトのgemバージョンは最新のバンドラーと互換性がないため `$ gem update --system` - [ ] Bundlerをインストール Rubyのパッケージマネージャ&環境分離ツール ≒composerや(pip+venv)など `$ gem install bundler --no-document` - [ ] rootユーザーに戻る # PostgreSQL Docker上にあったPostgreSQLのバージョンは9.6でした。 UbuntuにPostgreSQLをインストールするとデフォルトでバージョン10が入るため、いったん10を削除して 9.6バージョンを指定して入れる必要があります。 (その場合は、別途aptリポジトリ※後述を作成しなければいけません) `/etc/postgresql` 以下で ``` $ ls 10 ``` - [ ] 10を削除 `$ apt remove postgresql postgresql-contrib` (参照) https://www.postgresql.org/download/linux/ubuntu/ - [ ] ubuntuの**aptリポジトリ**を追加。 18.04なので ``` $ vim /etc/apt/sources.list.d/pgdg.list deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main ``` - [ ] リポジトリ署名キーをインポートし、パッケージリストを更新。 ``` $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - ``` `$ sudo apt-get update` - [ ] 9.6を指定してインストール。 `$ apt-get install postgresql-9.6` # PostgreSQLの設定 以下を参考にPostgreSQLの設定を行います。 https://pgtune.leopard.in.ua/#/ DBのバージョンやインスタンスのサイズを入力していきます。 PostgreSQLの設定については別途記事 『[PostgreSQLの設定 \~PGTuneを使ってみて~](https://qiita.com/suwa3/items/ccec9a757408a6fab695)』 に、まとめてみました。 `$ vim /etc/postgresql/9.6/main/postgresql.conf` - [ ] 設定が完了したら、再起動 `$ systemctl restart postgresql` - [ ] Mastodonが使用できるPostgreSQLユーザーを作成。 ``` $ sudo -u postgres psql CREATE USER mastodon CREATEDB; \q ```