在Ubuntu中为Nginx部署自动续期的Let's Encrypt服务

首先需要安装好nginx,这里直接略过,不知道怎么安装的点击这里,查看nginx的安装那一段。

相关内容:

安装certbot服务

使用 Let's Encrypt 获取 SSL 证书的第一步是在服务器上安装 Certbot,我们是nginx环境所以需要注意后面的版本,如果是apche则是python3-certbot-apache

sudo apt install certbot python3-certbot-nginx

配置防火墙

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

为域名获取证书

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

注意这里的nginx参数

 sudo certbot --nginx -d app.towait.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): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 app.towait.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/app.towait.com

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): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/app.towait.com

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

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

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/app.towait.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/app.towait.com/privkey.pem
   Your cert will expire on 2022-10-09. 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

这里的信息跟着提示来就行,这时候应该可以使用https来访问我们的网站了,同时还提供了https在线检测的方法

https://www.ssllabs.com/ssltest/analyze.html?d=yourdomain.com

为Let's Encrypt设置自动续期

由于Let's Encrypt的证书有效期只有90天,到期后使用certbot renew命令来更新证书,我们只需要把该命令做成任务定期执行即可

sudo systemctl status certbot.timer

可以看到以下信息,每天执行两次

sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Mon 2022-07-11 16:27:47 UTC; 14min ago
    Trigger: Tue 2022-07-12 02:31:05 UTC; 9h left
   Triggers: ● certbot.service

Jul 11 16:27:47 ubuntu-s-1vcpu-1gb-intel-fra1-01 systemd[1]: Started Run certbot twice daily.

最后测试任务运行情况

sudo certbot renew --dry-run

如果没有任何错误说明已经一切就绪,certbot会在到期前自动执行证书的续期动作。

Post Comment