Skip to main content

免费 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.pemprivkey.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 可自动续期)。

  • 如果是公司内部测试,可以用自签证书(但浏览器会提示不安全)。

  • 申请时域名必须已解析到你的服务器,否则验证失败。


在服务器未开放80和443端口的情况下,仍然可以通过以下几种方式申请并下载免费的SSL域名证书:

---

### **1. 使用DNS验证方式申请证书**
DNS验证方式不需要开放80或443端口,而是通过添加DNS TXT记录来验证域名所有权。

#### **方法一:使用acme.sh(推荐)**
1. **安装acme.sh**  
   ```bash
   curl https://get.acme.sh | sh
   ```
2. **申请证书(以阿里云DNS为例)**  
   ```bash
   export Ali_Key="你的AccessKey"
   export Ali_Secret="你的AccessSecret"
   acme.sh --issue --dns dns_ali -d example.com -d *.example.com
   ```
3. **证书存储位置**  
   证书默认存放在 `~/.acme.sh/example.com/` 目录下,包含 `fullchain.cer` 和 `example.com.key` 文件。

#### **方法二:使用CertBot**
1. **安装CertBot**  
   ```bash
   sudo apt install certbot
   ```
2. **手动DNS验证**  
   ```bash
   certbot certonly --manual --preferred-challenge dns -d example.com
   ```
   按照提示添加TXT记录 `_acme-challenge.example.com`,等待解析生效后继续。

---

### **2. 使用邮箱验证(适用于ZeroSSL)**
如果无法使用DNS验证,可以尝试通过邮箱验证:
1. **在ZeroSSL申请证书**  
   - 访问 [ZeroSSL](https://zerossl.com/) 并选择邮箱验证。
2. **在服务器上搭建Postfix邮件服务**  
   - 接收验证邮件(如 `admin@example.com`)以完成验证。

---

### **3. 使用JoySSL等支持非标准端口的CA**
某些CA(如JoySSL)支持通过22端口(SSH)进行验证:
1. **访问JoySSL官网**  
   - 选择IP或域名证书类型。
2. **通过SSH验证服务器所有权**  
   - 提供服务器访问权限,CA通过22端口完成验证。

---

### **4. 使用acme-redirect(Rust工具)**
1. **安装acme-redirect**  
   ```bash
   git clone https://github.com/kpcyrd/acme-redirect.git
   cd acme-redirect && cargo build --release
   ```
2. **配置并运行**  
   - 修改 `/etc/acme-redirect.d/example.com.conf`,启动服务:
   ```bash
   systemctl enable --now acme-redirect
   ```
   - 证书存放在 `/var/lib/acme-redirect/live/example.com/`。

---

### **5. 自动续期**
- **acme.sh**:默认自动续期,无需额外操作。
- **CertBot**:需手动设置cron任务:
  ```bash
  30 0 * * 1 certbot renew --manual-auth-hook /path/to/dns-update-script.sh
  ```

---

### **总结**
| 方法 | 适用场景 | 工具 | 验证方式 |
|------|---------|------|---------|
| **DNS验证** | 有DNS管理权限 | acme.sh / CertBot | 添加TXT记录 |
| **邮箱验证** | 无80/443端口 | ZeroSSL | 邮件接收验证 |
| **SSH验证** | 服务器可SSH访问 | JoySSL | 22端口验证 |
| **acme-redirect** | 需临时占用80端口 | acme-redirect | HTTP验证 |

选择最适合你的方式,确保域名解析正确后即可获取证书。