Let’s Encrypt是目前最主流的免费SSL证书提供商,证书有效期90天,支持自动续期。我用它给雨云服务器上的站点配过多次,流程固定,没什么坑。
前置条件
– 一个域名(如 example.com),DNS解析指向你的服务器IP
– 服务器已安装 Nginx 或 Apache(本文以 Nginx 为例)
– 端口 80 和 443 开放(Let’s Encrypt验证域名所有权需要)
安装 Certbot
Certbot 是 Let’s Encrypt 官方推荐的客户端工具,负责申请、安装、续期证书。
Ubuntu/Debian:
sudo apt update
sudo apt install certbot python3-certbot-nginx
CentOS/RHEL 7+:
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx
申请证书
假设你的 Nginx 配置已经指向了域名,运行:
sudo certbot --nginx -d example.com -d www.example.com
– `–nginx` 表示自动修改 Nginx 配置,添加 SSL 相关指令
– `-d` 指定域名,可以多个,第一个作为主域名
首次运行会要求输入邮箱(用于到期提醒),同意服务条款,选择是否将 HTTP 自动重定向到 HTTPS(选 2 启用)。
成功后你会看到类似输出:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Nginx 配置会被自动修改,加入 ssl_certificate 和 ssl_certificate_key 路径,并监听 443 端口。
手动申请(不用 Nginx 插件)
如果你的服务器不是标准 Web 服务器,或者想手动控制过程,用 standalone 模式:
sudo certbot certonly --standalone -d example.com
这会临时占用 80 端口做验证。申请前需要先停止 Nginx:
sudo systemctl stop nginx
sudo certbot certonly --standalone -d example.com
sudo systemctl start nginx
证书文件位置同上,手动配置 Nginx 的 SSL 部分。
配置 Nginx 使用证书
如果 certbot 没有自动改配置,或者你想自己写,在 server 块里加:
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://localhost:3000; # 你的应用
}
}
重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx
自动续期
Let’s Encrypt 证书只有 90 天,必须续期。Certbot 安装时会自动添加 systemd 定时器或 cron 任务。
检查定时器是否启用:
sudo systemctl list-timers | grep certbot
或者查看 cron:
sudo crontab -l
通常会有类似 `0 /12 certbot renew –quiet` 的行,每 12 小时检查一次,到期前 30 天内自动续期。
手动测试续期是否正常:
sudo certbot renew --dry-run
输出 “Congratulations, all renewals succeeded” 就代表没问题。
常见问题
1. 申请时报 “DNS problem: NXDOMAIN”:域名解析未生效或写错了,检查 DNS 记录。
2. 报 “Too many certificates already issued”:Let’s Encrypt 有速率限制,每周每个域名最多 50 个证书。测试环境用 staging 参数:
sudo certbot --staging -d example.com
3. 证书续期后 Nginx 没生效:certbot 默认会 reload 服务,但如果你的 Nginx 配置有语法错误 reload 会失败。手动 `sudo systemctl reload nginx` 看报错。
一点建议
我习惯在雨云服务器上跑多个站点,每个域名单独申请证书。雨云的机器性能稳定,网络延迟低,配合 certbot 自动续期,基本不用管证书过期的问题。如果预算有限,选个低配的雨云实例跑个人项目完全够用,每月几块钱,SSL 证书还免费,省心。
检查一下你的站点是否强制 HTTPS 了。可以访问 http://example.com 看是否自动跳转到 https://。如果没有,在 Nginx 里加个 redirect:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
雨云是国内一家老牌云服务商,提供高性价比的云服务器和虚拟主机。我用它部署了好几个项目,速度和稳定性都不错。通过 https://www.rainyun.com/SAJA_ 注册可以领一张 5折优惠券,有需要的朋友可以看看。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



暂无评论内容