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

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

概要

環境:

  • Ubuntu 22.04 / Apache2 / Wordpress
  • ホスト名: www.example.u-tokyo.ac.jp

今回はcertbotのapache2モジュールを利用します。apache2以外にも nginx, webroot, standalone などのモジュールがあるので、環境に合わせて利用します。

HTTP-01チャレンジの場合は80番ポートが外部からアクセスできる必要があるので、FWを設定しておきます。

ACME申請用アプリで生成されるコマンド

user@www:~$ sudo certbot --apache --server https://secomtrust-acme.com/acme/ \
  --eab-hmac-key [secret] --eab-kid [kid] \
  -d www.example.u-tokyo.ac.jp \
  --key-type rsa

※ このコマンド例ではcertbotが設定ファイルなども書き換えを行いますが、以下の手順ではそれを行わないため、certonly オプションで証明書の生成のみを行います。

環境確認

user@www:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.5 LTS
Release:        22.04
Codename:       jammy
user@www:~$ apt info apache2
Package: apache2
Version: 2.4.52-1ubuntu4.16
Priority: optional
Section: web
Origin: Ubuntu

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

user@www:~$ sudo apt install certbot python3-certbot-apache
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています... 完了
...
libc-bin (2.35-0ubuntu3.11) のトリガを処理しています ...

--dry-run で動作確認

certonlyオプションがついていることに注意します。

user@www:~$ sudo certbot certonly --dry-run --apache --server https://secomtrust-acme.com/acme/ \
  --eab-hmac-key [secret] --eab-kid [kid] -d www.example.u-tokyo.ac.jp --key-type rsa
# /var/log/letsencrypt/letsencrypt.log のログを見ること

実際のコマンド実行

certonlyで、証明書生成だけにしています。

  • 生成される証明書ファイル: /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem
  • 生成される証明書の鍵ファイル: /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/privkey.pem
user@www:~$ sudo certbot certonly --apache --server https://secomtrust-acme.com/acme/ \
  --eab-hmac-key [secret] --eab-kid [kid] -d www.example.u-tokyo.ac.jp --key-type rsa
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for www.example.u-tokyo.ac.jp
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/privkey.pem
This certificate expires on 2026-02-03.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

生成された証明書の確認

root@www:~# openssl x509 -in /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            46:dd:af:**:**:**:**:**:**:ff:9a:18:13:98:b8:d9
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = JP, O = "SECOM Trust Systems CO.,LTD.", CN = NII Open Domain CA - G7 RSA
        Validity
            Not Before: Nov 6 04:45:16 2025 GMT
            Not After : Feb 3 04:45:15 2026 GMT
        Subject: C = JP, ST = "Tokyo, C=JP", L = Bunkyo-ku, O = The University of Tokyo, CN =
www.example.u-tokyo.ac.jp
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:f5:47:88:06:ad:3a:aa:c0:87:0b:ef:de:3c:4c:
                    64:31:af:54:ad:03:bd:96:f4:e8:95:d1:de:a9:05:
                    0f:c3:d8:77:d6:3d:41:59:f9:5d:ef:4d:59:8a:d4:
                    24:0b:74:02:c8:dd:7a:b3:2a:be:1f:14:22:30:88:
                    17:e4:7d:1a:4b:f3:d5:a5:ec:48:c3:c9:70:6b:45:
...
       51:de:49:81:b5:71:f9:4b:41:88:a5:5d:39:1b:a6:bf:57:6a:
       d2:54:5c:f4:f2:09:9b:b3:5a:07:3c:87:12:d2:14:17:86:e0:
       94:ce:ec:83

apache2の証明書の変更

# HTTPSの設定ファイルの確認
user@www:~$ ls -l /etc/apache2/sites-available/default-ssl.conf
-rw-r--r-- 1 root root 8090 11月 6 14:04 /etc/apache2/sites-available/default-ssl.conf

# HTTPSで使用するSSL証明書の変更
# 前の証明書ファイルは /old/path/to/www.example.u-tokyo.ac.jp.2025-2.cer
# 前の証明書の鍵ファイルは /old/path/to/www.example/www.example.u-tokyo.ac.jp.2025-2.key
# 古い証明書を参照している箇所をコメントアウトして、certbotが生成する証明書を参照する設定にする
user@www:~$ sudo vi /etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    ServerName www.example.u-tokyo.ac.jp
...
    #SSLCertificateFile    /old/path/to/www.example.u-tokyo.ac.jp.2025-2.cer
    #SSLCertificateKeyFile /old/path/to/www.example/www.example.u-tokyo.ac.jp.2025-2.key
    SSLCertificateFile     /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/privkey.pem
  </VirtualHost>
</IfModule>

設定の反映と確認

# 設定の反映
user@www:~$ sudo systemctl reload apache2

# 証明書の反映の確認
# 有効期限が作成したSSL証明書になっていることを確認した
user@www:~$ openssl s_client -connect localhost:443 -showcerts
CONNECTED(00000003)
Can't use SSL_get_servername
depth=2 C = JP, O = "SECOM Trust Systems CO.,LTD.", OU = Security Communication RootCA2
verify return:1
depth=1 C = JP, O = "SECOM Trust Systems CO.,LTD.", CN = NII Open Domain CA - G7 RSA
verify return:1
depth=0 C = JP, ST = "Tokyo, C=JP", L = Bunkyo-ku, O = The University of Tokyo, CN =
www.example.u-tokyo.ac.jp
verify return:1
---
Certificate chain
 0 s:C = JP, ST = "Tokyo, C=JP", L = Bunkyo-ku, O = The University of Tokyo, CN =
www.example.u-tokyo.ac.jp
   i:C = JP, O = "SECOM Trust Systems CO.,LTD.", CN = NII Open Domain CA - G7 RSA
   a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA256
   v:NotBefore: Nov 6 04:45:16 2025 GMT; NotAfter: Feb 3 04:45:15 2026 GMT
...

自動更新の確認

# 自動更新のタイマーを確認
user@www:~$ sudo systemctl list-timers | grep certbot
Fri 2025-11-07 08:20:32 JST 18h left    n/a   n/a certbot.timer    certbot.service

# 実際に更新処理が動いた時の動作を --dry-runで確認
user@www:~$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.example.u-tokyo.ac.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for www.example.u-tokyo.ac.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

証明書が更新された時のapache2への反映は自動化されないため、証明書更新時のhookでapache2をreloadするスクリプトを仕掛けます。

# 証明書が更新された時の処理(hook)のスクリプトを書く
user@www:~$ sudo vi /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
#!/bin/bash
systemctl reload apache2

# スクリプトの実行テスト
user@www:~$ sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh

# 証明書更新処理のテスト
user@www:~$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.example.u-tokyo.ac.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for www.example.u-tokyo.ac.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/www.example.u-tokyo.ac.jp/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# ログから、更新処理が走った時の動作を確認
user@www:~$ sudo cat /var/log/letsencrypt/letsencrypt.log | grep -i reload
2025-11-06 14:18:34,618:INFO:certbot._internal.hooks:Dry run: skipping deploy hook command:
/etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
# 今回は証明書の更新がないので、スキップされていることを確認した