https://www.kalasearch.cn/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04/
现在越来越简单了,简要列下步骤:
- 安装Certbot
apt install certbot python3-certbot-nginx
两个都要装!
- 确认nginx配置中有域名
server_name example.com www.example.com;
- 生成证书
certbot --nginx -d example.com -d www.example.com
手动更新证书
certbot --nginx certonly -d example.com -d www.example.com
自动更新证书
https://www.mingdao.me/Nginx/nginx-add-ssl/
sudo certbot renew --dry-run
看到**Congratulations, all renewals succeeded. ……**的字样说明自动更新成功。
这个命令会产生一个定时任务,用来自动更新证书。可能位于下面的位置:
/etc/crontab/
/etc/cron.*/*
systemctl list-timers
证书自动更新后重启Nginx服务
到 /etc/letsencrypt/renewal-hooks/post目录下,创建restart_nginx.sh文件,在有证书更新时重启服务。文件内容如下:
systemctl restart nginx
再次用sudo certbot renew --dry-run
测试验证一下,可以看到nginx被重启的提示消息。