免费 SSL 域名证书申请
你要的其实就是免费 SSL 域名证书,能直接配到 Nginx 上,并且可以下载到本地(而不是像一些托管服务只给 HTTPS 反代)。
我帮你分成两种方式:自动申请(推荐)和 手动申请(方便离线保存)。
一、自动申请(Let’s Encrypt + Certbot,适合可联网服务器)
Let’s Encrypt 提供免费 DV 证书,有效期 90 天,支持自动续期。
1. 安装 Certbot
Ubuntu / Debian:
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
2. 申请证书
假设你的域名是 example.com,且已经解析到服务器 IP:
sudo certbot --nginx -d example.com -d www.example.com
Certbot 会自动:
-
验证域名
-
生成
/etc/letsencrypt/live/example.com/fullchain.pem
和privkey.pem
-
修改 Nginx 配置加载证书
3. Nginx 配置示例
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
root /var/www/html;
index index.html;
}
}
4. 自动续期
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
二、手动申请(可下载保存 Nginx 格式证书)
如果你想在本地生成并保存证书文件(方便迁移到其他服务器),可以用 acme.sh。
1. 安装 acme.sh
curl https://get.acme.sh | sh
source ~/.bashrc
2. 使用 DNS 验证申请(适合不能开放 80/443 的环境)
以阿里云为例(需要有 API Key):
export Ali_Key="你的阿里云AccessKeyId"
export Ali_Secret="你的阿里云AccessKeySecret"
acme.sh --issue --dns dns_ali -d example.com -d www.example.com
3. 转换成 Nginx 可用格式并下载
acme.sh --install-cert -d example.com \
--key-file /root/cert/example.com.key \
--fullchain-file /root/cert/example.com.crt \
--reloadcmd "nginx -s reload"
生成后 /root/cert/example.com.key
和 /root/cert/example.com.crt
就可以打包下载了。
三、注意事项
-
免费证书一般 90 天有效,需要续期(acme.sh 可自动续期)。
-
如果是公司内部测试,可以用自签证书(但浏览器会提示不安全)。
-
申请时域名必须已解析到你的服务器,否则验证失败。
如果你愿意,我可以帮你写一个一键脚本,自动用 acme.sh
申请证书并输出 Nginx 配置,证书会直接保存在你指定的目录方便下载。
这样你只要运行一次,就能获得可以直接用的 .crt
和 .key
文件。
你要我帮你写这个一键申请脚本吗?我可以帮你加上可选 DNS 验证方便内网环境。