AlmaLinux9へTLS証明書(Let's Encrypt)を導入しよう

 Webサーバーで、暗号化通信(HTTPS)の設定は必須ですね。ここでは無償で取得できるLet's EncryptのTLS証明書を使用します。有料のTLS証明書は実在証明として有効ですが、今はEV証明書とLet's Encryptの証明書は詳細に見ないと見分けがつきません。ちょっと残念な状況です。

 Let's Encryptの導入については前回と概ね同様です。

 ここでは次のドメイン名で証明書を取得します。ドメイン名をそれぞれ読み替えてください。

hiroski.com
www.hiroski.com

 加えて、メールサーバー用(mail/mx等)を加えれば十分だと思います。

 

certbotをインストール

 最初に、certbotというLet's EncryptのTLS証明書の取得を行うパッケージをインストールします。CentOS 7時には動作環境がPython 2でしたが、今回はPython 3となります。

$ sudo dnf install certbot python3-certbot-apache

certbotの詳細については本家を参照してください。ただしcertbotのインストールは、Linuxに関してはパッケージ管理のsnapあるいはpipの記載のみです。さくらのVPSのSetup and updateスクリプトでもsnapをインストールするオプションがあります。

"6. Choose how you'd like to run Certbot"よりcertbotコマンドの実行例が記載されています。

 "certbot --apache"を実行すると、certbotはTLS証明書が必要なドメインの検出と、必要な変更を行ってくれます。

定義ファイルを準備し動作を確認します(01-example.conf にhttps(443)ポートの<VirtualHost>ブロックを追加します)。/home/userb/は適宜読み替えてください。

/etc/httpd/domain.d/01-example.conf

<VirtualHost *:80>
ServerName example.hiroski.com
ServerAlias hiroski.com
DocumentRoot /home/userb/www/htdocs
CustomLog /home/userb/www/log/access_log combined env=!nolog
ErrorLog /home/userb/www/log/error_log
Alias /cgi-bin/ "/home/userb/www/cgi-bin/"
<Directory /home/userb/www/htdocs>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory /home/userb/www/cgi-bin>
Options FollowSymLinks ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

# https ポート定義
<VirtualHost *:443>
ServerName www.hiroski.com
ServerAlias hiroski.com
DocumentRoot /home/userb/www/htdocs
ErrorLog /home/userb/www/log/ssl_error_log
TransferLog /home/userb/www/log/ssl_access_log
LogLevel warn
Alias /cgi-bin/ "/home/userb/www/cgi-bin/"
<Directory /home/userb/www/htdocs>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory /home/userb/www/cgi-bin>
Options FollowSymLinks ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

 定義ファイルを準備したらhttpdを再起動してエラーが起きないことを確認します。

$ sudo systemctl restart httpd
$ sudo systemctl status httpd

 いよいよTLS証明書を取得します。"--apache"オプションで定義ファイルに必要な変更が行われます。

$ sudo certbot --apache

 最初の実行では、メールアドレスの入力が求められます。

$ sudo certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): (入力せずにEnterすると)
Invalid email address: .

If you really want to skip this, you can run the client with
--register-unsafely-without-email but you will then be unable to receive notice
about impending expiration or revocation of your certificates or problems with
your Certbot installation that will lead to failure to renew.

Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel):  (メールアドレスを入力)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-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.
Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: hiroski.com
2: www.hiroski.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 2
Requesting a certificate for hiroski.com and www.hiroski.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/hiroski.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/hiroski.com/privkey.pem
This certificate expires on 2025-06-02.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for hiroski.com to /etc/httpd/domain.d/01-example.conf
Successfully deployed certificate for www.hiroski.com to /etc/httpd/domain.d/01-example.conf
Congratulations! You have successfully enabled HTTPS on https://hiroski.com and https://www.hiroski.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 

 証明書が発行を確認します。前回(2020年)Key Typeが無かったのですが追加され"ECDSA"となっています。CentOS7サーバーを再確認したところやはり追加されていて"RSA"となっていました。

