如何使用Let's Encrypt + Certbot提供完全免费的https证书
如何使用Let’s Encrypt + Certbot提供完全免费的https证书
背景
阿里云的SSL证书到期了,续费需要付费。经过研究发现Let’s Encrypt + Certbot可以提供完全免费且自动续期的SSL证书,完美替代付费方案。
阿里云SSL的缺点
- 付费续期:证书到期后需要支付续费费用
- 手动更新:需要手动下载证书并更新服务器配置
- 管理复杂:多个域名需要分别购买和管理
- 有效期短:通常1年有效期,频繁续费
Let’s Encrypt + Certbot的优点
- 完全免费:证书永久免费,无任何费用
- 自动续期:每3个月自动续期,无需人工干预
- 自动配置:一条命令自动配置Apache/Nginx
- 批量管理:可以同时管理多个域名
- 权威认证:被所有主流浏览器信任
实施步骤
1. 检查当前SSL配置状态
1 2 3 4 5
| sudo ls -la /etc/apache2/sites-enabled/ | grep ssl
sudo certbot certificates
|
2. 安装Certbot
1 2 3 4 5 6
| sudo apt update sudo apt install certbot python3-certbot-apache
sudo yum install certbot python3-certbot-apache
|
3. 从阿里云SSL切换到Let’s Encrypt
识别问题:通过curl -I https://yourdomain.com发现证书过期错误:
1
| curl: (60) SSL certificate problem: certificate has expired
|
检查配置文件:发现Apache使用的是过期的阿里云证书路径:
1 2 3
| SSLCertificateFile /etc/ssl/aliyun/domain_public.crt SSLCertificateKeyFile /etc/ssl/aliyun/domain.key SSLCertificateChainFile /etc/ssl/aliyun/domain_chain.crt
|
4. 批量申请Let’s Encrypt证书
1 2 3 4 5 6 7 8
| sudo certbot --apache -d yourdomain.com
sudo certbot --apache -d blog.domain1.com -d api.domain2.com -d www.domain3.com
sudo certbot --apache -d yourdomain.com --force-renewal
|
选择重定向选项:
- 选择
2: Redirect 强制HTTPS访问
- 自动添加HTTP到HTTPS的重定向规则
5. 配置自动续期
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo systemctl status certbot.timer sudo systemctl list-timers | grep certbot
sudo systemctl enable certbot.timer sudo systemctl start certbot.timer
sudo certbot renew --dry-run
sudo crontab -e
|
配置验证
检查证书状态
1 2 3 4 5 6 7 8
| sudo certbot certificates
curl -I https://yourdomain.com
curl -I http://yourdomain.com
|
预期结果
1 2 3
| HTTP/1.1 200 OK Server: Apache/2.4.41 Strict-Transport-Security: max-age=31536000
|
这样就能拥有一个非常不错的完全免费且自动续期的SSL证书