1. 程式人生 > >使用acme.sh快速生成SSL證書

使用acme.sh快速生成SSL證書

Let's Encrypt

起因

早上收到了一封來自MySSL EE <[email protected]>的郵件提示證書即將過期, 少於7天,但是acme.sh應該是60天自動renew的。於是檢視下安裝的證書列表:

acme.sh --list

結果如下(已排版):

Main_Domain:*.lovemiku.info  ""         
KeyLength:lovemiku.info  
Created:Wed Sep 12 02:01:06 UTC 2018  
Renew:Sun Nov 11 02:01:06 UTC 2018

查詢到應該是11月11日更新過但是實際上並沒有更新。

才想起以前是通過手動新增DNS

txt解析來認證的,可能會失效?

於是這次準備直接換成API認證。

acme.sh的DNS API認證支援各大解析商,在此可以查詢。

下面是完整安裝到申請證書的流程。

安裝acme.sh

根據中文文件可以可知,輸入以下指令便可以安裝成功。

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

安裝完後:

在使用者home目錄下生成一個.acme.sh資料夾,使用ls -a可以檢視

自動為你建立 cronjob, 每天 0:00 點自動檢測所有的證書, 如果快過期了(預設60天), 需要更新, 則會自動更新證書。使用crontab -l命令可以檢視任務。

使用DNS解析商API認證並且生成證書

我的域名是在namesilo購買的,所以先去查詢API,然後匯入:

vim /etc/profile

新增APIKey

export Namesilo_Key="your api key"

使其生效:

source /etc/profile

生成泛域名證書,一定要含有頂級域名yourdomain.com和泛域名*.yourdomain.com

900為等待生效時間,每個解析商時間可能不同,預設120秒。

acme.sh --issue --dns dns_namesilo --dnssleep 900 -d yourdomain.com -d *.yourdomain.com

個人生成結果如下,可以看到成功生成了證書:

acme.sh --issue --dns dns_namesilo --dnssleep 900 -d lovemiku.info -d *.lovemiku.info
Multi domain='DNS:lovemiku.info,DNS:*.lovemiku.info'
Getting domain auth token for each domain
Getting webroot for domain='lovemiku.info'
Getting webroot for domain='*.lovemiku.info'
Found domain api file: /root/.acme.sh/dnsapi/dns_namesilo.sh
Successfully added TXT record, ready for validation.
Sleep 900 seconds for the txt records to take effect
Verifying:lovemiku.info
It seems the CA server is busy now, let's wait and retry. Sleeping 1 seconds.
Success
*.lovemiku.info is already verified, skip dns-01.
Removing DNS records.
Successfully retrieved the record id for ACME challenge.
Successfully removed the TXT record.
Verify finished, start to sign.
Cert success.
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
Your cert is in  /root/.acme.sh/lovemiku.info/lovemiku.info.cer 
Your cert key is in  /root/.acme.sh/lovemiku.info/lovemiku.info.key 
The intermediate CA cert is in  /root/.acme.sh/lovemiku.info/ca.cer 
And the full chain certs is there:  /root/.acme.sh/lovemiku.info/fullchain.cer

安裝證書

普通安裝證書直接輸入以下指令:

坑注意-d後面的域名地址不要用*.yourdomain.com,否則當訪問yourdomain.com時會出現證書錯誤

acme.sh --installcert -d yourdomain.com \
        --key-file /path/to/key/yourdomain.key \
        --fullchain-file /path/to/key/fullchain.cer \
        --reloadcmd "systemctl force-reload nginx"

由於我有2臺vps都在使用這個域名,所有我想把證書上傳到另一個機器,就需要寫一個shell指令碼。由於要上傳檔案遠端reload nginx,於是先把另一臺機器的ssh金鑰上傳到生成證書的vps中,然後更改許可權:

chmod 600 /root/Server_JP/id_rsa

接下來新建一個指令碼檔案UploadCert.sh,使用vim新增以下執行程式碼:

[ip]:去掉[]修改為目標vpsip

[port]:去掉[]修改為目標vpsssh埠

[username]:去掉[]修改為目標vps登入名

[password]:去掉[]修改為目標vps密碼

[/path/to/save]:去掉[]修改為目標vps需要儲存證書的目錄

[/local/path/to/copy]:去掉[]修改為當前vps儲存證書的目錄

[yourdomain.key]:去掉[]修改為安裝證書時命名的key名稱

#!/bin/bash
#上傳檔案
ftp -n<<FtpEnd
open [ip]
user [username] [password]
binary
hash
cd [/path/to/save]
lcd [/local/path/to/copy]
prompt
mput [yourdomain.key] fullchain.cer
close
bye
FtpEnd
#登入ssh重新整理nginx
ssh -T -i [/path/to/rsa] [username]@[ip] -p [port] << SSHEnd
echo "Connect to remote server."
systemctl force-reload nginx
echo "Force reload nginx."
exit
SSHEnd

儲存後更改許可權:

chmod +x UploadCert.sh

修改前面安裝的程式碼,新增一個--reloadcmd來執行指令碼,如下:

acme.sh --installcert -d lovemiku.info \
        --key-file /etc/nginx/sites/lovemiku.key \
        --fullchain-file /etc/nginx/sites/fullchain.cer \
        --reloadcmd "systemctl force-reload nginx" \
        --reloadcmd  "/root/MyScript/UploadCert.sh"

執行結果如下,沒有報錯應該完美完成,檢查下目標vps上檔案更改時間和nginxforce-reload時間即可。

Installing key to:/etc/nginx/sites/lovemiku.key
Installing full chain to:/etc/nginx/sites/fullchain.cer
Run reload cmd: /root/MyScript/UploadCert.sh
Hash mark printing on (1024 bytes/hash mark).
Local directory now /etc/nginx/sites
Interactive mode off.
#
###
Connect to remote server.
Force reload nginx.
Reload success

結語

就這樣很簡單就完成了SSL證書的Renew,到底能不等60天后自動更新。

執行acme.sh --list可以檢視Renew的具體時間,等2019年2月份,我再來看看是否會自動Renew。

本文也發表在自己的部落格https://lovemiku.info/2018/12/04/how-to-use-acme-sh.html