VPSでサブドメインを適用し、複数のRailsアプリを運用する(CentOS 8.2 / Nginx)

VPSでサブドメインを適用し、複数のRailsアプリを運用する(CentOS 8.2 / Nginx)

September 5, 2020

CentOS(8.2)と Nginx の環境を前提としています。本記事の作業開始時点は、すで Rails アプリを1つデプロイしてあり、ドメインの適用、Certbot を利用した SSL 化も終わっている状態です。例えば、https://example.comにアクセスすると、デプロイ済のRailsアプリが表示されるようになっている状態を想定しています。

この記事のゴールは、すでにデプロイ済みの Rails アプリ(アプリ A)に加え、もう一つの Rails アプリ(アプリ B)をデプロイし、アプリ A には aaa.example.com のサブドメインを、アプリ B には bbb.example.com のサブドメインを適用および SSL 化することです。

なお、私の環境としては、前々回および前回投稿した以下の記事から連続して作業している状態です。

前々回:ConoHa VPS(CentOS 8.2)に Rails 6 + PostgreSQL + Nginx + Unicorn + Capistrano でデプロイする

前回:VPS の Rails に独自ドメインを適用し、SSL(https)化する(CentOS 8.2 / Nginx)

では、以下手順です。

1.サブドメインを取得・適用する #

example.com というドメインを取得しているとして、 aaa.example.combbb.example.com の2つのサブドメインをそれぞれの Rails アプリに適用するとします。

aaa.example.combbb.example.com を取得します。

以下、変更前の状態。

HOST NAME       | TYPE  |  TTL  | VALUE
--------------- | ----- | ----- | ----------------------------------
example.com     |  NS   | 86400 | 01.dnsv.jp
example.com     |  NS   | 86400 | 02.dnsv.jp
example.com     |  NS   | 86400 | 03.dnsv.jp
example.com     |  NS   | 86400 | 04.dnsv.jp
example.com     |  A    | 3600  | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.example.com | CNAME | 3600  | example.com

DNS レコード設定のページで、2つのサブドメインを追加する。www 無しと有りで2行ずつの計4行。

HOST NAME           | TYPE  | TTL  | VALUE
------------------- | ----- | ---- | ---------------------------------
aaa.example.com     |   A   | 3600 | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.aaa.example.com | CNAME | 3600 | aaa.example.com
bbb.example.com     |   A   | 3600 | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.bbb.example.com | CNAME | 3600 | bbb.example.com

以下、変更後の状態。

HOST NAME           | TYPE  |  TTL  | VALUE
------------------- | ----- | ----- | --------------------------------
example.com         |  NS   | 86400 | 01.dnsv.jp
example.com         |  NS   | 86400 | 02.dnsv.jp
example.com         |  NS   | 86400 | 03.dnsv.jp
example.com         |  NS   | 86400 | 04.dnsv.jp
example.com         |  A    | 3600  | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.example.com     | CNAME | 3600  | example.com
aaa.example.com     |  A    | 3600  | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.aaa.example.com | CNAME | 3600  | aaa.example.com
bbb.example.com     |  A    | 3600  | サーバの IP アドレス(xxx.xxx.xxx.xxx)
www.bbb.example.com | CNAME | 3600  | bbb.example.com

設定が反映され次第、 aaa.example.com (および www.aaa.example.com )、 bbb.example (および www.bbb.example.com )にアクセスすると、サーバにアクセスし、Nginx のページが表示されるようになっている。

なお、ネイキッドドメインである example.comwww.example.com の A レコード2行については、今後使用しないのであれば削除しても構わないが、今回はそのままにしておく。

2.不要となる SSL 証明書を削除する #

まずは以下を実行し、証明書の設定状況を確認する。

certbot certificates

上記を実行すると、取得済みの証明書の情報を出力する。 example.com に対してすでに発行済みと想定すると、1つ分の情報が出力され、Domains: example.com となっているはず。

サブドメインを使用するに伴い、 example.com 自体は使用しなくなるのであれば、証明書を削除しておく。

以下を実行し、証明書を失効させる。

certbot revoke --cert-name example.com

上記を実行すると証明書が失効し、その後下記の通り、証明書を削除するか、と質問されるので、Yes と回答して削除まで済ませておく。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you like to delete the cert(s) you just revoked, along with all earlier
and later versions of the cert?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es (recommended)/(N)o: Y # Y(はい)で回答

これで削除まで終わった。なお、上記で No と答えた場合でも、下記のコマンドで証明書を削除できる。

certbot delete --cert-name example.com

証明書の削除完了後、再び証明書の確認を実施。

certbot certificates