$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: hiroski.com
Serial Number:  (略)
Key Type: ECDSA
Domains: hiroski.com www.hiroski.com
Expiry Date: 2025-06- (略)
Certificate Path: /etc/letsencrypt/live/hiroski.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/hiroski.com/privkey.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

各定義ファイルには次が追加されました。

01-example.confのポート80 ブロック

(途中略)
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.hiroski.com [OR]
RewriteCond %{SERVER_NAME} =breakscan.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

httpからhttpsへリダイレクトが追加。httpが必要な場合はコメントアウトします。IPアドレスでアクセスしたときにhttps://ドメイン/へリダイレクトするときは定義ファイル(ここでは、00-default.conf)へ同様の追加をします。

01-example.confのポート443 ブロック

(途中略)
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hiroski.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hiroski.com/privkey.pem
</VirtualHost>

Includeディレクティブで追加された"options-ssl-apache.conf"はLet's Encryptでの暗号スイートの推奨設定のようです。このファイルは "certbot --apache"で追加されるので一度は実行が必要です(別途バックアップ等から回復等を除き)。

/etc/letsencrypt/options-ssl-apache.conf

# Intermediate configuration, tweak to your needs
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
SSLHonorCipherOrder off
SSLSessionTickets off

SSLOptions +StrictRequire

SSLHonorCipherOrder ディレクティブは、サーバー主導での暗号選択なのでonでよいと思います。

この推奨設定を使用すると、古いいくつかのデバイスが接続出来なくなるようです。

SSL Labs Server Testの結果は前回と同様です。TLSv1.3がサポートされます。

 

HTTPSの動作を確認しますが

 前回次のような記事を記載しました。解決方法は次の通りですが、さくらのVPSコントロールパネルには、ネットワークタブに「ホスト名逆引き登録」があり変更できます。これをWebサーバー側のホスト名と一致させると、ssl.conf内の<VirtualHost>ブロックが生きてくるようです。
「ホスト名逆引き登録」を変更しなければ問題も発生しません。(さくらが変更を推奨しない理由はここにあるのかもしれません。)

(前回の記録です)

 TLS証明書のセットアップが完了したので、さっそくHTTPSでアクセスします。

https://www.hiroski.com/

ドキュメントが入っていないので、404 Not Foundです。

202004-letsencrypt-www.hiroski.com-ok.png

別名である、https://hiroski.com/ は、証明書に問題があるようです。

202004-letsencrypt-hiroski.com-warn.png

原因は、"/etc/httpd/conf.d/ssl.conf"がApacheインストール状態のままで、https://hiroski.com/のときに、Apacheインストール時に組み込まれたローカルな証明書を読み込んでいたからでした。

"/etc/httpd/conf.d/ssl.conf"から、<VirtualHost> ディレクティブ内を削除し、httpdを再起動します。

$ sudo cp -p /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org
$ sudo vim /etc/httpd/conf.d/ssl.conf
(<VirtualHost> ディレクティブ内を削除)

$ sudo systemctl restart httpd

再度、https://hiroski.com/をアクセスして問題ないことを確認します。

"/etc/httpd/conf.d/ssl.conf"から<VirtualHost> ブロック内には次のブロック/ディレクティブがあります。"SSLOptions +StdEnvVars"についてはCGI/SSIにSSL環境変数を追加するので追加した定義ファイル(ポート443)にも設定します。

MSIEの記述は古すぎるので不要でしょう。TLSバージョンや暗号形式のログも無くてもよさそうです。

#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<Files ~ "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
# Notice: Most problems of broken clients are also related to the HTTP
# keep-alive facility, so you usually additionally want to disable
# keep-alive for those clients, too. Use variable "nokeepalive" for this.
# Similarly, one has to force some clients to use HTTP/1.0 to workaround
# their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
# "force-response-1.0" for this.
BrowserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0

