ラズパイでSSL化(https)対応してみる

2017年11月7日

certbot-autoがインストールできたので、使用してみます。

実行する前にnginxを停止させてからでないとストップしてしまいます。
80番ポートを使うからですね。

$ sudo service nginx stop
$ sudo ./certbot-auto certonly --standalone -t
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): mlincyan@gmail.com

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): mlin.sakura.ne.jp
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for mlin.sakura.ne.jp
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mlin.sakura.ne.jp/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mlin.sakura.ne.jp/privkey.pem
Your cert will expire on 2018-01-08. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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

 

 

更新するのは簡単で「renew」すればいいだけです。
古い情報ではもっと色々オプションを追加しているのですけど、今は不要です。

$ sudo ./certbot-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/mlin.sakura.ne.jp.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/mlin.ga.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

-------------------------------------------------------------------------------

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/mlin.sakura.ne.jp/fullchain.pem (skipped)
  /etc/letsencrypt/live/mlin.ga/fullchain.pem (skipped)
No renewals were attempted.
-------------------------------------------------------------------------------

実際にはSKIPされています。

で、実はこのままrenewするとstandaloneで実行されてしまうので、nginxを停止させる必要があります。
これだとちょっとあれなので、WEBROOTで実行するように変更しておきます。

$ ./certbot-auto certonly --webroot -w /home/mlin/www/html/mlin.sakura.ne.jp -d mlin.sakura.ne.jp --agree-tos --force-renewal -n
Requesting to rerun ./certbot-auto with root privileges...
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for mlin.sakura.ne.jp
Using the webroot path /home/mlin/www/html/mlin.sakura.ne.jp for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Unable to clean up challenge directory /home/mlin/www/html/mlin.sakura.ne.jp/.well-known/acme-challenge

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/mlin.sakura.ne.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/mlin.sakura.ne.jp/privkey.pem
   Your cert will expire on 2018-01-08. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto 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

----

diffコマンドで何が変わったか見てみます。

/etc/letsencrypt $ diff renewal.def/mlin.sakura.ne.jp.conf renewal/mlin.sakura.ne.jp.conf
11c11
< authenticator = standalone --- > authenticator = webroot
13a14,16
> webroot_path = /home/mlin/www/html/mlin.sakura.ne.jp,
> [[webroot_map]]
> mlin.sakura.ne.jp = /home/mlin/www/html/mlin.sakura.ne.jp

ちなみに
standalone方式は自分自信でWEBサーバーを立てて認証をするのですけど、
WEBROOT方式は/.well-known/acme-challenge/というディレクトリに認証キーをおいて認証をします。

cronはこんな感じで。

0 4 1 * * /usr/local/certbot/certbot-auto renew && systemctl reload nginx

reloadしないと反映されないらしい。再起動は不要みたいです。
でもsystemctlはルートじゃないと駄目なのでルートのcrontabに設定しないと駄目です。

 

nginxの設定は最低限以下を追加すればOKです。
server{ の中に書きます。

listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/mlin.sakura.ne.jp/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mlin.sakura.ne.jp/privkey.pem;

 

うまく起動しなくなったときには設定ファイルをテストします。

$ sudo nginx -t

 

起動しなくなると結構ハマることがあるので、できればバックアップを行ってから実行しましょう。