Let’s Encrypt是目前最主流的免费SSL证书提供商,证书有效期90天,支持自动续期。我用它给雨云服务器上的站点配过几十次证书,没出过问题。
申请工具选certbot,acme.sh也行,但certbot官方支持最好,文档全。
环境准备
服务器需要装好nginx或apache,确保80和443端口能访问。Let’s Encrypt验证域名所有权时会通过80端口做HTTP验证,或者通过DNS解析验证。我习惯用HTTP验证,快,不需要动DNS。
安装certbot:
# Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx
# CentOS/RHEL 8+
sudo dnf install certbot python3-certbot-nginx
申请证书
假设域名为 example.com,nginx已配置好站点。运行:
sudo certbot --nginx -d example.com -d www.example.com
certbot会自动检测nginx配置,修改server block加入SSL相关指令。如果不想让certbot改nginx配置,只拿证书:
sudo certbot certonly --nginx -d example.com -d www.example.com
证书文件会放在 /etc/letsencrypt/live/example.com/ 下,包含:
– fullchain.pem (证书链)
– privkey.pem (私钥)
nginx配置里指向这两个文件就行。
手动配置nginx
如果不想用certbot自动配置,自己写SSL配置:
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;
# 推荐的安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:3000; # 你的后端服务
}
}
# HTTP重定向到HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
配置完重载nginx:
sudo nginx -t
sudo systemctl reload nginx
自动续期
Let’s Encrypt证书只有90天,必须自动续。certbot默认加了systemd定时器,检查一下:
sudo systemctl status certbot.timer
如果没启用,手动开:
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
也可以自己写cron:
0 3 * * * /usr/bin/certbot renew --quiet
renew命令会自动检测距离过期不足30天的证书并续期。续期后nginx需要重载证书,certbot的nginx插件会自动做,如果用certonly方式拿的证书,需要在renew钩子里重载nginx:
sudo certbot renew --post-hook "systemctl reload nginx"
常见问题
申请失败大多因为80端口不通。检查服务器防火墙,雨云的控制面板安全组里要放行80和443端口。另外域名解析必须指向服务器IP,Let’s Encrypt验证时会请求 http://example.com/.well-known/acme-challenge/ 下的文件。
如果必须用DNS验证(比如服务器没有公网IP),用certbot的DNS插件:
sudo certbot certonly --manual --preferred-challenges dns -d example.com
按提示去DNS服务商加TXT记录。这种方式适合内网或反向代理场景,但每次续期都要手动加记录,不推荐。建议用acme.sh配合DNS API自动续。
多域名通配符
Let’s Encrypt支持通配符证书 *.example.com,但必须DNS验证。用certbot配合DNS插件:
sudo certbot certonly --manual --preferred-challenges dns -d *.example.com -d example.com
同样需要手动加TXT记录。自动化的话用acme.sh,支持几十家DNS服务商的API。
总结
certbot + nginx是最省心的方案,一条命令搞定申请和配置。定时器确保自动续期,基本不用管。雨云服务器上我这样跑了两年多,没出过证书过期的问题。唯一要注意的是服务器时间和时区要正确,否则续期可能因为时间偏差失败。
雨云是国内一家老牌云服务商,提供高性价比的云服务器和虚拟主机。我用它部署了好几个项目,速度和稳定性都不错。通过 https://www.rainyun.com/SAJA_ 注册可以领一张 5折优惠券,有需要的朋友可以看看。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



暂无评论内容