AWSにWordPressを構築する 2(SSL Let’s Encrypt設定)

AWS13 AWS

AWS上のEC2をSSL化しWordPress構築をゴールとする記事。今回はLet’s EncryptでSSL化に挑戦します。

この記事の趣旨

自分が分からなかったこと・知らなかったことをググり倒した結果を備忘録として残すことが目的で、初学者の方に教え伝えるものではありません(きっぱり)。

誤認と思われる箇所は教えて頂ければうれしいです。

ここまでの流れ

EC2構築〜Route53の設定あたりまではこちらをご覧ください。

AWSにWordPressを構築する 1(EC2構築 Route53設定)
今回はAWSに仮想マシンを作成し、独自ドメイン設定→SSL証明書(Let's Encrypt)を設定してWordPress構築を目指します。ドメインは取得済みの前提です。この記事の趣旨自分が分からなかったこと・知らなかった...

epelリポジトリ追加

Amazon Linuxへepelリポジトリを追加します。

[ec2-user@ip-172-31-11-12 ~]$ sudo amazon-linux-extras install epel
Installing epel-release
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-epel
12 metadata files removed
4 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                               

# 以下省略                                                                                                                

Certbotクライアントのインストール

Let’s Encryptの証明書は90日間しかありません。この更新処理を自動で行ってくれるのがCertbotです。

[ec2-user@ip-172-31-11-12 ~]$ sudo yum install certbot python-certbot-apache
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
219 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check

# 以下省略

証明書作成

Let’s Encryptの証明書を作成します。その前に/etc/httpd/conf/httpd.confのDocumentRootを下記のように変更し(sandambaraの箇所はお好みのディレクトリで)、.confで指定したディレクトリを作成します。

DocumentRoot "/var/www/html/sandambara"

Ctrl + x → y → Enterキーで保存して終了したら

sudo mkdir /var/www/html/sandambara

【注意】セキュリティグループでhttp・httpsが通るようにしておきましょう。

証明書発行コマンドの書式はこうです。

certbot certonly –webroot -w DocumentRootのディレクトリ -d SSLにしたいドメイン

コマンド入力後は下記要領で進みます。

  • 連絡先メールアドレス
  • 規約同意 → y
  • メールのお知らせ → お好みに応じてお好きな方を(y or n)

これが実際の流れです。

[ec2-user@ip-172-31-11-12 ~]$ sudo certbot certonly --webroot -w /var/www/html/sandambara -d sandambara.work
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): 連絡先メールアドレス

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
404 Page not found - Let's Encrypt
Let's Encrypt is a free, automated, and open certificate authority brought to you by the nonprofit Internet Security Research Group (ISRG).
You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: n Account registered. Requesting a certificate for sandambara.work Performing the following challenges: http-01 challenge for sandambara.work Using the webroot path /var/www/html/sandambara for all unmatched domains. Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/sandambara.work/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/sandambara.work/privkey.pem Your certificate will expire on 2021-08-20. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le [ec2-user@ip-172-31-11-12 ~]$

IMPORTANT NOTES:に続いて「Congratulations!」とあれば成功です。証明書が保存されているディレクトリは「/etc/letsencrypt/live/SSLにしたドメイン/」です。

[ec2-user@ip-172-31-11-12 ~]$ sudo ls -l /etc/letsencrypt/live/sandambara.work/
total 4
lrwxrwxrwx 1 root root  39 May 22 12:40 cert.pem -> ../../archive/sandambara.work/cert1.pem
lrwxrwxrwx 1 root root  40 May 22 12:40 chain.pem -> ../../archive/sandambara.work/chain1.pem
lrwxrwxrwx 1 root root  44 May 22 12:40 fullchain.pem -> ../../archive/sandambara.work/fullchain1.pem
lrwxrwxrwx 1 root root  42 May 22 12:40 privkey.pem -> ../../archive/sandambara.work/privkey1.pem
-rw-r--r-- 1 root root 692 May 22 12:40 README

ssl.conf作成

先ほど確認したディレクトリを/etc/httpd/conf.d/ssl.confへ書き込みます。

SLCertificateFile /etc/letsencrypt/live/sandambara.work/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/sandambara.work/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/sandambara.work/chain.pem     

私の環境ではSSLCertificateChainFileだけデフォでコメントアウトされていました。

httpd再起動

sudo systemctl restart httpd
sudo firewall-cmd --add-service=https --zone=public --permanent
sudo systemctl restart firewalld
sudo firewall-cmd --list-all          ←確認

index.html作成

index.htmlを用意します。

sudo nano /var/www/html/sandambara/index.html

では、https://SSL化したドメインにアクセスしてみましょう!

コメント