外部の別サーバでLets’s Encrypt証明書を発行する

内輪でちょっと使うだけとか、検証のためとかでLet’s Encryptでお手軽にSSLを使いたいけど、本番環境なので色々インストールするのは気が引ける…という場合に、適当な開発サーバにLet’s Encryptをぶちこんで、証明書だけそっちで発行して使いたい、という場合。結構あるんじゃないでしょうかね、こういうニーズって。
というわけでやってみました。

構成と関係

・本番サーバ prod … SSL証明書のインストール先。
・発行サーバ issue … Let’s Encryptをインストールして、本番サーバ用の証明書を作成する。

Acmeチャレンジの仕様を理解する

Let’s Encryptは、Acme標準形式のChallengeで、そのドメインが制御下に置かれていることを確認します。具体的に言うと、
http://hogehoge.com/.well-known/acme-challenge/P9wK8ZogGk994V9cUZMYekWj6zKyw2OWJFTOJ1gBopo
みたいなURLが動的に生成されて、そこにアクセスして決められたキーが返ってくることを確認する、という流れです。この文字列は自動生成で、要求キーも自動生成です。

普通に、発行も設置も自分の中でやる場合は、Lets’s EncryptのCertbot-autoがうまいこと自動でやってくれるんですが、発行と設置が別サーバだとそうはいきません。
たとえ.htaccessなどでhttp://hogehoge.com/.well-known/acme-challenge で何が返ってきても200になるように制御したとしても、発行サーバで
certbot-auto certonly –standalone -d hogehoge.com
とやっても、chalengeエラーになります。

エラー例

[root@issue ~]# certbot-auto certonly --standalone -d hogehoge.com
Plugins selected: Authenticator standalone, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for hogehoge.com
Waiting for verification...
Challenge failed for domain hogehoge.com
http-01 challenge for hogehoge.com
Cleaning up challenges
Some challenges have failed.

IMPORTANT NOTES:
- The following errors were reported by the server:

Domain: hogehoge.com
Type:   unauthorized
Detail: Invalid response from
http://hogehoge.com/.well-known/acme-challenge/zzzzzzzzzgGk994V9cUZMYekWj6zKyw2OWJFzzzzzzzzz

別サーバで発行する場合は手動発行をする

そこで、手動で発行します。

[root@issue ~]# certbot-auto certonly --manual
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel):

ドメイン名を聞かれるので答えます。

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): hogehoge.com
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for hogehoge.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

注:このマシンのIPは、この証明書を要求したものとしてパブリックに記録されます。 サーバーではないマシンでcertbotを手動モードで実行している場合は、それで問題ないことを確認してください。
IPが記録されていても大丈夫ですか?

と聞かれているので、甘んじて受け入れます。Y。

そうすると、Acmeチャレンジの要求と回答を教えてくれます。

広告

Create a file containing just this data:

X2XU9yxxxxxxxxxxxM0sbc-l8JoEbwUyxxxxxxxxxxxxxxxhxFKAZTJDNJFjC2T-1WZkpoDG8xxxxxxxxxxxx

And make it available on your web server at this URL:

http://hogehoge.com/.well-known/acme-challenge/X2XU9zzzzzzzRyoGS5Fcq0M0sbzzzzzzzzz

ここで一旦開発サーバ(発行サーバ)での作業をストップして、最初の「Create a file containing just this data:」の後にある文字列を、設置先サーバの/.well-known/acme-challenge/ ディレクトリの下にテキストファイルで設置します。

設置先サーバにて

[dacelo@prod ~]$ mkidr -p /DocumentRoot/.well-known/acme-challenge/
$ echo "X2XU9yxxxxxxxxxxxM0sbc-l8JoEbwUyxxxxxxxxxxxxxxxhxFKAZTJDNJFjC2T-1WZkpoDG8xxxxxxxxxxxx" > X2XU9zzzzzzzRyoGS5Fcq0M0sbzzzzzzzzz

発行サーバに戻って、Enterを押して処理を進めます。

Press Enter to Continue
Waiting for verification...
Cleaning up challenges

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

おめでとう!発行が完了しました。
あとは、
/etc/letsencrypt/live/hogehoge.com/(/etc/letsencrypt/archive/hogehoge.com/のシンボリックリンク)
の下にできている各証明書

-rw-rw-r-- 1 root root 1923 10月 21 15:13 2019 cert1.pem
-rw-rw-r-- 1 root root 1647 10月 21 15:13 2019 chain1.pem
-rw-rw-r-- 1 root root 3570 10月 21 15:13 2019 fullchain1.pem
-rw------- 1 root root 1704 10月 21 15:13 2019 privkey1.pem

を、設置先サーバに格納してOKです。

About: dacelo


Leave a Reply

Your email address will not be published. Required fields are marked *