Ubuntu 20.04 Let’s Encryptを使用してNginxでhttps接続(Certbot使用)

Ubuntu 20.04にインストールしたnginx。
Let’s Encryptで証明書を発行し、https接続できるよう構成します。

こちらを参考に実施しました。
Ubuntu 20.04でLet’s Encryptを使用してNginxを保護する方法



Certbotのインストール



証明書の発行や自動更新を行ってくれる便利なツールがあるので利用します。
https://certbot.eff.org/


$ sudo apt install certbot python3-certbot-nginx



nginxの設定ファイルを編集。

$ sudo vi /etc/nginx/sites-available/default



server_nameにsslを発行するドメイン名を指定します。

# server_name _;
server_name www.example.com;



設定が正しく記載されているかテスト。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful



テストを通過するのを確認してnginxサービスをリロードします。

$ sudo service nginx restart






SSL証明書の取得




$ sudo certbot --nginx -d [ドメイン名]


を実行して、SSL証明書の犯行を実行します。


$ sudo certbot --nginx -d www.example.com



メールアドレスの入力や、各種規約の確認を行います。

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): [メールアドレスを入力]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
Future versions of Certbot will automatically configure the webserver so that all requests redirect to secure HTTPS access. You can control this behavior and disable this warning with the --redirect and --no-redirect flags.

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

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.example.com/privkey.pem
Your cert will expire on 2021-09-11. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. 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





設定ファイルはcertbotが自動で更新してくれます。
これでhttps接続できるようになりました。お手軽。


Certbotの自動更新の検証



証明書の自動更新も設定されているようです。
certbot.timerサービスを確認します。

$ sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset:>
     Active: active (waiting) since Sun 2021-06-13 18:48:30 JST; 14min ago
    Trigger: Mon 2021-06-14 11:01:55 JST; 15h left
Triggers: ● certbot.service

6月 13 18:48:30 ik1-417-40711 systemd[1]: Started Run certbot twice daily.



起動していますね。


dry-runで更新処理をテストしておきます。

$ sudo certbot renew --dry-run
(略)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/www.example.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**         (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- 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.




「Congratulations, all renewals succeeded.」と表示されれば問題ないようです。
初めてCertBotでSSL設定してみましたが、こんなに便利とは。驚きました。


【参考URL】
Ubuntu 20.04でLet’s Encryptを使用してNginxを保護する方法
関連記事

コメント

プロフィール

Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
https://symfo.web.fc2.com/

PR

検索フォーム

月別アーカイブ