LoginSignup
0
1

More than 5 years have passed since last update.

マストドンを「GCP上」で「VM上のDocker」と「CloudSQL」と「Storage」を使って構築してみた。

Last updated at Posted at 2017-08-17

マストドンを「GCP上」で「VM上のDocker」と「CloudSQL」と「Storage」を使って構築

いろんなサイトを参考にしました。感謝!

1)VM作ります。

Ubuntuで作りました。
静的IPにしておきます。(説明省略)

作業PCはMacです。Macにgcloudを入れてあります。
(参考:MacのターミナルからVMに入る方法)→psqlを使うときはバックスラッシュ利用のため こっちが便利かな
$ gcloud compute ssh [サイト名] --zone [ゾーン名](例)

2)以下、VM作業。

面倒なので、suで作業、

$sudo su

「必要ライブラリがありません問題」対策といえば、apt-file

# apt-get update
# apt-get upgrade
# apt-get install apt-file

タイムゾーンをJSTに

# timedatectl set-timezone Asia/Tokyo

dockerをapt-getリポジトリに追加

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# apt-get update
# apt-get upgrade

dockerインストール

# apt-get install -y docker-ce

docker-composeインストール この時点での最新バージョンは、1.15.0だったです。

# VERSION=1.15.0
# curl -L https://github.com/docker/compose/releases/download/$VERSION/docker-compose-`uname -s-uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# ls -al /usr/local/bin/d*

ポスグレクライアントインストール

# apt install postgresql-client-common
# apt-get install postgresql-client

pg_configはここにあるらしい

# apt-get install libpq-dev

そして、mastodonインストール。バージョンはmasterでなく意図的に1.5.1に。
# git clone https://github.com/tootsuite/mastodon.git
# cd mastodon/
# git branch
# git checkout -b v1.5.1
# git branch

3)DNS設定

CloudDNSで、Aレコードを設定(説明省略)

4)DB作成

CloudSQLを利用

とりあえず、データベース名はわかりやすく。
user:postgres
pwd:パスワードを設定
外部IPをメモ
autovacuumを設定
承認済みネットワークに[VMのIPアドレス]を設定

(参考:接続方法)
VMから:
# psql -h [VMのIPアドレス] -U postgres

Cloud Shell、又は、Macターミナルから:
$ gcloud sql connect データベース名 --user postgres

ログインしたらタイムゾーン変更

postgres=> alter database postgres set timezone = 'Asia/Tokyo';

5)Storage作成

ブラウザ>バケットの作成

とりあえず、バケット名はわかりやすく。
Multi-Regional
US(→一応同じ国に)
にしました。

次に、相互運用性のアクセスキーを作成。

アクセスキーと、非公開をメモる。

6)nginxインストール

(参考:ubuntuバージョン取得方法)
# VCNAME=`cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2`
# echo $VCNAME

apt-getに入れるために、キー追加
# curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
下記2行を「/etc/apt/sources.list」に追加
ーーー
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx
ーーー
# apt-get update
# apt-get install nginx

7)無料証明書

certbot-autoインストール
# wget https://dl.eff.org/certbot-auto
# chmod a+x certbot-auto
# ./certbot-auto
取得
# ./certbot-auto certonly --standalone -d [サイト名] --rsa-key-size 4096

ついでに、DHparamでサイト評価アップ
# mkdir /etc/nginx/ssl
# cd /etc/nginx/ssl
# openssl dhparam 4096 -out dhparam.pem

8)nginx設定

nginx.confをメンテ。下記を追加。
ーーー
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_trusted_certificate /etc/letsencrypt/live/[サイト名]/chain.pem;
ssl_stapling on;
ssl_stapling_verify on;
ーーー

次に、マストドンのドキュメントから、コピペで、./conf.d/mastodon.confを作成。
サーバ名をメンテ。

9).env.production設定

ストレージ

ーーー
# S3 (Minio Config (optional) Please check Minio instance for details)
S3_ENABLED=true
S3_BUCKET=バケット名
AWS_ACCESS_KEY_ID=アクセスキー
AWS_SECRET_ACCESS_KEY=非公開キー
S3_REGION=[リージョン名]
S3_PROTOCOL=https
S3_HOSTNAME=storage.googleapis.com
S3_ENDPOINT=https://storage.googleapis.com
# S3_SIGNATURE_VERSION=
ーーー

Postgresql

ーーー
# You may set DATABASE_URL instead for more advanced options
DB_HOST=メモったIPアドレス
DB_USER=postgres
DB_NAME=postgres
DB_PASS=パスワード
DB_PORT=5432
ーーー

メール

今回は、mailgunに登録して設定(省略)

10)まず、ビルド

# docker-compose build

11)キー作成→転記

キー作成3回(.env.productionに転記)

# docker-compose run --rm web rake secret
# docker-compose run --rm web rake secret
# docker-compose run --rm web rake secret

WebPush用のキー(.env.productionに転記)

# docker-compose run --rm web rake mastodon:webpush:generate_vapid_key

12)DBとアセット

DBマイグレーション

# docker-compose run --rm web rails db:migrate

Assetsプレコンパイル

# docker-compose run --rm web rails assets:precompile

13)起動してみる

# docker-compose up -d
# docker ps

14)nginx起動

# nginx

15)ブラウザで開いてみる。

https://サイト名

(参考:エラーになっちゃったときの、個別にメアド確認方法)
# docker-compose run --rm web rails mastodon:confirm_email USER_EMAIL=[メアド名]

16)ログインできたら、アドミン付与

# docker-compose run --rm web rails mastodon:make_admin USERNAME=ユーザ名

(参考:データベースが壊れたとき(壊した!?)のリセット?方法)
#psql -h メモったIPアドレス -U postgres -d template1
>drop database postgres;
>create database postgres;
>\q


おしまい。

0
1
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
0
1