$ cat contents/tech/106.md
Amazon Linux 2 + Nginx で Let’s Encryptを使う

Let’s Encryptでは無料でSSL証明書が利用できます。企業で利用する際に採用する実在の証明などは利用出来ませんが、ドメインの証明は可能なため、個人のサイトとしては十分かと思います。
更新作業も完全自動化に対応しており、とても便利なので実際に行なった手順を元に導入方法を紹介します。
作業の前にEBSのスナップショットかAMIを取っておきます。
前提作業
例として本サイトのアドレスtroches.jpでの設定で記載します。
nginxの設定
troches.jp, www.troches.jpどちらからもアクセス出来るようにnginxを設定しておきます。
...
server {
...
server_name troches.jp www.troches.jp;
...
RHEL7のリポジトリをインストール&有効化
Redhat Enterprise Linux 7 のリポジトリインストールパッケージを取得&インストール
$ sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
リポジトリをインストール
$ sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
リポジトリを有効化
$ sudo yum-config-manager --enable epel*
Certbot のインストールと実行
インストール
$ sudo yum install certbot-nginx
certbotを実行
$ sudo certbot --nginx
以下のように入力
Enter email address....
(Enter 'c' to cancel): // Emailを入力 //
-----------------------------------------------
Please read the Terms of Service at
...
(A)gree/(C)ancel: A
-----------------------------------------------
Would you be willing to share your email
...
(Y)es/(N)o: Y
-----------------------------------------------
Starting new HTTPS connection
...
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):そのままEnter
-----------------------------------------------
Please choose whether or not to redirect HTTP traffic to HTTPS,
...
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
-----------------------------------------------
Congratulations!
と出ればよいでしょう。
nginxのコンフィグにリダイレクトの設定など追加されているはず。
以下のようにエラーになったらこちらを参照
Cleaning up challenges
An unexpected error occurred:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 15: ordinal not in range(128)
Please see the logfiles in /var/log/letsencrypt for more details.
Nginxのコンフィグをチェック&再起動
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl restart nginx
セキュリティグループを変更する
443を開放する
URLにアクセスして確認します。
http://troches.jp/, http://www.troches.jp/にアクセスした時にhttps://troches.jpへリダイレクトされているか、証明書の詳細などを確認します。
証明書を自動で更新されるように設定する
cronかsystemdで自動実行されるように設定する。
以下、systemdでの設定
サービスファイル
[Unit]
Description=Let's Encrypt renewal
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --agree-tos --deploy-hook "systemctl reload nginx.service"
タイマーファイル
[Unit]
Description=Daily renewal of Let's Encrypt's certificates
[Timer]
OnCalendar=0/12:00:00
RandomizedDelaySec=1h
Persistent=true
[Install]
WantedBy=timers.target
設定ファイルをリロードする
$ sudo systemctl daemon-reload
手動で実行する
$ sudo systemctl start certbot.service
ログを確認しましょう
$ sudo less /var/log/letsencrypt/letsencrypt.log
タイマーを有効化する
$ sudo systemctl enable certbot.timer
参考
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/SSL-on-an-instance.html#letsencrypt
https://certbot.eff.org/lets-encrypt/centosrhel7-nginx