Your certificate (or certificates) for the names listed below will expire in 20 days (on 29 Jan 19 18:00 +0000). Please make sure to renew your certificate before then, or visitors to your website will encounter errors.期限切れるよ、という通知だが、無償でこんな監視までしてくれてるのだなぁとちょっと驚いた。
しかし、crondで定期的に更新するよう設定していたのに、いつの間にか動かなくなっていたらしい。
昔書いた設定時の記事はこちら。
その時に設定していたコマンドは以下
# /usr/share/letsencrypt/letsencrypt-auto renew --force-renew && /bin/systemctl reload httpd調べてみると、certbot と呼ばれるものを使うように変わったらしい。
このcertbotというものについて、WebrootプラグインとStandaloneプラグインというものがあり、
それぞれ特徴がある。それぞれの仕組みを解説しつつ、更新方法について記録。
なお、環境はCentOS7でApacheを動かしている環境となっている。
まずはパッケージのインストール。ここはどちらのプラグインでも共通。
# yum install certbot python-certbot-apachまずはWebrootプラグインの方式から。
証明書更新コマンドは以下。
# certbot renew --webroot-path /var/www/html/ --post-hook "systemctl reload httpd"
renewは更新の時に指定する引数。
Webrootプラグインの仕組みは、コマンド実行時、所定の場所に認証用のファイルを配置した後、Let's Encryptのサーバがhttpでアクセス、きちんと指定のドメインで
名前解決してアクセスできるよね!ということで認証を行う。
認証用のファイルの配置等はcertbotが行ってくれる。
--webroot-pathというのはそのファイルを何処に置くかという指定。
上記の例の場合、以下の通り、認証用のファイルが配置される。
/var/www/html/.well-known/acme-challenge//var/www/html
はApacheのDocumentRootのパスとなっている。
したがって、Let's Encryptのサーバがhttpでアクセスでき、認証できる、といった感じだ。 --post-hook
という引数については、certbotの処理が終わった後、実行したいコマンドを指定する。
certbotの処理が成功しても失敗しても実行されるらしい。
httpdをリロードする処理を行うことで、証明書を読み込みし直している。
もし、certbotの処理前にコマンドを実行したい場合は以下となる。
--pre-hook 自宅サーバで更新コマンドを実行した場合、以下のメッセージが表示された。
Saving debug log to /var/log/letsencrypt/letsencrypt.log80番ポートで接続してるけど、繋がらないよ、というメッセージ。。更新に失敗。
Processing /etc/letsencrypt/renewal/[FQDN].conf
Cert is due for renewal, auto-renewing…
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for [FQDN]
Cleaning up challenges
Attempting to renew cert ([FQDN]) from /etc/letsencrypt/renewal/[FQDN].conf produced an unexpected error: Unable to find a virtual host listening on port 80 which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port 80.. Skipping.
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/[FQDN]/fullchain.pem (failure)
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/[FQDN]/fullchain.pem (failure)
Running post-hook command: systemctl reload httpd
1 renew failure(s), 0 parse failure(s
自宅の環境の場合、443番と80番ポートとで別々のサービスを動かしているため、困ったことに。
とりあえず、80番ポートでサーバに接続できるようにした後、
httpd.confを編集、以下の記述を追記し、80番ポートのVirtual Hostを設定。
NameVirtualHost *:80その後、再度更新コマンドを実行。更新が成功した。
<VirtualHost *:80>
ServerAdmin root@[FQDN]
DocumentRoot /var/www/html
ServerName [FQDN]
</VirtualHost
Saving debug log to /var/log/letsencrypt/letsencrypt.logさて、とりあえず更新はできたものの、毎回80番ポートに接続できるようにするのはめんどいし、
Processing /etc/letsencrypt/renewal/[FQDN].conf
Cert is due for renewal, auto-renewing…
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for [FQDN]
Waiting for verification…
Cleaning up challenges
Resetting dropped connection: acme-v02.api.letsencrypt.org
new certificate deployed with reload of apache server; fullchain is
/etc/letsencrypt/live/[FQDN]/fullchain.pem
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/[FQDN]/fullchain.pem (success)
Running post-hook command: systemctl reload httpd
自動化が難しい。
443ポートで認証できる仕組みはないものか、と調べてみた所、Standaloneプラグインを使うと
できるらしい。
[参考]
ポート80を開放していないWebサーバでも証明書を発行できますか?
StandaloneプラグインはWebサーバが無くてもサーバ証明書が発行できる仕組み。
上記の通り、Webroot方式ではhttpでアクセスできて認証を行うため、Webサーバを用意する必要がある。
Standaloneプラグインの場合、Certbot自体が一時的にWebサーバを立ててくれて、認証終わったら
Webサーバを落とす仕組みとなっている。
注意点としては、CertbotがWebサーバを立てるため、もし既存のWebサーバが存在していた場合、
Webサーバを一度落とさないと、サービスが競合してしまい、証明書発行ができない。
つまり、更新の度に更新が終わるまでWebサーバを落とさないといけない。
個人サーバなので良しとするが、サービス仕様によっては厳しいかも?
Standaloneプラグインを使う場合は以下のコマンドとなる。
# certbot --authenticator standalone --installer apache --standalone-supported-challenges tls-sni-01出力されたログは以下
The standalone specific supported challenges flag is deprecated. Please use the --preferred-challenges flag instead.なんだか失敗である。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Which names would you like to activate HTTPS for?
1: [FQDN]
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 (←ここで1を入力している)
Cert is due for renewal, auto-renewing…
Renewing an existing certificate
Performing the following challenges:
Client with the currently selected authenticator does not support any combination of challenges that will satisfy the CA.
どうも、いきなり -standalone-supported-challenges tls-sni-01
はうまく行かないらしい。ということで、一旦http,80番ポートでアクセスできるようにした後、
以下のコマンドを実行。
# certbot --authenticator standalone --installer apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log上記の中盤あたり、
Plugins selected: Authenticator standalone, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Which names would you like to activate HTTPS for?
1: [FQDN]
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 (←ここで1を入力)
Cert is due for renewal, auto-renewing…
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for [FQDN]
Waiting for verification…
Cleaning up challenges
Resetting dropped connection: acme-v02.api.letsencrypt.org
Deploying Certificate to VirtualHost /etc/httpd/conf.d/ssl.conf
Error while running apachectl graceful.
Job for httpd.service invalid.
Unable to restart apache using ['apachectl', 'graceful']
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 (←ここで2を入力)
Redirecting vhost in /etc/httpd/conf/httpd.conf to ssl vhost in /etc/httpd/conf.d/ssl.conf
Your existing certificate has been successfully renewed, and the new certificate
has been installed.
The new certificate covers the following domains:
https://[FQDN]
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=[FQDN]
IMPORTANT NOTES:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/[FQDN]/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/[FQDN]/privkey.pem
Your cert will expire on 2019-07-24. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. 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
2: Redirect
を選択している。これでhttpを使わなくともhttpsで認証を行えるようにできるらしい。
あとコマンドにrenewをつけてなかったが、勝手にrenewしてくれている。
この設定移行はhttpを使わなくても、httpsのみでrenewできるようようになった。
最後に、自動更新を行うようにcrontabに設定を行う。
0 4 1 * * /bin/certbot renew --pre-hook "systemctl stop httpd" --post-hook "systemctl start httpd"上記で説明したとおり、--pre-hookで一度httpdのサービスを停止、
更新が終わった後、--post-hookでサービスを起動している。
上記だと毎月の1日、4時に更新が行われるようにしている。
以上が更新の方法となる。
最後に、Webroot方式の参考情報。
常にhttpsでアクセスさせるようになっているサイト(httpでアクセスした場合もhttpsに
リダイレクトされるサイト)の場合について、こちらも更新しようとするとhttp認証できないため、失敗する。
ただ、Webroot方式は認証時にアクセスされるパスは決まっているため、そのアクセスされるパスだけ
80番ポートでアクセスするようにconfファイルに書けば対応はできるとのこと。
ちょっとめんどくさいが、止められないサーバの場合、そのように対処すると良さそう。