Let's Encrypt+AmazonLinux2+ApacheでSSL化

# やりたいこと
WebページのSSL化をしたい(http→https)
無料のSSL証明書である、Let’s Encryptを使用します。

# 環境
- Amazonlinux2
- Apache(ver2.4.54)

# 設定手順
[1. SSL証明書の導入](#1-ssl証明書の導入)
[2. apacheの設定](#2-apacheの設定)
[3. SSL証明書の自動更新設定](#3-ssl証明書の自動更新設定)

## 1. SSL証明書の導入

 yumリポジトリの追加
```:
amazon-linux-extras install -y epel
```

certbotの導入
```:
yum install certbot
```

SSL証明書の発行
※2つ以上のドメインを指定して証明書を発行する場合は※のコマンドを実行します。
```:
certbot certonly --webroot -w /var/www/html/ -d example.com
※certbot certonly --webroot -w /var/www/html/ -d example.com -w /var/www/html/hogehoge -d hogehoge.com

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): miguchi@example.jp (メールアドレスの登録)
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org

 (Enter 'c' to cancel): 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 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
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2022-03-19. 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" 
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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
```
Congratulations! が表示されれば、完了です！

証明書の確認
```:
# ll /etc/letsencrypt/live
total 4
drwxr-xr-x 2 root root  93 Sep 12 18:10 example.com
-rw-r--r-- 1 root root 740 Sep 11 17:07 README

# ll /etc/letsencrypt/live/example.com
total 4
lrwxrwxrwx 1 root root  41 Sep 12 18:10 cert.pem -> ../../archive/example.com/cert1.pem
lrwxrwxrwx 1 root root  42 Sep 12 18:10 chain.pem -> ../../archive/example.com/chain1.pem
lrwxrwxrwx 1 root root  46 Sep 12 18:10 fullchain.pem -> ../../archive/example.com/fullchain1.pem
lrwxrwxrwx 1 root root  44 Sep 12 18:10 privkey.pem -> ../../archive/example.com/privkey1.pem
-rw-r--r-- 1 root root 692 Sep 12 18:10 README
```


## 2. Apacheの設定

Apacheの設定ファイルに移動
```:
cd /etc/httpd/conf
```

念のため、バックアップファイルを作成
```:
cp -p httpd.conf httpd.conf_yyyymmdd
```

ファイルの編集
```:
# vi httpd.conf

～編集前～
<VirtualHost *:80>
  DocumentRoot /var/www/html/
  ServerName example.com
</VirtualHost>
↓
～編集後～
<VirtualHost *:443>
  DocumentRoot /var/www/html/
  ServerName example.com
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

80ポート→443ポートのリダイレクト設定をする場合は、下記を追記
<VirtualHost *:80> 
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualhost>
```

mod_sslのインストール
(これをしないとApacheでSSLが有効にならない)
```
yum install mod_ssl
```

apacheの再起動
```
systemctl restart httpd
```

## 3. SSL証明書の自動更新設定
Let’s EncryptのSSL証明書は有効期限が90日であるため、定期的に更新を行う必要があります。手動でも更新は可能ですが、自動更新設定をしておくと楽です。

まずは、証明書更新のdry-run（リハーサル）を行い、エラーの有無を確認します。
※dry-runでは実際に証明書は更新されません。
```
certbot renew --dry-run
```
```
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Simulating renewal of an existing certificate for example.com
Performing the following challenges:
http-01 challenge for example.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/example.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
```
Congratulationsが表示されればdry-runは成功です！
続いて、cronで自動更新設定を行います。
```
vi /etc/crontab
```
```
#毎月1日の深夜4時に更新を試みる場合、以下を追記
00 04 01 * * root certbot renew && systemctl restart httpd
```
こちらのコマンドの自動実行は毎月1日に行われますが、実際に更新されるのは有効期限が30日未満になってからとなります。
自動更新設定は以上で終了です。

※手動で行う場合は、--dry-runなしでコマンドを実行します。
有効期限が30日未満であれば成功しますが、そうでなければ更新はされません。
```
certbot renew
```
```
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/example.com/fullchain.pem expires on 2022-12-30 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
```
↑今回は30日未満ではなかったため、更新されませんでした。
強制的に手動更新を行いたい場合は、以下のコマンドを実行することで、更新できます。
```
certbot renew --force-renew
```
```
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate for example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/example.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all renewals succeeded:
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
```
↑成功しました。
手動更新後はapacheの再起動をお忘れなく。
```
systemctl restart httpd
```
# 参考記事
[Let’s EncryptのSSL証明書を更新する方法](https://hirooooo-lab.com/development/renew-lets-encrypt/)
[無料のSSL証明書Let’s Encryptを設定・更新・自動更新する方法(CentOS7, Apache2.4対応）](https://blog.proglus.jp/105/#SSL-3)