2021/02/20作成, 2020/02/19更新

Let's Encrypt + nginx で SSLの証明書を自動管理する

公式サイト: https://letsencrypt.org/ja/

Let's Encryptとは

無償で利用できるSSLの証明書基地局です。最近ではスマホから通信する先はhttpsにしないとならないなどの制約も出始めており、SSLの証明書はもはや必須となりつつあります。しかしSSLの証明書を取得するには結構なお値段がするのですが、Let's Encryptという無償の証明書基地局が使えるようになりました。そこでLet's Encrypt を使って証明書を取得してみます。

パッケージのインストール

公式サイトを見ると certbot というコマンドを使うことを推奨されています。FreeBSDにもcertbotがあるので、それを使います。

py36-certbot-nginxかpy37-certbot-nginxをインストールします。py36とかpy37はpythonのバージョンです。すでにpythonがインストールされているならバージョンを合わせると良いですが、気にせず新しいものを使って構いません。

pkg search cartbot
pkg install py37-certbot-nginx

証明書を取得する

certbotというコマンドで証明書を取得できます。certbox に --nginx オプションを渡すと nginx 用の設定ファイルを作成してくれます。certbot --nginx -d ドメイン名 とすることで証明書を首都機できます。例えばこのウェブサイトなら docs.isapon.com になります。

コマンドを実行するとメールアドレスの入力とライセンスに同意するかを聞かれます。

certbot --nginx -d DOMAINNAME
# メールアドレスを聞かれます
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Enter email address (used for urgent notices and lost key recovery) (Enter 'c' to cancel): foo@bar.bar.com
# ライセンスの確認があります(Aを入力して同意します)
-------------------------------------------------------------------------------
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: 

これで /usr/local/etc/letsencrypt 以下にファイルが生成されます。

nginx側の設定

証明書は /usr/local/etc/letsencrypt/live/DOMAINNAME でアクセスできます。nginxからこのファイルを参照するように指定し、/usr/local/etc/letsencrypt/options-ssl-nginx.conf を include するようにします。

/usr/local/etc/nginx/nginx.conf

server{
  ssl_certificate     /usr/local/etc/letsencrypt/live/docs.isapon.com/fullchain.pem;
  ssl_certificate_key /usr/local/etc/letsencrypt/live/docs.isapon.com/privkey.pem;
  include             /usr/local/etc/letsencrypt/options-ssl-nginx.conf;

証明書の自動更新

Let's Encrypto の証明書は短期間で期限が切れます(おそらく使用されていない証明書をなるべく早く破棄するためかと思われます)。そこで定期的に certbot renew で証明書を更新するのですが、FreeBSDの場合はウィークリーで自動実行できます。periodic.conf を更新します。

/etc/periodic.conf

weekly_certbot_enable="YES"

以上で終了です。certbot --nginx を指定したときにnginxを再起動するする設定も出来上がっているので、あとは何も気にする必要はありません。

ワイルドカード編

certbot にワイルドカードモードでの証明書の取得を依頼します。ここで気を付けてほしいのがドメイン名の "*.isapon.com" です。ダブルコーテーションで囲っておくのがミソです。そうしないとシェルによってはファイル名を展開するものかと思ってファイルを探しに行きます。

certbot certonly --manual \
	--server https://acme-v02.api.letsencrypt.org/directory \
	--preferred-challenges dns \
	-d "*.ドメイン名" -d トップドメイン名 \
	-m メールアドレス \
	--agree-tos \
	--manual-public-ip-logging-ok
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for isapon.com
dns-01 challenge for isapon.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.ドメイン名 with the following value:

認証用のコード

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

ここでDNSのTXTレコードに「認証用のコード」を追加する。即座に反映されるわけでもないので、念のために10秒ほど待ち、時間がたったらENTERを押す。

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /usr/local/etc/letsencrypt/live/ドメイン名/fullchain.pem
   Your key file has been saved at:
   /usr/local/etc/letsencrypt/live/ドメイン名/privkey.pem
   Your cert will expire on YYYY-MM-DD. 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"
 - 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

あとは非ワイルドカードの時と同じ。

INTEL 小型ベアボーン BOXNUC8I5BEH
共有だけじゃなくていろいろとサーバーに仕事をさせたいならまずは場所を取らない小型PCを使ってみるのをお勧め。
Amazon.comのページを別ウィンドウで開きます
Synology DiskStation DS918+
ホットスワップ4ベイのNAS。RAIDも使えるし運用も簡単なのでおすすめ。
Amazon.comのページを別ウィンドウで開きます