Let’s Encryptでワイルドカード証明書が作成できるようになったので、試しに作成してみました。
参考にしたのは下記サイトとなります。
https://laboradian.com/use-wildcard-with-letsencrypt/
事の発端
長らく使用していたGMO VPSを解約し、GMO Conohaへサーバ引っ越しを行っています。
引っ越し理由は、OSがCentos6なのでそろそろCentos7に乗り換えたい!というのと、どうせお金突っ込むなら、かわいい女の子に突っ込みたい!となります。
で、今回はせっかくなら全部Dockerで作ってしまえということで、現在利用しているjenkinsやらgitbucketやらをDockerで構築し、バーチャルホストで運営することにしました。
サブドメインがたくさんできた
上記の理由で、「xxx.sheltie-garage.xyz」「yyy.sheltie-garage.xyz」というようにたくさんのサブドメインが出来てしまったため、一元管理したいということで、今回はワイルドカード証明書を作ってみようということになりました
ここから作業内容
実施環境は「CentOS Linux release 7.5.1804 (Core)」です
まずyumでcertbotをインストールします
# yum install certbot
Installed: certbot.noarch 0:0.34.2-3.el7
証明書を作成します
certbot certonly --manual \ --preferred-challenges dns-01 \ --server https://acme-v02.api.letsencrypt.org/directory \ -m メールアドレス \ -d sheltie-garage.xyz \ -d *.sheltie-garage.xyz
--------------------------------------------------- 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: 利用規約に同意するかの問い合わせ
--------------------------------------------------- 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: 暗号化に関するメールを受け取るかの選択らしい。 自分はNを入力
--------------------------------------------------- NOTE: The IP of this machine will be publicly logged as having requested this certificate. If you're running certbot in manual mode on a machine that is not your server, please ensure you're okay with that. Are you OK with your IP being logged? --------------------------------------------------- (Y)es/(N)o: このコマンドを実行しているマシンのIPアドレスを公開ログへ保存してよいかの確認。 Nを選ぶとコマンドが終了するため、証明書を発行する場合、Yを選択するしかありません
--------------------------------------------------- Please deploy a DNS TXT record under the name _acme-challenge.sheltie-garage.xyz with the following value: ~ ランダムな文字列 ~ Before continuing, verify the record is deployed. --------------------------------------------------- Press Enter to Continue DNSレコードへ以下のランダム文字列を設定せよ と表示されるので、DNSレコードを設定します ターミナルでの処理は、ここで一度中断してDNSの設定を行います。
自分はお名前.comを利用しているため、お名前.comのDNSレコード編集画面より以下のように設定を行いました。

ほかの方の事例を見ると、DNSレコードの変更が反映されるまで3時間ほどかかったとのことで、自分もターミナルを3時間ほど開きっぱなしで放置してから処理を続行しました。
参考:
https://def-4.com/certbot-wildcard-certificate/
Press Enter to Continue Waiting for verification… Resetting dropped connection: acme-v02.api.letsencrypt.org Resetting dropped connection: acme-v02.api.letsencrypt.org Cleaning up challenges IMPORTANT NOTES: Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/sheltie-garage.xyz/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/sheltie-garage.xyz/privkey.pem Your cert will expire on 2019-10-03. 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
以上で、証明書の作成は完了です。
Nginxに適用する
作成した証明書をNginxに適用します
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub.sheltie-garage.xyz;
server_tokens off;
ssl on;
add_header Strict-Transport-Security "max-age=31536000" always;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!ADH:!AECDH:!MD5;";
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4;
root /var/www/html;
index index.html index.php;
ssl_certificate /etc/letsencrypt/live/sheltie-garage.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sheltie-garage.xyz/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/sheltie-garage.xyz/chain.pem;
}
Nginxの設定部分の抜粋です。letsEncryptで取得したSSL証明書を設定します。
以上
これでLet’s Encryptのワイルドカード証明書が取得できました。
サブドメインすべてで利用できるのは便利ですが、certbot-renewコマンドなどで問題(自動更新ができない?)もあるそうなので、しばらく様子見しながら利用しようと思います