Let's EncryptのSSL証明書を、wwwあり・なし両方で取得する方法

目次

 

こんにちは、ごえです。

 Webサイトを運用していると、次のように wwwあり・なしの両方のドメインでアクセスされることがあります。

example.com
www.example.com

どちらでアクセスしてもHTTPS通信できるようにするには、
Let's Encryptで両方のドメインを含む証明書を取得する必要があります。

今回は certbotを使ってwwwあり・なしの2WAY証明書を取得する方法を紹介します。

今回の検証環境は以下の通りです。

項目 バージョン
OS Amazon Linux 2023
Apache 2.4.66
certbot 2.6.0

 

 今回は以下のドメインで証明書を取得するという想定です。 

example.com
www.example.com

※ 実際に実行する際は、環境に合わせてドメインを置き換えてください。

certbot設定

certbot は、SSL証明書を自動取得・設定・更新することができるツールです。
certbotを利用することで、証明書取得の申請や、ドメインの認証を自動化することができます。

今回は、Let's EncryptのSSL証明書取得にcertbotを使用します。

まず最初にcertbot をインストールします。

 yum install certbot 

 また、今回はApache環境で証明書を取得するため、Apacheプラグインもインストールします。 

yum install python3-certbot-apache

このパッケージをインストールすることで、certbotがApacheの設定を利用して
ドメイン認証(HTTP-01 challenge)を自動で行えるようになります。

 certbotで複数ドメインの証明書を取得する 

Let's Encryptでは SAN証明書(複数ドメイン証明書) を取得できます。
certbotでは -d オプションで複数ドメインを指定します。

以下のコマンドを実行します。

 certbot --apache certonly -d example.com -d www.example.com 


実行結果は以下のようになります(対話型で入力を求められます。)

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): admin@example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.6-August-18-2025.pdf. You must agree
in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, 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
Account registered.
Requesting a certificate for example.com and www.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2026-06-04.
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

【対話部分】
 Enter email address (used・・・
⇒メールアドレスを入力します

 Please read the Terms of Service at ・・・
⇒利用規約に同意します

 Would you be willing to share your email address... 
⇒メール共有をするかどうか聞かれます。Noを選択しても問題ありません。

証明書取得完了

問題が無ければ以下のように証明書の保存場所が表示されます。
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem 

Apacheの設定変更

証明書を取得出来たら、Apacheの設定ファイルに取得した証明書ファイルのパスを記載します。

<VirtualHost *:443>

    ServerAdmin serveradmin@example.com
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /var/www/vhosts/example.com/httpdocs/

    SSLEngine on

    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

</VirtualHost>

【ポイント】
wwwありなし両方を記載
ServerName → wwwドメイン
ServerAlias → wwwなしドメイン

以下のコマンドでApacheを再起動し、設定を反映させます。

systemctl status httpd 
systemctl restart httpd
systemctl status httpd

 

動作確認

https://example.com
https://www.example.com

両方で証明書が適用されていることを確認します。

おわりに

certbotは無料で簡単にHTTPS化できるツールです。
ぜひ試してみてください。

最後までお読みいただきありがとうございました。