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"



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