2019/07/12(金)FreeBSD11/FreeBSD12 にて無償利用可能なサーバ証明書 Let's Encrypt を使う

2019/07/12 17:39 サーバ運営・管理
他のFreeBSD バージョンでも恐らく使用可能です。
Let's Encrypt は、有効期間90日(自動延長可能)な無償サーバ証明書です。
FreeBSD におけるやり方について、余り情報が書かれていないので、ここで提示しておきます。

これを使うには、先ずは使用するサーバにて、管理ツールのようなものをインストールします:
FreeBSD Ports においては、 securiry/py-certbot をインストールします。
#Python を使用したスクリプトなので、依存関係で Python 本体と、動作に必要な python モジュールが
#インストールされます。

インストールが終わったら、Apache や nginx を一旦停止して、
# certbot certonly --standalone -d 《FQDN名》-m 《通知電子メールアドレス》
# certbot certonly --standalone --non-interactive --agree-tos --keep --expand --email 《通知電子メールアドレス》 --no-eff-email --domains 《FQDN名》
のようにコマンドを入力します。
#このスクリプトが一時的に http サーバになるため、ポート番号が競合するからのようです。

《FQDN名》は、証明書発行申請するときのコモン名。例えば https://server.example.com/ のサーバ証明書が欲しければ、 ここに server.example.com と記述します。

《通知電子メールアドレス》は、何かある場合に連絡が入る電子メールアドレスを指定します。
実在する電子メールアドレスを指定します。
証明書の期限が間近になった場合などに連絡が入るようです。

コマンドの実行が始まると、先ず、下記のメッセージが表示されます:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
使用条件・契約条件を許諾するか否かの問いです。Agree の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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
次に、各種の連絡を電子メールで送るけれど、本当に良いか? という趣旨の問いです。
これも Yes の Y を入力します。
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for server.example.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/usr/local/etc/letsencrypt/live/server.example.com/fullchain.pem
Your key file has been saved at:
/usr/local/etc/letsencrypt/live/server.example.com/privkey.pem
Your cert will expire on 2019-10-10. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /usr/local/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
上記で、'Congratulation' のメッセージが含まれていたら、作成は成功です。
正直なところ、少し判り難いです。
上記例では有効期限は 2019/10/10 だと言っています。

失敗しているような場合は、再度、
# certbot certonly --standalone -d 《FQDN名》-m 《通知電子メールアドレス》
# certbot certonly --standalone --non-interactive --agree-tos --keep --expand --email 《通知電子メールアドレス》 --no-eff-email --domains 《FQDN名》
上記のコマンドを入力してみましょう。
但し、FQDN名がDNSで正引き出来ないと、上手く行かないみたいです。

Apache には以下の要領で設定します (要所を抜粋):
SSLCACertificateFile /usr/local/etc/letsencrypt/live/server.example.com/chain.pem
SSLCertificateFile /usr/local/etc/letsencrypt/live/server.example.com/cert.pem
SSLCertificateKeyFile /usr/local/etc/letsencrypt/live/server.example.com/privkey.pem
 'server.example.com' の部分を、使用するFQDN に合わせて変更します。

最後に自動更新させるには、以下を、crontab に追加します:
0 6,21 * * * /usr/local/bin/certbot renew --agree-tos --webroot -w
/home/webroot/server.example.com/public --renew-by-default
&& /usr/local/etc/rc.d/apache2 restart
#表示の便宜上3行に分けているが、実際に適用の際は半角スペースで区切って1行にすること。
 -w のあとのフルパスは、該当 FQDN におけるドキュメントルート絶対パスを指定します。

1日2回の更新チェックが推奨されているようです。しかしながら、実用上、月に1回でもよいと思います。

※ certbot certonly コマンドが例示だと上手く動作しなくなっていたので、動作確認出来た内容に修正。〔2021/09/13〕