経理からエンジニア転向した人のメモ

元経理マンがエンジニアに転向して現在

GCPでLet's encryptでSSL(TLS)を自動更新

続き

ryomoyr.hatenablog.com

証明書の更新

Let's encryptの証明書が3ヶ月で切れるとのこと。 最初に手動で更新してみる。

$ cd /usr/local/certbot

$ ./certbot-auto renew
Requesting to rerun ./certbot-auto with root privileges...
Saving debug log to /var/log/letsencrypt/letsencrypt.log

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

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

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/xxxx.xxxx/fullchain.pem expires on 2018-11-29 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

恐らくまだ証明書切れまで近いわけではないので、スキップされる。

certbot設定の変更

参考記事にもあるように、--standaloneでやるときは、certbotが80番ポートを利用する。

なので最初にNginxをストップした手順があったと思うが、 更新のときも同様にストップさせる必要が出てくる。

そのため設定を--webrootに変更。

$ ./certbot-auto certonly --webroot -w /home/[ユーザ名]/[ドキュメントルートディレクトリ] -d xxxx.xxxx --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 xxxx.xxxx
Using the webroot path /home/xxxx.xxxx for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/xxxx.xxxx/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/xxxx.xxxx/privkey.pem
   Your cert will expire on 2018-12-01. 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

成功したぽい

証明書チェックのリクエストだけhttpsへのリダイレクトを避ける

Nginxの設定でhttpsへリダイレクトかけているが、 Let's encryptの証明チェックはhttpでアクセスしてくるぽいので、 httpでリクエストがくるときだけリダイレクトをかけない設定をしてみる。

こんな感じですかね。

server {
  listen 80;
  server_name xxxx.xxxx;

  location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
  }

  return 301 https://$host$request_uri;
}

server {
  listen 443;

(略)

自動更新の設定

自動更新をするにはcronを使用

cron(クーロン)とは

cron の設定ガイド

初めてcronを使用。 自動でジョブを実行してくれるデーモンプロセスらしい。 日時を指定してシェルスクリプトを実行してくれる気が効くやつなのかな。

CentOS7では以下のコマンドで起動確認ができる。

$ systemctl status crond

● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 火 2018-08-28 06:07:42 UTC; 4 days ago
 Main PID: 313 (crond)
   CGroup: /system.slice/crond.service
           └─313 /usr/sbin/crond -n

Let's encryptのcronの設定は以下のように実施する。

$ sudo vi /etc/cron.d/letsencrypt

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

これは毎月1日の4:00に更新する例。 証明書の更新後、Nginxをリロードして反映することを忘れずに。

とのこと。

備考

他の人の記事を参考にさせていただいて見よう見まねで設定しただけなので、 12月1日まで本当に証明書が自動更新されているかわからない。。。 ユーザーのままでnginxのrestartをsudoで実行しないとパスワード聞かれるけど、cronで再起動してくれるのか不安

参考記事