OpenWrt で Let's Encrypt (with acme.sh)

acme.sh を使った。

wget なり、curl なりで acme.sh をダウンロードして、あとは acme.sh コマンドを駆使してお任せ。


./acme.sh --install
It is recommended to install socat first.
We use socat for standalone server if you use standalone mode.
If you don't use standalone mode, just ignore this warning.
Installing to /root/.acme.sh
Installed to /root/.acme.sh/acme.sh
No profile is found, you will need to go into /root/.acme.sh to use acme.sh
Installing cron job
OK

socat なるものが必要とされるが、スタンドアローン・モードに限った話であり、スタンドアローン・モードで使う予定はないので問題なし。


cd .acme.sh
./acme.sh --issue -d scaredeer.com -d www.scaredeer.com -w /mnt/data/www/scaredeer.com --accountemail scaredeer@scaredeer.com
Registering account
Registered
ACCOUNT_THUMBPRINT='(省略)'
Creating domain key
The domain key is here: /root/.acme.sh/scaredeer.com/scaredeer.com.key
Multi domain='DNS:scaredeer.com,DNS:www.scaredeer.com'
Getting domain auth token for each domain
Getting webroot for domain='scaredeer.com'
Getting new-authz for domain='scaredeer.com'
The new-authz request is ok.
Getting webroot for domain='www.scaredeer.com'
Getting new-authz for domain='www.scaredeer.com'
The new-authz request is ok.
Verifying: scaredeer.com
Success
Verifying: www.scaredeer.com
Success
Verify finished, start to sign.
Cert success.
-----BEGIN CERTIFICATE-----
(省略)
-----END CERTIFICATE-----
Your cert is in  /root/.acme.sh/scaredeer.com/scaredeer.com.cer 
Your cert key is in  /root/.acme.sh/scaredeer.com/scaredeer.com.key 
The intermediate CA cert is in  /root/.acme.sh/scaredeer.com/ca.cer 
And the full chain certs is there:  /root/.acme.sh/scaredeer.com/fullchain.cer

以上で、certification の issue が終わる(issue の段階では nginx.conf を更新・リロードしないこと。従来の http でのアクセスが正常にできる状態でないと、acme-challenge に失敗してしまう)。あとは発行した証明書を nginx 用にインストール。インストール先としては、nginx.conf で指定する予定の path(未だ存在しない場合は予め mkdir しておく)を使った。


./acme.sh --install-cert -d scaredeer.com -d www.scaredeer.com --key-file /mnt/data/www/scaredeer.com/cert/cert.key --fullchain-file /mnt/data/www/scaredeer.com/cert/cert.pem --reloadcmd "service nginx reload"
Installing key to:/mnt/data/www/scaredeer.com/cert/cert.key
Installing full chain to:/mnt/data/www/scaredeer.com/cert/cert.pem
Run reload cmd: service nginx reload
./acme.sh: eval: line 1: service: not found
Reload error for :

acme.sh が service コマンドを使った nginx の reload まで面倒を見てくれているが、何故かエラーになっている。自分で手動で service nginx reload すればいいだけなので、別に問題ない、はず。ただ、acme.sh は証明書の自動更新まで面倒を見てくれるようで、自動更新にこの --reloadcmd が関係している可能性もあり、今後自動更新が無事に行われるかどうかは、その時になってみないと不明。

Please take care: The reloadcmd is very important. The cert can be automatically renewed, but, without a correct 'reloadcmd' the cert may not be flushed to your server(like nginx or apache), then your website will not be able to show renewed cert in 60 days.

ともかく、(nginx.conf を更新した上で)service nginx reload して、実際に HTTPS でアクセスできるようになっていれば、無事終了である。

追記:結局、自動更新は上手く機能していなかった

やはり service nginx reload が(僕の OpenWrt 環境では)acme.sh コマンド経由だと常にエラーになる影響で、証明書自体の自動更新はされていたものの、nginx が再起動されず、新しい証明書が反映されないという事態に陥っていた。今回は、手動で OpenWrt 自体を再起動したが、ひとまずは、service nginx reload コマンドを直接 cron ジョブとして毎日一回動かすことにして、様子見することにする。

再追記:結局、nginx のリロードでは、証明証が読み込み直されていなかった

service nginx reload ではせっかく更新された証明書を読み込み直していないようだ。仕方ないので、毎月一回、cron ジョブでルーター自体を再起動(reboot)することにする。

# Monday 01:11
11 1 * * 1 /root/.acme.sh/acme.sh --cron --home /root/.acme.sh > /dev/null
# The 5th day of Month, 05:55
55 5 5 * * reboot

コメント

このブログの人気の投稿

シークエンスパパともの先見の明

Mac → Mac のメールアカウントの移行

OpenWrt での Wi-Fi 設定