阿里云,腾讯云等服务商的上游CA公司把SSL免费证书改为3个月有效后,对开发者而言,维护HTTPS证书变成了一件很麻烦的事情,本文介绍通过Certbot申请证书,并自动续期,相当于一次配置,永久生效,本文通过
ChatGPT 4.0
AI自动生成;
本文涉及的内容:
为什么免费的CA证书的有效期改为了90天?
在 Rocky Linux 9 中使用 certbot
为 Nginx 生成 SSL 证书的步骤如下:
首先,确保你已经启用了 EPEL,并安装了 Certbot 及其 Nginx 插件。
sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y
确保你有一个正在运行并配置良好的 Nginx 服务器。例如:
# /etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/html;
index index.html;
}
//注意第三步会用到这里的root /var/www/html路径;
}
sudo certbot certonly --webroot -w /var/www/html -d nosub.net -d www.nosub.net
说明一下:因为我的nginx是手动编译的,并不适合certbot --nginx;
这个命令会为 example.com 和 www.example.com 这两个域名生成一个证书,使用 --webroot 模式会在 /var/www/html 中创建 .well-known 文件夹,这个文件夹里面包含了一些验证文件,certbot 会通过访问 example.com/.well-known/acme-challenge 来验证你的域名是否绑定的这个服务器。这个命令在大多数情况下都可以满足需求,
但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 --webroot 就走不通了。certbot 还有另外一种模式 --standalone , 这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。
certbot certonly --standalone -d example.com -d www.example.com
我们忽略--standalone的方式,因为此方法有弊端,需要 占用端口号
;
操作成功之后:
Hint: The Certificate Authority failed to download the temporary challenge files created by Certbot. Ensure that the listed domains serve their content from the provided --webroot-path/-w and that files created there can be downloaded 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.
[root@iZwz90jozfhvw71ymemnf6Z ~]# sudo certbot certonly --webroot -w /mnt/nosub/dist/NosubWeb -d nosub.net -d nosub.net
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for nosub.net
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/nosub.net/fullchain.pem
Key is saved at: /etc/letsencrypt/live/nosub.net/privkey.pem
This certificate expires on 2024-10-15.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background
发现SSL证书已经生成保存在
Certificate is saved at: /etc/letsencrypt/live/nosub.net/fullchain.pem
Key is saved at: /etc/letsencrypt/live/nosub.net/privkey.pem
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name nosub.net;
#改为自动更新证书;
#ssl_certificate ssl/nosub.net.pem;
#ssl_certificate_key ssl/nosub.net.key;
ssl_certificate /etc/letsencrypt/live/nosub.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nosub.net/privkey.pem;
}
//其他配置省略;
systemctl reload nginx
重新加载配置一次,测试是否已经成功;
使用 certbot
生成证书后,最好设置定期自动更新并在证书更新后重启 Nginx 服务器。下面是详细的步骤:
certbot
提供了 renew
命令,可以用来定期检查和更新即将到期的证书。一般情况下,你可以通过 cron
来定期运行此命令。
cron
任务打开 cron
编辑器:
sudo crontab -e
添加以下任务,每天凌晨3点运行 certbot renew
并在需要时重启 Nginx:
0 3 * * * /usr/bin/certbot renew --quiet --renew-hook "systemctl reload nginx"
0 3 * * *
: 每天凌晨3点运行/usr/bin/certbot
: certbot
的路径,根据实际情况调整renew --quiet
: 尝试更新所有快到期的证书,--quiet
禁用输出--renew-hook "systemctl reload nginx"
: 在成功更新证书后,重新加载 Nginx检测certbot的定时器是否已经生效:
可以使用以下命令查看当前用户的 cron 任务:
crontab -l
可以通过系统命令查看当前定时器:
systemctl list-timers: 查看激活的timer
systemctl list-timers -all: 查看所有的timer
systemctl start certbot-renew.timer: 开启timer
如果使用systemctl list-timers没有看到certbot的定时器,
就使用systemctl list-timers -all看有没有,
如果有就使用systemctl start certbot-renew.timer开启,
没有就使用systemctl enable certbot-renew.timer
然后在使用systemctl start certbot-renew.timer
可以通过手动运行以下命令来测试自动更新是否能够正常工作:
sudo certbot renew --dry-run
#如果是手动续期:
sudo certbot renew
如果这条命令提示成功,则说明自动更新功能已正确配置。
certbot
的日志文件通常位于 /var/log/letsencrypt
目录中,你可以查看日志来验证证书的自动更新情况。
sudo cat /var/log/letsencrypt/letsencrypt.log
sudo certbot certificates
输出结果如下:
Found the following certs:
Certificate Name: nosub.net
Serial Number: 4c6111fc35ca8573cf52409b31f8be95cfa
Key Type: ECDSA
Domains: nosub.net
Expiry Date: 2024-10-15 05:22:53+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/nosub.net/fullchain.pem
Private Key Path: /etc/letsencrypt/live/nosub.net/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
泛域名证书因为要修改DNS的解析记录,须自行研究,本文忽略;
通过上述步骤,你可以确保证书能够定期自动更新,并在更新后自动重启或重新加载 Nginx 服务器,从而达到SSL证书一直有效的目的;