東京大学情報基盤センターPKI

参考)UPKI-certbot-HTTP-01チャレンジ対応(複数VirtualHost設定例)

概要

ApacheのVirtualHost構成で、複数ホスト名の証明書をcertbotで発行する例です。HTTP-01チャレンジを使います。

  • ホスト名ごとにDocumentRootを用意します。
  • ホスト名ごとに証明書を発行します。

対象ドメイン

この手順で作成する証明書は以下の3つです。

  • uatest01.example.u-tokyo.ac.jp
  • uatest02.example.u-tokyo.ac.jp
  • uatest03.example.u-tokyo.ac.jp

前提

  • ApacheのVirtualHostにより、一つのホストで複数のドメインのWebページを運用しています。
  • 各ホスト名のDocumentRootはHTTPで参照が可能な設定です。(HTTP-01チャレンジであるため、HTTPで参照できる必要があります)
  • EAB情報(--eab-hmac-key / --eab-kid)が用意されています。

事前に準備するファイルなど

  • /etc/apache2/sites-available/000-default.conf(VirtualHost設定ファイル)
  • /var/www/html/test01/(uatest01のDocumentRoot)
  • /var/www/html/test02/(uatest02のDocumentRoot)
  • /var/www/html/test03/(uatest03のDocumentRoot)

複数EAB Credentialでのポイント

  • 既存アカウントがある環境で通常の certbot register を行うと重複エラーになります。
  • このため、証明書の発行(certbot certonly)を実行する前に、明示的にアカウントの登録(certbot register)を実施します。通常は証明書の発行時に同時に実施されます。
  • 一時ディレクトリでアカウントを作成し、アカウントIDのディレクトリを /etc/letsencrypt にコピーして併用します。
  • オプション--account で指定するアカウントIDは、一時ディレクトリ(例: ./temp-uatest01/accounts/secomtrust-acme.com/acme/)に作成されるディレクトリ名です。 このディレクトリを /etc/letsencrypt/accounts/secomtrust-acme.com/acme/ にコピーします。 ディレクトリ名がアカウントIDとなります。
  • アカウントIDは英小文字の16進数で32文字のディレクトリ名になります(例: 3fd4bc2a5bffe8874c61db822c8d25e4)。
  • 必要に応じて certbot certificates で追加された証明書を確認します。

VirtualHostの準備

この手順では、一時ディレクトリを /root 配下に置くため、rootアカウントで作業しています。

DocumentRootの作成

root@www:~# mkdir -p /var/www/html/test01 /var/www/html/test02 /var/www/html/test03
root@www:~# echo test01 | tee /var/www/html/test01/index.html
root@www:~# echo test02 | tee /var/www/html/test02/index.html
root@www:~# echo test03 | tee /var/www/html/test03/index.html

VirtualHost設定例

以下は設定ファイルの抜粋例です(編集対象: /etc/apache2/sites-available/000-default.conf)。

# /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
  ServerName uatest01.example.u-tokyo.ac.jp
  DocumentRoot /var/www/html/test01
</VirtualHost>

<VirtualHost *:80>
  ServerName uatest02.example.u-tokyo.ac.jp
  DocumentRoot /var/www/html/test02
</VirtualHost>

<VirtualHost *:80>
  ServerName uatest03.example.u-tokyo.ac.jp
  DocumentRoot /var/www/html/test03
</VirtualHost>

root@www:~# systemctl reload apache2

ACMEアカウントの作成(EAB)

ホスト名ごとにアカウントを分ける場合は、作業ディレクトリも分けて登録します。

root@www:~# mkdir -p ./temp-uatest01
root@www:~# certbot register \
  --config-dir ./temp-uatest01 \
  --work-dir   ./temp-uatest01 \
  --logs-dir   ./temp-uatest01 \
  --server 'https://secomtrust-acme.com/acme/' \
  --eab-hmac-key [secret] \
  --eab-kid [kid] \
  --agree-tos \
  -m user@example.ac.jp \
  --no-eff-email

作成したアカウント情報を /etc/letsencrypt にコピーして使います。

root@www:~# cp -r ./temp-uatest01/accounts/secomtrust-acme.com/acme/[account-id]/ /etc/letsencrypt/accounts/secomtrust-acme.com/acme/

注意 アカウント情報には秘密鍵が含まれます。特に以下のファイルは漏洩しないように厳重に管理してください。このため、コピー後の一時ディレクトリは削除しておいた方が安全です。

  • /etc/letsencrypt/accounts/*/acme/*/private_key.json(秘密鍵)
  • /etc/letsencrypt/accounts/*/acme/*/regr.json(登録情報)

証明書の発行

root@www:~# certbot certonly \
  --server https://secomtrust-acme.com/acme/ \
  --account [account-id] \
  --key-type rsa \
  --webroot \
  -w /var/www/html/test01 \
  -d uatest01.example.u-tokyo.ac.jp

