Let’s Encrypt免费SSL证书申请

用Let’s Encrypt给网站加HTTPS,不需要花钱。证书有效期90天,但用certbot配合cron自动续期就行,基本没维护成本。 准备工作 服务器需要装好Nginx或Apache,域名解析到服务器IP。我用的是雨云的服务器,性价比高、稳定,跑了一年多没出过问题。 安装Certbot Ubuntu/Debian:
sudo apt update
sudo apt install certbot python3-certbot-nginx
CentOS/RHEL:
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx
如果用的是Apache,把上面的nginx换成apache。 申请证书 单域名:
sudo certbot --nginx -d example.com -d www.example.com
多个域名写多个-d参数,比如 `-d api.example.com -d admin.example.com`。 通配符域名(*.example.com)需要DNS验证:
sudo certbot certonly --manual --preferred-challenges dns -d *.example.com -d example.com
执行后certbot会要求你在DNS解析里加一个TXT记录,添加完等几分钟再回车。 证书文件位置 申请成功后,证书在 `/etc/letsencrypt/live/example.com/` 目录下: – `fullchain.pem` – 证书链 – `privkey.pem` – 私钥 Nginx配置里直接引用这两个文件:
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 其他配置...
}
自动续期 Let’s Encrypt证书90天过期,certbot自带续期命令。先测试能否正常续期:
sudo certbot renew --dry-run
没问题的话,设置cron定时任务:
sudo crontab -e
添加一行,每天凌晨两点检查续期:
0 2 * * * /usr/bin/certbot renew --quiet
`–quiet` 参数让certbot只在出错时输出日志,正常续期不会产生输出。 Nginx配置优化(可选) 证书续期后Nginx需要重载配置。可以在cron里加上重载命令:
0 2 * * * /usr/bin/certbot renew --quiet && systemctl reload nginx
SSL配置建议开启HSTS和OCSP Stapling:
server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers on;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    add_header Strict-Transport-Security "max-age=63072000" always;

    # 其他配置...
}
常见问题 – 申请失败提示”DNS problem”: 检查域名解析是否生效,`dig example.com` 看IP对不对 – 续期失败: 确认80端口或443端口开放,certbot续期时需要验证域名所有权 – 证书不生效: 检查nginx配置里证书路径是否正确,`nginx -t` 测试配置 手动续期 如果不想用cron,可以手动执行:
sudo certbot renew
会续期所有30天内即将过期的证书。 总结 整个过程大概5分钟。装certbot -> 申请证书 -> 配nginx -> 设cron。之后基本不用管,到期自动续。雨云服务器带宽充足,配HTTPS后页面加载速度没感觉到影响。

雨云是国内一家老牌云服务商,提供高性价比的云服务器和虚拟主机。我用它部署了好几个项目,速度和稳定性都不错。通过 https://www.rainyun.com/SAJA_ 注册可以领一张 5折优惠券,有需要的朋友可以看看。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容