Let’s Encrypt のサブドメインの証明書取得

無料のLetsEncryptはとても素晴らしいサービスです。
すでにドメインの証明書を作成している状態で、サブドメインを追加したいというときに証明書の取得方法について。
サブドメインを追加する場合は、追加で証明書をが必要になる。
サブドメインも含めてドメインとしての証明書を発行する方法もあるが、ドメインのtxtレコードに鍵ファイルを登録する運用となる。証明書の期限である90日毎の証明書の更新で、ドメインのtxtレコードの登録も更新する必要があるため手動での更新作業が必要になってしまう。サブドメインの数が限られる運用の場合は自動更新が可能な以下の方法がおすすめ。

サブドメインをDNSレコードに追加

ドメインを管理しているベンダで、DNSレコードにサブドメインを追加する。操作方法は、各サービスを確認ください。この登録を行っていないと、以下の証明書発行でエラーが発生します。

登録まで少し時間がかかる場合があります。
dig sub2.example.com コマンドで、追加したサブドメインを検索して登録内容が表示されればOK。

certbot でサブドメインを追加して証明書を発行

まずは、現在の状況を確認する。

sudo certbot certificates

Domains に現在設定されているドメイン・サブドメインが表示される。それを踏まえて、以下のコマンドで、sub2.exsample.com 等を追加していく。

sudo certbot certonly --force-renew --cert-name example.com -d example.com -d sub1.exsample.com -d sub2.exsample.com 

すると、以下のような確認が出てくるので選択する。nginxでserverを追加するのであれば、「1」で良い。

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):

次に、以下の確認が出てくるので「U」で更新を行う

Did you intend to make this change?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(U)pdate certificate/(C)ancel:

Successfully received certificate が出てきたらOK。
再度、sudo certbot certificates で追加されたドメインを確認する。
nginx 等のWebサーバで、生成された鍵ファイルを登録する。
必要に応じて、nginx 等を restart させる。

Xserver が お友達紹介割引 で申込みが可能になります。

コメント

タイトルとURLをコピーしました