証明書が消えていれば、No certs found. と返ってくる。

3.サブドメインを設定、SSL 化する #

3 − 1.Nginx の設定ファイルでサブドメインを設定する #

サーバに接続し root ユーザにスイッチした後、すでにデプロイ済みのアプリ A の設定ファイルを開く。

vim /etc/nginx/conf.d/アプリケーションA名.conf

なお、Certbot で SSL 設定を行った時、Nginx の設定ファイルも自動で書き換えられていたが、証明書を削除した際には Nginx の設定ファイルは自動では修正されない。

そのため、前回の記事から作業しているとすると、設定ファイルの中身は下記のようになっているはず。

upstream unicorn_アプリケーションA名 {
  server unix:/var/www/アプリケーションA名/current/tmp/sockets/unicorn.sock;
}
server {
  listen 443;
  ssl on;
  server_name example.com;
  root /var/www/アプリケーションA名/current/public;
  access_log /var/log/nginx/アプリケーションA名_access.log;
  error_log /var/log/nginx/アプリケーションA名_error.log;
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  location / {
    try_files $uri @unicorn;
  }
  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unicorn_アプリケーション名;
  }
}
server {
  listen 443;
  server_name www.example.com サーバのIPアドレス;
  return 301 https://example.com$request_uri;
}
server {
  listen 80;
  server_name example.com www.example.com サーバのIPアドレス;
  return 301 https://example.com$request_uri;
}

Certbot 適用前に戻す、およびサブドメインに変えるため下記を変更。

  • 1つめの listen を 443 から 80 に変更
  • ssl on;を削除
  • Certbot による SSL 設定を削除し
  • server_name 部分をサブドメイン( aaa.example.com )に変更
  • server_name 部分のサーバの IP アドレス記載を削除
  • return 部分をサブドメイン( aaa.example.com )に変更

変更後は下記の通り(server 部分のみ記載)。

server {
  listen 80;
  ssl on;
  server_name aaa.example.com;
  root /var/www/アプリケーションA名/current/public;
  access_log /var/log/nginx/アプリケーションA名_access.log;
  error_log /var/log/nginx/アプリケーションA名_error.log;
  location / {
    try_files $uri @unicorn;
  }
  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unicorn_アプリケーション名;
  }
}
server {
  listen 443;
  server_name www.aaa.example.com;
  return 301 https://aaa.example.com$request_uri;
}
server {
  listen 80;
  server_name aaa.example.com www.aaa.example.com;
  return 301 https://aaa.example.com$request_uri;
}

変更後、設定内容をテスト、問題なければ Nginx を再起動し、

nginx -t
systemctl restart nginx

http://aaa.example.com にアクセスし、アプリが表示されることを確認する。

3 − 2.SSL 証明書の取得と設定を行う #

Certbot を使って SSL 証明書の取得と設定を行う。

certbot --nginx

証明書を取得、設定できていることを確認する。

certbot certificates

aaa.example.com ドメインの証明書情報が表示されれば OK.

これで HTTPS で接続できるようになった。 https://aaa.example.com でアクセスできることを確認する。

(証明書更新のクーロン設定は、初回の独自ドメインの SSL 化時に実施済みと想定して省略)

念のため、ドライラン(=テスト)を実施して、自動更新に問題がないか確認する。

certbot renew --dry-run

aaa.example.com の証明書に対して処理が行われており、「Congratulations, all renewals succeeded.」となれば OK.

3 − 3.Nginx の設定を修正する #

Certbot が自動更新しているため、改めて記載を整えておく。以下は修正後の例。

server {
  listen 443;
  ssl on;
  server_name aaa.example.com;
  root /var/www/アプリケーションA名/current/public;
  access_log /var/log/nginx/アプリケーションA名_access.log;
  error_log /var/log/nginx/アプリケーションA名_error.log;
  ssl_certificate /etc/letsencrypt/live/aaa.example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/aaa.example.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  location / {
    try_files $uri @unicorn;
  }
  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unicorn_アプリケーション名;
  }
}
server {
  listen 443;
  server_name www.aaa.example.com;
  return 301 https://aaa.example.com$request_uri;
}
server {
  listen 80;
  server_name aaa.example.com www.aaa.example.com;
  return 301 https://aaa.example.com$request_uri;
}

修正後は忘れずテスト&再起動。

nginx -t
systemctl restart nginx

4.もう一つの Rails アプリをデプロイする #

ここまでで既存のアプリのサブドメイン適用、SSL 化は完了した。次にもう一つのアプリをデプロイする。1つ目アプリをデプロイするまでに Ruby のインストール等の環境構築は終わっているため、2つ目のアプリのデプロイに実施すべきことはわずか。

