Centos7 下使用acme.sh以DNS方式申请免费SSL证书

一、安装acme.sh(Let's Encrypt 客户端)

github:https://github.com/Neilpang/acme.sh

wget -O -  https://get.acme.sh | sh

安装完成后,会自动增加任务用于证书的自动更新

注意:使用acme.sh不必在root下操作,但建议在root下操作


二、手工申请证书:

  注:这种方法一般适用于DNS供应商不提供API的场景,且必须带有参数--yes-I-know-dns-manual-mode-enough-go-ahead-please,且不能自动续期

1、首先配置好要申请证书的域名,例如:*.myimportantdomain.com,一般为10分钟:

acme.sh --issue --dns -d \
  *.myimportantdomain.com \
  --yes-I-know-dns-manual-mode-enough-go-ahead-please


2、根据提示(会以绿色文字显示)在DNS中创建所申请域名的对应的TXT记录,并等待DNS记录生效,一般为10分钟:

Add the following TXT record:

Domain: '_acme-challenge.myimportantdomain.com'

TXT value: '123pXF6fDUM88pg14kY123D3AUc5Cd_YVYZ5znpnC38'

Please be aware that you prepend _acme-challenge. before your domain

so the resulting subdomain will be: _acme-challenge..myimportantdomain.com

Please add the TXT records to the domains, and re-run with --renew.

Please add '--debug' or '--log' to check more details.

See: https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh


3、等待DNS解析生效后,执行以下命令

acme.sh --renew --dns \
  -d *.myimportantdomain.com
  --yes-I-know-dns-manual-mode-enough-go-ahead-please


4、申请成功,会提示证书文件存放位置:

Your cert is in  /root/.acme.sh/*.myimportantdomain.com/*.myimportantdomain.com.cer

Your cert key is in  /root/.acme.sh/*.myimportantdomain.com/*.myimportantdomain.com.key 

v2 chain.

The intermediate CA cert is in  /root/.acme.sh/*.myimportantdomain.com/ca.cer

And the full chain certs is there:  /root/.acme.sh/*.myimportantdomain.com/fullchain.cer

_on_issue_success


三、安装证书:

acme.sh --install-cert -d *.myimportantdomain.com \ 
  --key-file /usr/local/nginx/conf/*.myimportantdomain.com.key \ 
  --fullchain-file /usr/local/nginx/conf/*.myimportantdomain.com \
  --reloadCmd "nginx -s restart"


四、自动申请多个域名证书和并续期

 注:这种方法一般适用于DNS供应商支持API的场景,免去了手工续期的麻烦

1、由于使用的是腾讯云的dnspod,根据api接口找到对应的API进行设置,其他API可参考这里

export DP_Id="id"

export DP_Key="key"


2、使用--challenge-alias参数做别名处理,增加重要域名的安全性,由于使用api模式,dns生效时间一般在10分钟左右,acme会不断重试,等待即可。

acme.sh --issue --debug \

 --dns dns_dp -d *.aliasDomain.com \

 --dns dns_dp -d *.myimportantdomain.com --challenge-alias aliasDomain.com

注:生成的证书是保存在以别名域名命名的文件夹,而不是重要域名命名的文件夹


3、由于acme的--reloadcmd参数只会执行一条命令,我们可以把所有需要更新证书的操作写在一个reload_cert.sh脚本内

mkdir -p /root/cmd
vi /root/cmd/reload_cert.sh

#!/bin/bash
#reload nginx
service nginx restart
service php-fpm restart
#restart httpd server
rm -rf /usr/local/httpd/data/conf/server.crt
rm -rf /usr/local/httpd/data/conf/server.key
cp /root/.acme.sh/*.aliasDomain.com/fullchain.cer /usr/local/httpd/data/conf/server.crt
cp /root/.acme.sh/*.aliasDomain.com/*.aliasDomain.com.key /usr/local/httpd/data/conf/server.key
chown svn:svn /usr/local/httpd/data/conf/server.crt
chown svn:svn /usr/local/httpd/data/conf/server.key
sudo -u svn /usr/local/httpd/bin/httpd -f /usr/local/httpd/data/conf/httpd.conf -k restart

再执行证书安装命令,使用reload_cmd参数执行我们自行编写的脚本

acme.sh --install-cert -d *.aliasDomain.com \
  --key-file /usr/local/nginx/conf/cert/*.myimportantdomain.com \
  --fullchain-file /usr/local/nginx/conf/cert/*.myimportantdomain.com \
  --reloadcmd "/bin/bash /root/cmd/reload_cert.sh"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章