2017-02-17 61 views
8

我從LetsEncrypt的certbot中自動執行SSL證書續訂。實際的續訂工作正常,但我需要自動重新啓動服務,以便它們加載更新的證書。我想知道是否可以在cronjob中使用多個--renew-hook參數letsencrypt renewLetsEncrypt certbot多個更新掛鉤

如何在證書更新時自動重啓服務?

回答

11

是的,你可以使用多個--renew-hook語句。也使用-q標誌,因此它會通過電子郵件通知您一個空白通知,直到實際發生更新。在更新發生之前,它也不會重新啓動您的任何服務。如果您願意,這也會將日誌文件附加到電子郵件中。

我有一個每天運行bash的cron。

裏面我的bash(certbotrenew.sh)僅僅是這

#!/bin/bash 
cd /opt/certbot 
sudo ./certbot-auto renew --renew-hook "service postfix reload" --renew-hook "service dovecot restart" --renew-hook "service apache2 reload" -q >> /var/log/certbot-renew.log | mail -s "CERTBOT Renewals" [email protected] < /var/log/certbot-renew.log 
exit 0 

和我cron是

00 20 * * 1 /bin/certbotrenew.sh 

有人質疑我爲什麼不管什麼都沒發生過一封電子郵件,我只是一直想知道我的每日克朗正在運行。

+0

它看起來像默認的包管理器Ubuntu 16.04帶有舊版本的Certbot,它不接受'--renew-hook'參數。 –

+1

是的,回購有一個Let's Encrypt的舊版本,去年它已經重新命名爲certbot。你可以簡單地卸載repo版本,然後簡單地使用'mkdir/opt/certbot'然後cd/opt/certbot然後'sudo wget https:// dl.eff.org/certbot-auto'然後運行你的certbot - 從那裏的自動命令行。如果內存服務正確,Ubuntu repo中的版本是v0.4的加密。 Certbot現在在0.11。 – MitchellK

+2

@MitchellK - 與獲取每日cron電子郵件不同,如何使用cron報告服務(如https://IsItWorking.info(我的))。您的cron腳本可以在每次運行成功時進行檢查。如果沒有檢入,則服務會提醒您。 (可能除了電子郵件) –

7

從我從全新安裝看到在Ubuntu CertBot的16.04,它會創建一個cron作業:

# /etc/cron.d/certbot: crontab entries for the certbot package 
# 
# Upstream recommends attempting renewal twice a day 
# 
# Eventually, this will be an opportunity to validate certificates haven't been revoked, etc. Renewal will only occur if expiration is within 
# 30 days. 
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook 
'/bin/run-parts /etc/letsencrypt/pre-hook.d/' --post-hook '/bin/run-parts /etc/letsencrypt/post-hook.d/' --renew-hook '/bin/run-parts 
/etc/letsencrypt/renew-hook.d/' 

因此,許多目錄執行run-parts,包括/etc/letsencrypt/renew-hook.d/

你只需要添加任何這些掛鉤目錄中的可執行文件(選擇你需要的)。

舉個例子,在我的renew-hook.d我創建了一個文件restart-nginx,內容如下:

#!/bin/bash 
/etc/init.d/nginx restart 

作爲一個說明:您可以知道哪些文件會被run-parts使用--test選項調用。 (例run-parts --test /etc/letsencrypt/renew-hook.d/

+3

他們實際上已將certbot cronjob更改爲不再包含這些運行部件參數。現在推薦的方法是創建一個'/ etc/letsencrypt/cli.ini'並在那裏指定鉤子參數,參見 https://github.com/certbot/certbot/issues/1706#issuecomment-302774426 – Drew

2

您還可以設置掛鉤(和其他人的選擇,如果你喜歡)作爲文件/etc/letsencrypt/cli.inisee documentation)像這樣的全局選項:

# Global config for letsencrypt runs 
# 
# Note that these options apply automatically to all use of Certbot for 
# obtaining or renewing certificates, so options specific to a single 
# certificate on a system with several certificates should not be placed 
# here. 

renew-hook = service postfix reload 
post-hook = service nginx reload 

您必須首先在大多數系統上創建文件。 Letsencrypt來沒有。

您還可以創建在每個文件夾renewal證書特定版本,如果你不喜歡走出去。