Ubuntu自动更新ssl证书
Table of Contents
使用 Let's Encrypt 的 Certbot 来自动更新 Ubuntu 系统下的 Nginx SSL 证书。
1. 安装 Snapd :
通过 Snapd 来安装 Certbot ,因为 Snapd 能保证你获取到最新的版本。如果你的系统上没有 Snapd ,你需要先安装它。
sudo apt update sudo apt install snapd sudo snap install core; sudo snap refresh core
2. 安装 Certbot :
使用snap来安装Certbot:
sudo snap install --classic certbot
3. 准备 Certbot 运行:
确保Certbot命令可以通过一个简单的符号链接运行:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
4. 获取并安装 SSL 证书:
用Certbot为Nginx获取并安装一个SSL证书。记得将 yangk.net
替换成你实际的域名,并且确保在DNS中这个域名是指向你的服务器IP的。
sudo certbot --nginx -d yangk.net -d www.yangk.net
4.1 下载的 SSL 地址
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/yangk.net/fullchain.pem
Key is saved at: /etc/letsencrypt/live/yangk.net/privkey.pem
This certificate expires on 2024-08-04.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
4.2 替换nginx配置的SSL地址
ssl_certificate /etc/letsencrypt/live/yangk.net/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yangk.net/privkey.pem;
5. 自动续期:
Certbot Snap版本会自动设置一个系统任务(Timer),这个Timer会两次一天来运行续期操作。你不需要手动创建cron job。你可以通过以下命令检查Timer的激活状态:
sudo systemctl list-timers | grep certbot
5.1 返回结果
Mon 2024-05-06 23:09:00 CST 11h left n/a n/a snap.certbot.renew.timer snap.certbot.renew.service
6. 测试自动续签:
sudo certbot renew --dry-run
6.1 返回结果
$ sudo certbot renew --dry-run Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/yangk.net.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Account registered. Simulating renewal of an existing certificate for yangk.net - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/yangk.net/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
如果在测试中没有遇到错误,说明自动续签已正确设置。