Let,s Encrypt
- 2022年5月7日
2022/02 の作業
前回ドメインの設定までしたので、Let's Encrypt でタダのTLSの証明書を設定。
証明書の更新を自動でやってくれる certbot を使って入れる。
certbot が各種プラットホーム等々への対応から 2020/01 位から yum では入らない。
snap を使って入れるらしいが、amazon-linux2 のレポジトリが x86_64 しかないので、Pythonをつかっているので、Python を使っていれる。
certbot のページ( https://certbot.eff.org/instructions )通りやれば入る。
1. 入力欄の、「My HTTP website is running」 の次の入力欄(Software)で「Nginx」を「on」の後の入力欄(System)で「pip」を選ぶ。
「Nginx on pip」が表示される。
今回は1つの web サーバにだけ設定すればいいので、「default」のタブ通り進めていく。
2. Python は入っているから、Amazon2 Linux は Centos7 相当なんで、Python 以外のパッケージを入れる。
$ sudo yum install augeas-libs.aarch64
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd ・・・・ Complete!
3.既に入っているものがあったら消しといて的な説明があるので、入って無いとおもうけど、消しておく。
$ sudo yum remove certbot
4.certbot 用の venv を作成して、pip をアップデートする。( supervisor もこうすればよかったのかも )
$ sudo python3 -m venv /opt/certbot/ $ sudo /opt/certbot/bin/pip3 install --upgrade pip
・・・・ Successfully installed pip-22.0.3
5.certbot を入れる。nginx用のものも一緒に。
$ sudo /opt/certbot/bin/pip3 install certbot certbot-nginx
・・・・ Successfully installed ConfigArgParse-1.5.3 PyOpenSSL-22.0.0 acme-1.22.0 certbot-1.22.0 certbot-nginx-1.22.0 certifi-2021.10.8 cffi-1.15.0 charset-normalizer-2.0.11 configobj-5.0.6 cryptography-36.0.1 distro-1.6.0 idna-3.3 josepy-1.12.0 parsedatetime-2.6 pycparser-2.21 pyparsing-3.0.7 pyrfc3339-1.1 pytz-2021.3 requests-2.27.1 requests-toolbelt-0.9.1 six-1.16.0 urllib3-1.26.8 zope.component-5.0.1 zope.event-4.5.0 zope.hookable-5.1.0 zope.interface-5.4.0
6.certbot コマンドを使える様に、sudo のパスが通ってる /usr/bin にリンクを張っておく。
$ sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
7.certbot コマンドを叩いて、NGINX に証明書を入れる。
問い合わせ形式で何回かやり取りする。
証明書の候補のドメインが表示されるてこれでいいかみたいなのがある。
NGINX の server_name から持ってきてる?
成功すれば、証明書のパス等が表示される。
EC2 のインバウンドルールで、80 と 443 を開けておかないと以下のようにエラーになる。
$ sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log Enter email address (used for urgent renewal and security notices) (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. 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:
Y
Account registered. Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: w1.massolezume.ga - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel):
エンター
Requesting a certificate for w1.massolezume.ga Certbot failed to authenticate some domains (authenticator: nginx). The Certificate Authority reported these problems: Domain: w1.massolezume.ga Type: connection Detail: Fetching http://w1.massolezume.ga/.well-known/acme-challenge/majg2o5ww308xosGqq9yseMJtyAdwrenQqUE1Pk5X1Y: Timeout during connect (likely firewall problem) Hint: The Certificate Authority failed to verify the temporary nginx configuration changes made by Certbot. Ensure the listed domains point to this nginx server and that it is accessible from the internet. Some challenges have failed. Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.
こうなるんで、インバウンドルールを設定して、再度実行
$ sudo certbot --nginx Saving debug log to /var/log/letsencrypt/letsencrypt.log Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: w1.massolezume.ga - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): エンター Requesting a certificate for w1.massolezume.ga Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/w1.massolezume.ga/fullchain.pem Key is saved at: /etc/letsencrypt/live/w1.massolezume.ga/privkey.pem This certificate expires on 2022-05-09. These files will be updated when the certificate renews. Deploying certificate Successfully deployed certificate for w1.massolezume.ga to /etc/nginx/sites-enabled/worksite_nginx.conf Congratulations! You have successfully enabled HTTPS on https://w1.massolezume.ga NEXT STEPS: - The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
成功した様だ。
8.上記のコマンドが成功している、NGINX の設定ファイル ( /etc/nginx/sites-enabled/worksite_nginx.conf ) にSSLの設定が入る。
listen 80 が消えて SSL 用の設定が入っている。
そして、80 のアクセスを 443 にリダイレクトする設定が入っている。
$ sudo vi /etc/nginx/sites-enabled/worksite_nginx.conf
・・・・
# configuration of the server server { ・・・・ listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/www.massolezume.ga/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/www.massolezume.ga/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } ・・・・ server { if ($host = w1.massolezume.ga) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name w1.massolezume.ga; return 404; # managed by Certbot }
9.うまく行っていれば、https でアクセスできる。

10.証明書の更新の cron のサンプルも書いてあるので、実行して登録しておく。
$ echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab
更新コマンドがちゃんと動いているか確認するコマンド。
$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/w1.massolezume.ga.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Account registered. Simulating renewal of an existing certificate for w1.massolezume.ga - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/w1.massolezume.ga/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
月1位で certbot を更新した方がいいらしいので、気が向いたら、
$ sudo /opt/certbot/bin/pip install --upgrade certbot certbot-nginx
もし、更新してエラーになったら、
$ sudo rm -rf /opt/certbot
で消して、certbot を再インストールらしい。