木须柄的时光工坊

探索技术与游戏乐趣的奇妙之旅

目录
SSL 证书自动更新部署
/  

SSL 证书自动更新部署

一、需求背景

  • 经过了一年多的运行,服务器上的很多服务都出现了 SSL 证书过期的问题。因此想着去更新一下证书服务,但是又嫌服务商的免费证书时间太短,更新太麻烦了,因此考虑新的自动化方法;

  • 统一通过 Nginx 反向代理 + 子域名访问

  • 希望:

    • 使用 Let’s Encrypt 免费证书
    • 一个通配符证书覆盖 *.[根域名]
    • 后续可 稳定续期

二、修复 DNS 解析

一开始我发现服务器的 DNS 配置居然是空的

1ls -l /etc/resolv.conf 
2lrwxrwxrwx 1 root root 32 Nov 28 2023 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
3
4cat /etc/resolv.conf 
5cat: /etc/resolv.conf: No such file or directory 

修复问题

 1# 删除无效软链接
 2rm -f /etc/resolv.conf
 3
 4# 添加新的配置文件
 5cat > /etc/resolv.conf << 'EOF'
 6nameserver 223.5.5.5
 7nameserver 223.6.6.6
 8nameserver 8.8.8.8
 9nameserver 1.1.1.1
10EOF
11
12# 验证 DNS 是否恢复
13ping -c 3 baidu.com
14ping -c 3 gitee.com
15ping -c 3 github.com

三、安装 acme.sh

 1# 安装 acme.sh
 2cd /opt
 3curl -fsSL https://gitee.com/neilpang/acme.sh/raw/master/acme.sh -o acme.sh
 4sh acme.sh --install
 5
 6Installing to /root/.acme.sh
 7Installed cron job
 8Installed acme.sh
 9
10# 加入 PATH,作为系统命令使用
11ln -s /root/.acme.sh/acme.sh /usr/local/bin/acme.sh
12
13# 验证文件版本
14acme.sh --version
15
16# 设置默认 CA(Let’s Encrypt)
17acme.sh --set-default-ca --server letsencrypt
18acme.sh --info | grep DEFAULT_CA

四、申请通配符证书

需求说明

  • *.example.com必须使用 DNS-01
  • 使用手动模式,避免 API 授权复杂度

申请命令

1acme.sh --issue \
2  -d example.com \
3  -d '*.example.com' \
4  --dns \
5  --yes-I-know-dns-manual-mode-enough-go-ahead-please

操作要点

  • **根据提示,在 DNS 面板添加 **_acme-challenge.example.com TXT 记录
  • 等待解析生效后按提示继续

五、证书续期(验证成功)

1acme.sh --renew -d example.com --debug

成功标志:

  • status: valid
  • 证书路径输出正常

证书位置:

1~/.acme.sh/example.com_ecc/
2├── example.com.cer
3├── example.com.key
4├── fullchain.cer
5└── ca.cer

八、Nginx 配置与 SSL 生效(关键坑)

最终可用配置(以 Picuang 图床服务示例)

/etc/nginx/nginx.conf

 1user www-data;
 2worker_processes auto;
 3pid /run/nginx.pid;
 4
 5events {
 6    worker_connections 1024;
 7}
 8
 9http {
10    sendfile on;
11    tcp_nopush on;
12    types_hash_max_size 2048;
13
14    include /etc/nginx/mime.types;
15    default_type application/octet-stream;
16
17    access_log /var/log/nginx/access.log;
18    error_log  /var/log/nginx/error.log;
19
20    gzip on;
21
22    include /etc/nginx/conf.d/*.conf;
23}

/etc/nginx/conf.d/00-ssl.conf

1ssl_certificate     /etc/nginx/ssl/example.com/fullchain.pem;
2ssl_certificate_key /etc/nginx/ssl/example.com/privkey.pem;
3
4ssl_protocols TLSv1.2 TLSv1.3;
5ssl_ciphers   HIGH:!aNULL:!MD5;
6ssl_prefer_server_ciphers off;
7
8ssl_session_cache shared:SSL:10m;
9ssl_session_timeout 10m;

/etc/nginx/conf.d/00-proxy.conf

1proxy_set_header Host $host;
2proxy_set_header X-Real-IP $remote_addr;
3proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4client_max_body_size 100m;

/etc/nginx/conf.d/pic.conf

 1server {
 2    listen 80;
 3    server_name pic.example.com;
 4    return 301 https://$host$request_uri;
 5}
 6
 7server {
 8    listen 443 ssl http2;
 9    server_name pic.example.com;
10
11    location / {
12        proxy_pass http://127.0.0.1:8080/picuang/;
13        include /etc/nginx/conf.d/00-proxy.conf;
14    }
15}
评论
取消