HTTPS域名证书生成

Let’s Encrypt 颁发的是标准的域名验证型(DV)证书,且不收取任何费用,我们可以使用 ACME 客户端 Certbot 从 Let’s Encrypt 获得证书,为网站启用 HTTPS 连接。
服务器环境:

  • 操作系统:Centos
  • web server:Nginx

Webroot 方式

通过在 Web 服务器的 http://<你的域名>/.well-known/acme-challenge/<TOKEN>(用提供的令牌替换 <TOKEN>)路径上放置指定文件,来确认对域名的所有权。

  1. 安装Certbot:yum install certbotDocker方式安装,可查看官方文档
  2. 在对应域名的server{}中增加配置:
 server {
    ...
    location ^~ /.well-known/acme-challenge/ {
            default_type "text/plain";
            root /usr/share/nginx/html;
    }
    ...
}
重载Nginx使配置生效:`nginx -s reload`  
  1. 生成证书
    certbot certonly --webroot --email username@qq.com -d a.test.com --webroot-path /usr/share/nginx/html --debug
    • –email 邮件地址用于接收域名到期提醒
    • –webroot-path 与server中新增location配置的root路径一致
  2. 配置证书
server {
    ...
    listen       443 ssl http2;
    ssl_certificate     "/etc/letsencrypt/live/a.test.com/fullchain.pem";  # 证书链路径
    ssl_certificate_key "/etc/letsencrypt/live/a.test.com/privkey.pem"; # 私钥路径
    ...
}
重载Nginx使配置生效:`nginx -s reload`  
  1. 定时更新证书,执行crontab -e,新增一条计划任务
    1. 更新所有证书
      0 0 1 */2 * certbot renew --force-renewal --webroot --email username@qq.com --webroot-path /usr/share/nginx/html --post-hook "nginx -s reload" --quiet
    2. 更新单个证书
      0 0 1 */2 * certbot renew --force-renewal --cert-name a.test.com --webroot --email username@qq.com --webroot-path /usr/local/nginx/html --post-hook "nginx -s reload" --quiet

DNS方式

通过在一条名为_acme-challenge.<你的域名> 的TXT记录中放置特定值来确认对域名的DNS控制权,可用于颁发通配符证书

手动验证

  1. 安装Certbot:yum install certbot
  2. 执行命令certbot certonly --preferred-challenges dns --manual -d *.test.com --server https://acme-v02.api.letsencrypt.org/directory,提示如下:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
dns-01 challenge for test.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.test.com with the following value:

nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
  1. 添加TXT记录
    登录域名服务商,添加一条name为_acme-challenge.test.com,value为nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs的TXT解析记录。
    验证DNS解析是否生效的方法:
    1. nslookup -qt=txt _acme-challenge.test.com
    2. dig -t txt _acme-challenge.test.com

DNS解析生效后,按下Enter键通过验证,申请成功会看到以下信息:

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/test.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/test.com/privkey.pem
   Your cert will expire on 2024-01-08. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le
  1. 配置证书
server {
    ...
    listen       443 ssl http2;
    ssl_certificate     "/etc/letsencrypt/live/test.com/fullchain.pem";  # 证书链路径
    ssl_certificate_key "/etc/letsencrypt/live/test.com/privkey.pem"; # 私钥路径
    ...
}

重载Nginx使配置生效:nginx -s reload

使用 --manual 创建的证书不支持自动续订,除非通过 --manual-auth-hook身份验证hook脚本
结合使用以自动设置所需的 HTTP 文件或 DNS TXT记录。

自动验证

上面的方式需要手动到DNS服务商更改解析通过验证,Certbot提供了一些官方的插件实现自动更新DNS,但是没有针对国内 DNS服务商的插件,这里找到一个针对阿里云 DNS腾讯云 DNS华为云 NDSGoDaddy身份验证hook脚本。以华为云为例:

  1. 下载
$ git clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au
$ cd certbot-letencrypt-wildcardcertificates-alydns-au
$ chmod 0777 au.sh
  1. 配置
    1. domain.ini
      如果domain.ini文件没有你的根域名,请自行添加。
    2. 编辑au.sh文件,配置 DNS API 密钥:
      HWY_KEY 和 HWY_TOKEN:华为云 API 密钥官方申请文档
  2. 申请证书
    1. 验证是否有错误
      certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "./au.sh python hwy add" --manual-cleanup-hook "./au.sh python hwy clean" --dry-run
    2. 申请证书
      certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "./au.sh python hwy add" --manual-cleanup-hook "./au.sh python hwy clean"
  3. 自动续约
    执行crontab -e,新增一条计划任务
    0 0 1 */2 * certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "/path/to/au.sh python hwy add" --manual-cleanup-hook "/path/to/au.sh python hwy clean" --post-hook "nginx -s reload" --non-interactive
    • certonly:获取续订证书,但不安装
    • –config-dir:Configuration directory. (default: /etc/letsencrypt)
    • 更多参数设置可查看官方文档

其他 ACME 客户端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值