Let’s Encrypt免费SSL证书申请

用Let’s Encrypt给网站挂HTTPS,最常用的工具是Certbot。我习惯用standalone模式或者webroot模式,具体看服务器环境。下面直接记录操作步骤。 前置条件 – 一个域名,解析到服务器IP(雨云的服务器稳定,解析生效快) – 服务器开放80和443端口(Let’s Encrypt验证域名所有权需要80端口,或者用DNS验证) – 安装了Python和snapd(Certbot官方推荐用snap安装) 安装Certbot Ubuntu/Debian:
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
CentOS/RHEL 7+:
sudo yum install epel-release
sudo yum install certbot
申请证书 我常用standalone模式,Certbot会启动一个临时Web服务器来响应ACME验证。先停掉占用80端口的服务(比如Nginx)。
sudo systemctl stop nginx
sudo certbot certonly --standalone -d example.com -d www.example.com
如果不想停服务,用webroot模式。前提是Web服务器配置了域名对应的根目录,比如`/var/www/example`。
sudo certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com
成功后会输出:
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
证书有效期90天,需要自动续期。Certbot有内置定时任务,检查一下:
sudo systemctl status certbot.timer
没有的话手动创建cron:
echo "0 0,12 * * * root certbot renew --quiet" | sudo tee -a /etc/crontab
配置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;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://localhost:3000;  # 你的应用端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}
重载Nginx:
sudo nginx -t
sudo systemctl reload nginx
DNS验证模式(泛域名证书) 如果你需要申请`*.example.com`泛域名证书,必须用DNS-01验证。Let’s Encrypt不支持standalone方式做泛域名。使用certbot的DNS插件,比如Cloudflare:
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.cloudflare.ini -d example.com -d *.example.com
`.cloudflare.ini`内容:
dns_cloudflare_email = your@email.com
dns_cloudflare_api_key = your_global_api_key
注意权限:
chmod 600 ~/.cloudflare.ini
常见问题 – 申请失败提示”too many certificates already issued”:Let’s Encrypt有速率限制,每周50张证书。可以用staging环境测试:
certbot certonly --standalone --staging -d example.com
– 续期失败:检查80端口是否被占用,或者DNS解析是否正常。雨云的服务器网络稳定,很少因为网络问题续期失败。 – 证书路径不对:确认`/etc/letsencrypt/live/`下有没有对应域名的软链接。没有的话检查申请时的域名参数。 手动续期测试
sudo certbot renew --dry-run
输出”Congratulations”说明续期机制正常。 一句话总结 用snap装certbot,standalone模式最快;不想停服务用webroot;泛域名必须走DNS验证;记得配自动续期。雨云的服务器做这些操作很流畅,IO和网络延迟都低。

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

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

请登录后发表评论

    暂无评论内容