# Per-Server Logging:
# The home of a custom SSL log file. Use this when you want a
# compact non-error SSL logfile on a virtual host basis.
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

 

 https://IPアドレス/でアクセスされる場合ですが、証明書を用意しないで<VirtualHost *:443>ディレクティブブロックを組み込むと、Webブラウザにはエラー表示して進めない状態となります。

 

以上で、Apache httpdで定義したサイトに必要な証明書を取得できましたが、メール(postfix/dovecot)で使用するmail.hiroski.comの証明書は取得できていません。

 

メール用の証明書を追加しよう

 つづいて、取得した証明書にmail.hiroski.comを追加します。

mail.hiroski.comはApache httpdで定義していないので、certbotをstandaloneモードで内蔵ウェブサーバーで動作させます。必ずcertonlyで実行します。mailサブドメインはWebサーバーの定義に存在しないので、Apacheの定義ファイルへ予期せぬの変更があると混乱がおきます。
--expandで証明書を拡張するとき、既取得を含め全てを列挙します。

$ sudo systemctl stop httpd
$ sudo certbot certonly --standalone --expand -d hiroski.com -d www.hiroski.com -d mail.hiroski.com

$ sudo systemctl start httpd
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Renewing an existing certificate for hiroski.com and 2 more domains
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/hiroski.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/hiroski.com/privkey.pem
This certificate expires on
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

最終的に取得した証明書のドメインです。

$ sudo certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
Certificate Name: breakscan.com
Serial Number: (略)
Key Type: ECDSA
Domains: hiroski.com mail.hiroski.com www.hiroski.com
Expiry Date: (略)
Certificate Path: /etc/letsencrypt/live/hiroski.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/hiroski.com/privkey.pem

証明書の自動更新を設定しよう

 TLS証明書を取りっぱなしでは、90日の有効期限を過ぎて失効してしまいます。

certbotコマンドをcronで定期的に実行すれば、失効の30日前に更新できます。

以下では、毎日3時に自動更新を実行します。--pre-hook、--post-hookはcertbot実行前後のコマンド指定です。メールも含む場合はpostfix/dovecotの停止/再開も追加します。

$ sudo crontab -e
(以下を追加)
00 03 * * * /usr/bin/certbot renew --pre-hook "/bin/systemctl stop httpd" --post-hook="/bin/systemctl start httpd"

証明書の無効化について

 なんらかの都合で、OSを再インストールするなど証明書が消えてしまうときには、事前に証明書を無効化します。無効化せずに取り直しを繰り返すと回数制限にかかるようです。

$ sudo certbot revoke --cert-name hiroski.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you like to delete the certificate(s) you just revoked, along with all
earlier and later versions of the certificate?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es (recommended)/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificate(s) are selected for deletion:

* hiroski.com

WARNING: Before continuing, ensure that the listed certificates are not being
used by any installed server software (e.g. Apache, nginx, mail servers).
Deleting a certificate that is still being used will cause the server software
to stop working. See https://certbot.org/deleting-certs for information on
deleting certificates safely.
Are you sure you want to delete the above certificate(s)?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
Deleted all files relating to certificate hiroski.com.
Congratulations! You have successfully revoked the certificate that was located at /etc/letsencrypt/live/hiroski.com/cert.pem.

 

  1. AlmaLinux9をさくらのVPSでインストールしよう
  2. AlmaLinux9を初期セットアップしよう
  3. AlmaLinux9へWebサーバー(Apache)をインストールしよう
  4. AlmaLinux9へPHP(8.4)をインストールしよう
  5. AlmaLinux9へTLS証明書(Let's Encrypt)を導入しよう
  6. ALmaLinux9へMariaDBをインストールしよう(10.5 => 10.11)
  7. AlmaLinux9へメールサーバー(Postfix/Dovecot)をインストールしよう
  8. AlmaLinux9へDKIM、DMARCを設定しよう

前へ

AlmaLinux9へPHP(8.4)をインストールしよう

次へ

ALmaLinux9へMariaDBをインストールしよう(10.5 => 10.11)