SSL证书过期续期的操作
网站HTTPS访问出现证书过期警告,需要立即续期Let's Encrypt SSL证书。
一、检查证书状态
1.1 确认Nginx配置中的证书路径
root@iZ2vc71tkyouggqlc0484kZ:~# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
root@iZ2vc71tkyouggqlc0484kZ:~# cat /etc/nginx/sites-available/zjszkj
配置中显示证书位于:
ssl_certificate /etc/letsencrypt/live/zzhanzhang.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zzhanzhang.com/privkey.pem;
1.2 查看证书有效期
openssl x509 -in /etc/letsencrypt/live/zzhanzhang.com/fullchain.pem -noout -dates
输出:
notBefore=Mar 21 02:32:12 2024 GMT
notAfter=Jun 19 02:32:11 2024 GMT
当前是6月22日,证书显然已过期。如果没过期可以进一步计算剩余天数:
openssl x509 -in /etc/letsencrypt/live/zzhanzhang.com/fullchain.pem -noout -enddate | cut -d= -f2 | xargs -I {} sh -c 'date -d "{}" +%s' | xargs -I {} sh -c 'echo $((({} - $(date +%s)) / 86400))'
输出:-2(负值表示已过期2天)
也可以用 -checkend 0 确认:
openssl x509 -in /etc/letsencrypt/live/zzhanzhang.com/fullchain.pem -noout -checkend 0
输出:Certificate will expire
结论:确认证书已过期,需要立即续期。
二、尝试自动续期
执行强制续期:
certbot renew --force-renewal
续期过程处理了多个域名,结果如下:
- ecg.zzhanzhang.com:✅ 成功
- bk.zzhanzhang.com:❌ 失败 - Could not bind TCP port 80 because it is already in use
- zzhanzhang.com:❌ 失败 - 同样原因(80端口被占用)
失败原因分析
- 80端口被占用:Certbot默认使用nginx插件,需要临时占用80端口进行验证,但Nginx已在运行,导致冲突。
针对zzhanzhang.com,决定采用webroot方式绕过端口绑定问题。
三、使用webroot方式续期成功
webroot方式将验证文件写入网站根目录,由Nginx直接提供,无需额外监听端口。
certbot certonly --webroot -w /www/website -d zzhanzhang.com -d www.zzhanzhang.com --force-renewal
输出:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/zzhanzhang.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/zzhanzhang.com/privkey.pem
This certificate expires on 2024-09-20.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
新证书有效期至2024-09-20,问题解决。
四、重载Nginx使新证书生效
nginx -t && systemctl reload nginx
输出了一些警告(ssl_stapling ignored),但不影响加密功能,可以忽略:
nginx: [warn] "ssl_stapling" ignored, no OCSP responder URL ...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重载成功,网站HTTPS恢复访问。
五、检查自动续期定时器
确保未来证书能自动续期:
systemctl list-timers | grep certbot
输出:
Mon 2024-06-22 21:41:43 CST 12h left Mon 2024-06-22 02:36:59 CST 6h ago certbot.timer certbot.service
查看定时器实际执行的命令:
systemctl cat certbot.service
输出:
ExecStart=/usr/bin/certbot -q renew
默认使用配置文件中的认证方式,而在 /etc/letsencrypt/renewal/zzhanzhang.com.conf 中已经记录了 authenticator = webroot,因此下次自动续期将采用webroot,不会再出现端口冲突。