test02 / test03 も同様に、-w-d を対応するVirtualHostに合わせて実行します。--account も対応するアカウントIDに合わせて指定します。

  • -d uatest02.example.u-tokyo.ac.jp-w /var/www/html/test02
  • -d uatest03.example.u-tokyo.ac.jp-w /var/www/html/test03

失敗しやすいポイント

  • 指定する -w のパスが存在しないとエラーになります。
  • --account にはアカウントIDを必ず指定します。

最終的に出力されるファイル

証明書と鍵は以下に保存されます。VirtualHostごとにディレクトリが分かれます。

証明書ファイルと鍵ファイルにより、HTTPSの設定を行ってください(本ドキュメントではHTTPSの設定は省略しています)。

  • /etc/letsencrypt/live/uatest01.example.u-tokyo.ac.jp/fullchain.pem
  • /etc/letsencrypt/live/uatest01.example.u-tokyo.ac.jp/privkey.pem
  • /etc/letsencrypt/live/uatest02.example.u-tokyo.ac.jp/fullchain.pem
  • /etc/letsencrypt/live/uatest02.example.u-tokyo.ac.jp/privkey.pem
  • /etc/letsencrypt/live/uatest03.example.u-tokyo.ac.jp/fullchain.pem
  • /etc/letsencrypt/live/uatest03.example.u-tokyo.ac.jp/privkey.pem
  • /etc/letsencrypt/renewal/uatest01.example.u-tokyo.ac.jp.conf
  • /etc/letsencrypt/renewal/uatest02.example.u-tokyo.ac.jp.conf
  • /etc/letsencrypt/renewal/uatest03.example.u-tokyo.ac.jp.conf

certbot certificates の実行例

このコマンドはサーバ上の全証明書が表示されるため、今回の作業対象(uatest01/02/03)以外の証明書も併記されます。

root@www:~# certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: uatest01.example.u-tokyo.ac.jp
    Serial Number: 152f405853a0c918a04443ec0701406f
    Key Type: RSA
    Domains: uatest01.example.u-tokyo.ac.jp
    Expiry Date: 2026-05-18 05:39:54+00:00 (VALID: 88 days)
    Certificate Path: /etc/letsencrypt/live/uatest01.example.u-tokyo.ac.jp/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/uatest01.example.u-tokyo.ac.jp/privkey.pem
  Certificate Name: uatest02.example.u-tokyo.ac.jp
    Serial Number: 4e254af6968de5db1855211e29680173
    Key Type: RSA
    Domains: uatest02.example.u-tokyo.ac.jp
    Expiry Date: 2026-05-18 05:44:05+00:00 (VALID: 88 days)
    Certificate Path: /etc/letsencrypt/live/uatest02.example.u-tokyo.ac.jp/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/uatest02.example.u-tokyo.ac.jp/privkey.pem
  Certificate Name: uatest03.example.u-tokyo.ac.jp
    Serial Number: 6e2eaee353b45feb7d132dc00ec65a4a
    Key Type: RSA
    Domains: uatest03.example.u-tokyo.ac.jp
    Expiry Date: 2026-05-18 05:46:02+00:00 (VALID: 88 days)
    Certificate Path: /etc/letsencrypt/live/uatest03.example.u-tokyo.ac.jp/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/uatest03.example.u-tokyo.ac.jp/privkey.pem
  Certificate Name: www.example.u-tokyo.ac.jp
    Serial Number: 32634eb982a1e3b208709dec9b31db5c
    Key Type: RSA
    Domains: www.example.u-tokyo.ac.jp
    Expiry Date: 2026-05-07 05:47:32+00:00 (VALID: 77 days)
    Certificate Path: /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

補足:使用したcertbotコマンドのオプションについて

certbot register

オプション 概要
--config-dir 設定・アカウント情報の保存先を指定します。EABごとに分けるため一時ディレクトリを指定します。
--work-dir 作業用の一時ディレクトリを指定します。チャレンジ関連の一時ファイルが作成されます。
--logs-dir ログの出力先を指定します。検証用のログを分離して保存できます。
--server 利用するACMEサーバのURLを指定します。UPKIのACMEサーバを指定します。
--eab-hmac-key EABのHMAC鍵を指定します。機密情報です。
--eab-kid EABのKey IDを指定します。発行されたKIDを指定します。
--agree-tos 利用規約への同意を表します。コマンド実行時に表示される確認質問への回答を省略できます。
-m 連絡先メールアドレスを指定します。本ページではサンプル表記です。
--no-eff-email EFFからのメール送付を受け取らない設定です。受け取る場合は --eff-email を指定します。

certbot certonly

オプション 概要
--account 使用するアカウントID(ディレクトリ名)を指定します。複数アカウントを使い分けるために必須です。
--key-type 生成する鍵の種別を指定します。例: rsa
--webroot HTTP-01のwebroot方式を使用します。DocumentRoot配下に検証用ファイルを配置します。
-w webrootのパスを指定します。ドメインごとに対応するDocumentRootを指定します。
-d 発行対象のドメイン名を指定します。1つのドメインごとに発行します。
--server 利用するACMEサーバのURLを指定します。UPKIのACMEサーバを指定します。