Amazon EC2でのLet’s Encryptを用いたHTTPSの設定
概要
AWSでWebサービスを公開するにあたって,HTTPSにより安全な通信を実現する必要がある。
チュートリアル (チュートリアル: Amazon Linux 2 に SSL/TLS を設定する – Amazon Elastic Compute Cloud) を参考に,Let’s Encryptの証明書を使用して,Amazon Elastic Compute Cloud (EC2) でHTTPSを実現する。
なお,以下の2点は実施済みとする。
- 「チュートリアル: Amazon Linux 2 に LAMP ウェブサーバーをインストールする – Amazon Elastic Compute Cloud」でApacheをインストール
- 「AWSでの独自ドメイン名の設定」で独自ドメインの設定
セキュリティグループの設定
EC2で特別に設定が必要なのがセキュリティグループだ。デフォルトだと,インスタンスでHTTPS (ポート443) を受け付けないようになっているため,まずこれを受け付けるようにする。
[Network & Security]>[Security Groups] を選び,[Security Groups] から現在のEC2インスタンスを選び,[Actions]>[Edit inbound rules] を選ぶ。
[Add rule] を選ぶ。
ここは以下のように,HTTPSを受け入れるルールを設定して [Save rules] を選ぶ。
- Type: HTTPS
- Source: 0.0.0.0/0
これでセキュリティグループの設定は完了となる。
Let’s Encryptの証明書の取得
続いて,Let’s Encryptの証明書を取得する。
事前に独自ドメインを設定する必要があるので,まだの場合は「AWSでの独自ドメイン名の設定」を参考に設定しておく。
「CertbotによるLet’s Encryptの証明書の取得」に沿って証明書を取得する。
まず,Python3とpipを以下のコマンドでインストールする。
sudo yum install -y python3
続いて,Let’s Encryptの公式のACMEクライアントのCertbotをpipでインストールする。
pip3 install --user certbot==1.6.0
このとき,以下の設定を~/.profile
や~/.bashrc
に設定してPATHを設定しておく。
export PATH="$HOME/.local/bin:$PATH"
そして,以下のコマンドで証明書を取得する。
LOCAL=~/.local
certbot certonly --webroot -w /var/www/html -d aws.senooken.cf -m contact@example.com \
--agree-tos --no-eff-email \
--config-dir=$LOCAL/etc/letsencrypt \
--logs-dir=$LOCAL/var/log/letsencrypt \
--work-dir=$LOCAL/var/lib/letsencrypt
今回はaws.senooken.cfのドメインに対して証明書を取得した。
このコマンドにより,~/.local/etc/letsencrypt/live/aws.senooken.cf/
に証明書のfullchaing.pem
と秘密鍵のprivkey.pem
が生成される。
最後に証明書の自動更新の設定を行う。
crontab <<-"EOT"
* * * */1 * (LOCAL=/home/ec2-user/.local; $LOCAL/bin/certbot renew --webroot -w /var/www/html -d aws.senooken.cf -m contact@example.com --agree-tos --no-eff-email --config-dir=$LOCAL/etc/letsencrypt --logs-dir=$LOCAL/var/log/letsencrypt --work-dir=$LOCAL/var/lib/letsencrypt >/dev/null)
EOT
以下のコマンドでcronを再起動する。
sudo systemctl restart crond
以上でLet’s Encryptの証明書の設定が完了となる。
Let’s Encryptの証明書取得時のAWSのドメインでのエラー
なお,独自ドメインを設定せずに,EC2のデフォルトのドメイン (ec2-3-112-227-190.ap-northeast-1.compute.amazonaws.com
) に対してLet’s Encryptの証明書の取得を試みると以下のエラーが発生して失敗する。
ACME server returned an error: urn:ietf:params:acme:error:rejectedIdentifier :: The server will not issue certificates for the identifier :: Error creating new order :: Cannot issue for "ec2-3-112-227-190.ap-northeast-1.compute.amazonaws.com": The ACME server refuses to issue a certificate for this domain name, because it is forbidden by policy
以下に回答がある通り,AWSのドメインはすぐに破棄されてしまうためLet’s Encrypt側で拒絶しているようだ。
By policy, Let’s Encrypt does not allow people to get certificates for generic EC2 hostnames. Mostly because they can change hands far more quickly than certificates expire.
You need to use your own domain name of some sort.
(You can certainly use AWS, EC2, and EC2 IP addresses. You just can’t use the EC2 hostname.)
そのため,Let’s EncryptでのHTTPSを実現する場合,独自ドメインを事前に設定しておく。
WebサーバーのTLSの有効化
証明書を取得したので,Webサーバーに設定する。
Amazon Linux 2のApache HTTP Server (httpd) だとmod_ssl
が入っておらず,そのままではHTTPSに対応していない。そこで,まずはWebサーバーのTLSを有効にする。
以下のコマンドでmod_ssl
をインストールする。
sudo yum install -y mod_ssl
これにより,/etc/httpd/conf.d/ssl.conf
に設定ファイルが追加される。
続いて,取得したLet’s Encryptの証明書と秘密鍵を設定ファイルで参照する。設定内容については「Apache HTTP ServerでのHTTPSの設定方法」も参考にする。
/etc/httpd/conf.d/ssl.conf
を開いて,以下のように今回作成した証明書と秘密鍵を参照する。
# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. A new
# certificate can be generated using the genkey(1) command.
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
# SSLCertificateFile /home/ec2-user/.local/etc/ssl/letsencrypt/fullchain.pem
SSLCertificateFile /home/ec2-user/.local/etc/letsencrypt/live/aws.senooken.cf/fullchain.pem
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
# SSLCertificateKeyFile /home/ec2-user/.local/etc/ssl/letsencrypt/key.pem
SSLCertificateKeyFile /home/ec2-user/.local/etc/letsencrypt/live/aws.senooken.cf/privkey.pem
なお,mod_sslの読み込みは/etc/httpd/conf/httpd.conf
の56行目付近のInclude conf.modules.d/*.conf
で行っているので,特に何もしなくても問題ない。
最後にApacheを以下のコマンドで再起動する。
sudo apachectl restart
これでEC2でのHTTPS化が完了となる。最後に,Webブラウザーで対象ドメイン (例: https://aws.senooken.cf) にアクセスして表示できれば問題ない。
結論
Amazon EC2でのLet’s Encryptを用いたHTTPSの設定方法を記した。
ここまで来るのに,独自ドメインの取得,独自ドメインの設定,Let’s Encryptでの証明書の取得,AWSでの設定とわからないことが多く,2020-07-13頃から1週間近くかかった。
ようやくゴールに到達できてよかった。