使用 acme.sh 申请 SSL 证书并配置 Nginx 自动续期

使用 acme.sh 申请 SSL 证书并配置 Nginx 自动续期

本文介绍如何使用 acme.sh 工具快速申请免费的 Let’s Encrypt SSL 证书,并配置 Nginx 实现自动续期。

参考原文https://pangruitao.com/post/4812
本文使用AI自动生成

1. 背景说明

acme.sh 是一个功能强大的 Shell 脚本工具,可以方便地申请免费的 Let’s Encrypt SSL 证书,并且支持自动续期。相比手动申请证书的方式,acme.sh 极大地简化了流程。

免费证书存在以下问题:

  • 证书有效期从 1 年缩短至 3 个月,频繁申请非常麻烦
  • 每次需要手动申请、下载证书、通过 scp 上传服务器、重启 Nginx
  • 整个过程繁琐且容易出错

使用 acme.sh 后,这些问题都可以自动化解决。

2. 安装 acme.sh

2.1 一键安装脚本

使用官方提供的一键安装脚本:

1
curl https://get.acme.sh | sh

安装完成后:

  • 脚本会自动安装到 ~/.acme.sh/ 目录
  • 自动配置好环境变量
  • 创建别名方便使用

验证安装是否成功:

1
acme.sh --version

如果能看到版本号,说明安装成功。

2.2 注册账号

使用你的邮箱注册 Let’s Encrypt 账号(将 youremail@gmail.com 替换为你的真实邮箱):

1
acme.sh --register-account -m youremail@gmail.com

3. 配置 Nginx 验证

acme.sh 使用 HTTP 验证方式验证域名所有权,需要在 Nginx 中配置验证路径。

3.1 创建验证目录

首先创建验证文件存放目录:

1
mkdir -p /var/www/html

3.2 配置 Nginx

编辑 Nginx 配置文件(通常在 /etc/nginx/sites-enabled/ 目录,如果没有可以新建):

1
vi /etc/nginx/sites-enabled/yourdomain.conf

添加以下配置:

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name yourdomain.com www.yourdomain.com; # 替换为你的域名

location /.well-known/acme-challenge/ {
root /var/www/html; # acme.sh 验证时使用的目录
try_files $uri =404;
}
}

参数说明

  • server_name:替换为你的实际域名
  • /.well-known/acme-challenge/:Let’s Encrypt 验证专用路径
  • root:指定验证文件的根目录

3.3 重启 Nginx

使配置生效:

1
systemctl restart nginx

验证 Nginx 配置是否正确:

1
nginx -t

4. 申请 SSL 证书

4.1 执行申请命令

使用 webroot 模式申请证书(将域名替换为你的):

1
acme.sh --issue -d yourdomain.com -d www.yourdomain.com --webroot /var/www/html

参数说明

  • -d:指定要申请证书的域名(支持多个)
  • --webroot:指定网站根目录,用于验证

4.2 验证申请结果

如果看到类似以下内容,说明申请成功:

1
2
3
4
[Sun Nov 26 10:30:00 UTC 2025] Your cert is in: /root/.acme.sh/yourdomain.com/yourdomain.com.cer
[Sun Nov 26 10:30:00 UTC 2025] Your cert key is in: /root/.acme.sh/yourdomain.com/yourdomain.com.key
[Sun Nov 26 10:30:00 UTC 2025] The intermediate CA cert is in: /root/.acme.sh/yourdomain.com/ca.cer
[Sun Nov 26 10:30:00 UTC 2025] And the full chain certs is there: /root/.acme.sh/yourdomain.com/fullchain.cer

5. 安装证书到 Nginx

5.1 创建证书存放目录

建议将证书放在统一目录管理:

1
mkdir -p /root/certs/yourdomain.com

5.2 安装证书

使用 acme.sh 的 install-cert 命令安装证书:

1
2
3
4
acme.sh --install-cert -d yourdomain.com \
--key-file /root/certs/yourdomain.com/yourdomain.com.key \
--fullchain-file /root/certs/yourdomain.com/yourdomain.com.pem \
--reloadcmd "systemctl reload nginx"

参数说明

  • --key-file:指定私钥文件存放路径
  • --fullchain-file:指定完整证书链文件路径
  • --reloadcmd:证书更新后自动重载 Nginx 的命令

5.3 配置 Nginx 使用 SSL

编辑 Nginx 配置文件,添加 HTTPS 配置:

1
vi /etc/nginx/sites-enabled/yourdomain.conf

完整配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# HTTP 重定向到 HTTPS
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}

# HTTPS 配置
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;

# SSL 证书配置
ssl_certificate /root/certs/yourdomain.com/yourdomain.com.pem;
ssl_certificate_key /root/certs/yourdomain.com/yourdomain.com.key;

# 网站根目录
root /var/www/html;

# 验证路径(续期需要)
location /.well-known/acme-challenge/ {
root /var/www/html;
try_files $uri =404;
}

# 网站内容
location / {
try_files $uri $uri/ =404;
}
}

5.4 重载 Nginx

使 SSL 配置生效:

1
systemctl reload nginx

访问 https://yourdomain.com 验证证书是否生效。

6. 自动续期配置

6.1 验证自动续期任务

acme.sh 默认会自动添加 cron 任务,查看定时任务:

1
crontab -l | grep acme.sh

一般可以看到类似:

1
0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

6.2 手动测试续期

可以手动测试续期流程:

1
acme.sh --cron --force

观察输出,确认续期成功后自动重载 Nginx。

6.3 续期日志

查看 acme.sh 的续期日志:

1
tail -f /root/.acme.sh/acme.sh.log

7. 高级配置(可选)

7.1 使用 DNS 验证模式

如果无法使用 webroot 验证,可以使用 DNS 验证:

1
2
3
4
# 示例:使用 Cloudflare DNS API
export CF_Key="your_cloudflare_api_key"
export CF_Email="your_cloudflare_email"
acme.sh --issue --dns dns_cf -d yourdomain.com -d *.yourdomain.com

7.2 配置 SSL 安全参数

在 Nginx 配置中添加 SSL 安全优化:

1
2
3
4
5
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

8. 常见问题排查

8.1 证书申请失败

检查 Nginx 配置是否正确:

1
2
3
4
5
# 检查 80 端口是否监听
netstat -tlnp | grep :80

# 检查域名解析是否正确
nslookup yourdomain.com

8.2 续期失败

检查 Nginx 是否能正常重载:

1
2
systemctl status nginx
journalctl -u nginx -f

8.3 证书路径错误

确认证书文件权限:

1
ls -lh /root/certs/yourdomain.com/

9. 清理和备份

9.1 备份证书

建议定期备份证书目录:

1
tar -czf certs_backup_$(date +%Y%m%d).tar.gz /root/certs/

9.2 清理旧证书

acme.sh 会自动管理证书,如需手动清理:

1
acme.sh --remove -d yourdomain.com

以上。完结撒花 🎉!


使用 acme.sh 申请 SSL 证书并配置 Nginx 自动续期
https://blog.wenxin.site/2025/11/26/使用acme.sh申请SSL证书并配置Nginx自动续期/
作者
HarryMa
发布于
2025年11月26日
许可协议