LoginSignup
51
40

More than 1 year has passed since last update.

AWS EC2(Amazon Linux 2 × nginx) に SSL を設定する

Last updated at Posted at 2020-09-19

やりたいこと

ドメイン設定した、EC2にSSLを設定したい
Let's Encrypt で行います

前提

前回→(ドメインを設定してhttpアクセスはできる状態)
AWS Route53 親ドメインを移行しないでEC2にサブドメイン設定
前々回
Laravel 環境構築 AWS EC2(nginx + PHP) + RDS

インスタンスが以下の TCP ポートで接続を受け付けるようにセキュリティグループを設定済み。
HTTPS (ポート 443)

インストールの準備

EPEL 7 パッケージをダウンロードします。
これは、Certbot(Let's Encryptを自動的に使用してHTTPSを有効にするツール) が必要とする依存関係を提供するために必要です。

  • ホームディレクトリに移動
cd ~
  • EPEL をダウンロード
sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
  • EPEL インストール
sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
  • EPEL を有効化
sudo yum-config-manager --enable epel*
  • EPEL 有効化確認
sudo yum repolist all

# 出力 
...
epel/x86_64                          Extra Packages for Enterprise Linux 7 - x86_64                               enabled: 12949+175
epel-debuginfo/x86_64                Extra Packages for Enterprise Linux 7 - x86_64 - Debug                       enabled:      2890
epel-source/x86_64                   Extra Packages for Enterprise Linux 7 - x86_64 - Source                      enabled:         0
epel-testing/x86_64                  Extra Packages for Enterprise Linux 7 - Testing - x86_64                     enabled:    778+12
epel-testing-debuginfo/x86_64        Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug             enabled:       107
epel-testing-source/x86_64           Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Source            enabled:         0
...

Certbot のインストールと実行

Certbot(Let's Encryptを自動的に使用してHTTPSを有効にするツール) のインストール

sudo yum install certbot-nginx

Certbotの実行

sudo certbot --nginx

質問に選択で答えていく

1つ目
メアドの入力
セキュリティになんか問題あったときとか通知いく

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): メアド

2つ目
利用規約同意するか聞かれるので
a入力

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a

メアドの共有をしてよいかの確認


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

どのドメインをhttpsするかの選択
nginxのconfにserver_name指定してるので、自動でやってくれます


Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for twitter-admin.collapse-natsu.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/nginx.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/nginx.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

httpsでサイトにアクセスしてできたらok

自動更新設定

cronに書き込み

sudo crontab -e

以下を追記

39      1,13    *       *       *       root    certbot renew --no-self-upgrade && systemctl restart nginx

毎日、01:39 と 13:39 にコマンドが実行されるようにスケジュールします。ここで選択した値は任意ですが、Certbot 開発者は、コマンドを少なくとも毎日 2 回実行することを推奨しています。これにより、侵害されていることがわかった証明書は必ずすぐに取り消されて置き換えられます。

再起動して反映

sudo systemctl restart crond
51
40
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
51
40