前々回に投稿した以下の記事に準拠して、2つ目のアプリのデプロイを進める。

ConoHa VPS(CentOS 8.2)に Rails 6 + PostgreSQL + Nginx + Unicorn + Capistrano でデプロイする

4 − 1.Rails アプリで使用する DB を作成する #

前々回の記事中の以下を参照して実施。

→ 13.Rails アプリで使用する DB を作成する #

4 − 2.Capistrano、Unicorn をインストールする #

前々回の記事中の以下を参照して実施。

→ 15 − 2.Capistrano、Unicorn をインストールする #

なお、 /config/deploy/production.rb について、SSH のポート番号を変えている場合は、以下の通りポート番号を記載することを忘れずに。

→ 参考:80 − 1.SSH のポート番号を変更する #
server 'サーバのIPアドレス', user: 'サーバの作業用ユーザ名', port: '50022', roles: %w{app db web}
set :ssh_options, keys: 'ローカルの秘密鍵のパス'

また、 /config/deploy.rb について、GitHub をプライベートリポジトリにしている場合は、以下の通り set :repo_url でトークンを含めることを忘れずに。

→ 参考:80 − 2.GitHub のプライベートリポジトリからデプロイする #
set :repo_url, 'https://トークン:@github.com/xxxx/yyyy' # クローンするGitHubリポジトリ(xxxxはユーザ名、yyyyはアプリ名)

4 − 3.Unicorn の自動起動設定をする #

前々回の記事中の以下を参照して実施。

→ 15 − 3.Unicorn の自動起動設定をする #

4 − 4.Nginx の設定ファイルを作成 #

前々回の記事中の以下を参照して実施。

→ 15 − 4.Nginx の設定ファイルを作成 #

作成する設定ファイルの server_name 部分は、サーバの IP アドレスではなくサブドメインを記載しておく。サブドメインを bbb.example.com とした場合の記載例は以下。

server_name bbb.example.com;

4 − 5.デプロイできる状態か確認する #

前々回の記事中の以下を参照して実施。

→ 15 − 5.デプロイできる状態か確認する #

4 − 6.環境変数を設定する #

前々回の記事中の以下を参照して実施。

→ 15 − 6.環境変数を設定する #

4 − 7.改めてデプロイできる状態か確認する #

前々回の記事中の以下を参照して実施。

→ 15 − 7.改めてデプロイできる状態か確認する #

4 − 8.デプロイを実行する #

前々回の記事中の以下を参照して実施。

→ 16.デプロイを実行する #

デプロイ完了後、 http://bbb.example.com (トップページを設定していないのであれば、 http://bbb.example.com/blogs など存在するページを指定)して、Rails アプリのページが表示されることを確認する。

5.SSL 化する #

本記事で aaa.example.com に対して行ったことを、 bbb.example.com に対して行うだけ。

5 − 1.SSL 証明書の取得と設定を行う #

本記事の以下を参照して実施。

→ 3 − 2.SSL 証明書の取得と設定を行う #

なお、 certbot -nginx を実行した際に、以下のように対象のドメインを聞かれるので、 bbb.example.com を選択するように。

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: aaa.example.com
2: www.aaa.example.com
3: bbb.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 3 # bbb.example.comを選択

5 − 2.Nginx の設定を修正する #

本記事の以下を参照して実施。

→ 3 − 3.Nginx の設定を修正する #

参考として、upstream 部分含め変更後のファイル内容例全行を下記に記載しておく。

upstream unicorn_アプリケーションB名 {
  server unix:/var/www/アプリケーションB名/current/tmp/sockets/unicorn.sock;
}
server {
  listen 443;
  ssl on;
  server_name bbb.example.com;
  root /var/www/アプリケーションB名/current/public;
  access_log /var/log/nginx/アプリケーションB名_access.log;
  error_log /var/log/nginx/アプリケーションB名_error.log;
  ssl_certificate /etc/letsencrypt/live/bbb.example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/bbb.example.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  location / {
    try_files $uri @unicorn;
  }
  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unicorn_アプリケーション名;
  }
}
server {
  listen 443;
  server_name www.bbb.example.com;
  return 301 https://bbb.example.com$request_uri;
}
server {
  listen 80;
  server_name bbb.example.com www.bbb.example.com;
  return 301 https://bbb.example.com$request_uri;
}

これで2つ目のアプリの SSL 化も終わり。

以上 #

3つ、4つとアプリをデプロイしたい場合は、単純に上記を繰り返すだけです。

今回、VPS の環境構築から始まり、複数アプリのデプロイまでやったので、若干達成感がありますね。