見出し画像

Google-GCPのロードバランサを使わないでSSL対応する方法 [GCP-xampp/Apache]

以前、GCPのロードバランサを設置してSSL対応してみたのですが、知らないうちに(無知のため)そこそこNetwork課金されたため、他のやり方を試してみることにしました。

何はともあれドメイン取得~DNSレコード設定

ということで、ドメイン取得から始まります。ドメイン取得は「お名前ドットコム」を使います。詳細は省略しますが、
 ・ドメインの登録 → あなたのドメイン名.com (私は.comで取得)
 ・ネームサーバー → 「お名前.comのネームサーバーを使う」を設定
 ・DNSレコード設定(Aレコード)→ 「VALUE:GCP-VMインスタンスの外部IPアドレス」を設定
基本この3点で良いでしょう。この設定が終わったら反映されるまで数時間程度待ちましょう。

どれぐらい待てばよいのか?

CMANのページでnslookupテスト(DNSAサーバ接続確認)をして設定したアドレス(DNSレコード(Aレコード)のVALUEの値)が返ってくるか確認できるまでです。
「ホスト名(FQDN)を指定してください」のところに登録したあなたのドメイン名.comを入れて、下にあるnslookup実行を押してください。
確認結果のところに
Non-authoritative answer:
Name: あなたのドメイン名.com
Address: GCP-VMインスタンスの外部IPアドレス
になっていればOKです。私は20分程度待ってOKになりました。

Certbotを使ってSSL証明書の取得する

次にSSL証明書の取得(無料)です。
ComputeEngine上で以下のコマンドを打ってください。
なお、このときComputeEngine上でWebサーバが走っている場合は止めてください。

sudo certbot certonly --standalone -d あなたのドメイン名.com

xampp-Apacheの設定を更新する

取得したSSL証明書をApacheに設定する必要がありますので、以下の手順で設定を行ってください。

cd /opt/lampp/apache2/conf

sudo vi httpd.conf

以下を追記して保存します。

<VirtualHost *:80>
    ServerName あなたのドメイン名.com
    Redirect permanent / https://あなたのドメイン名.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName あなたのドメイン名.com

    DocumentRoot /opt/lampp/htdocs

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/あなたのドメイン名.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/あなたのドメイン名.com/privkey.pem

    <Directory /opt/lampp/htdocs>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

xampp-Apacheを起動してからあなたのページへブラウザでアクセスしてみてください。

sudo /opt/lampp/lampp startapache

https://あなたのドメイン名.com/top.htmlとか(/opt/lampp/htdocs/top.htmlの場合) うまくアクセスできていたらOKです。おめでとうございます!

SSL証明書の自動更新

Let's Encryptの証明書は90日間の有効期限があるため、Certbotの自動更新機能を設定します。

sudo crontab -e

一番下の行を追加します。

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

0 2 * * * /usr/bin/certbot renew --quiet

おわりに

いかがだったでしょうか。これでGCPのロードバランサを使わず、すなわちNetworkingのコストをかけずにSSL化することができました。お名前ドットコムでドメインの取得ですが無料で取得できるみたいなので、よく見てから取得してみてください!

いいなと思ったら応援しよう!