ヘルプ:Webサーバー間通信内容暗号化にCertbotを導入(CentOS Stream)

提供:あわWiki

メインページ > Help:目次 > Help:自宅サーバー構築(CentOS Stream)  > Webサーバー間通信内容暗号化にCertbotを導入(CentOS Stream)

  • バーチャルホスト構築を前提にしてます。
  • https化かつwwwなしに統一する。
  • 偶にしか行わないので、忘れないように書いておく。

Let's Encrypt から 発行料 無料の SSL/TLS サーバー証明書を取得します。
Let's Encrypt は Linux Foundation の協業プロジェクトで、Web 全体の安全性を改善することをミッションに掲げているとのことです。
発行料 無料 とはいえ、あやしいものではありません。
Let's Encrypt の詳細は公式サイトを参照ください。

Let's Encrypt では 一般的な ドメイン認証 (DV) の証明書を無料で発行しています。
無料ですが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているため、多くの主要ブラウザ等々で信頼済みとして扱われます。
なお、一回の作業で得られる証明書の有効期限は 90日です。よって、90日以内に更新作業を再度実施する必要があります。

mod_sslインストール

[root@host4 ~]# dnf -y install mod_ssl

メインホスト設定

メインホスト用ドキュメントルートディレクトリ作成

  • ドメイン名で作成。
[root@host4 ~]# mkdir /var/www/html/awanet.jp

ディレクトリの所有者変更

  • ドメイン名の所有者変更。
[root@host4 ~]# chown -R sysop. /var/www/html/awanet.jp/

メインホスト設定ファイル作成

[root@host4 ~]# vi /etc/httpd/conf.d/virtualhost-awanet.jp.conf
<VirtualHost *:80>
    ServerName awanet.jp
    ServerAlias www.awanet.jp
    DocumentRoot /var/www/html/awanet.jp
</VirtualHost>

Apache設定反映

[root@host4 ~]# systemctl reload httpd

Certbotクライアントインストール

Snapd のインストール

[root@host4 ~]# dnf --enablerepo=epel -y install snapd

[root@host4 ~]# ln -s /var/lib/snapd/snap /snap

[root@host4 ~]# echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' > /etc/profile.d/snap.sh

[root@host4 ~]# systemctl enable --now snapd.service snapd.socket

Created symlink /etc/systemd/system/multi-user.target.wants/snapd.service → /usr/lib/systemd/system/snapd.service.
Created symlink /etc/systemd/system/sockets.target.wants/snapd.socket → /usr/lib/systemd/system/snapd.socket.
  • 「/etc/profile.d/snapd.sh」で/snap/binのPATH設定などを行います。よってログインし直してください。

Certbotクライアントインストール

証明書を取得するためのツール Certbot クライアントをインストールします。

[root@host4 ~]# snap install certbot --classic
certbot 1.15.0 from Certbot Project (certbot-eff✓) installed

/snap/bin にインストールされたので、使いやすいように /usr/bin/certbot としてシンボリックリンクを作成。

[root@host4 ~]# ln -s /snap/bin/certbot /usr/bin/certbot

バージョンを確認します。

[root@host4 ~]# certbot --version
certbot 1.15.0

ドメイン名に対するサーバー証明書取得

Certbotではサーバー名の認証をWeb経由で行うため、サーバー証明書に指定するサーバー名で外部からWebアクセスできるようにしておく必要がある。

ここでは、www 有る無し両方で使える SSL/TLS サーバ証明書を取得します。

テスト用のサーバ証明書取得

Let's Encrypt 認証局では、ドメイン名あたりの SSL/TLS サーバ証明書の発行枚数について、制限を設けています。そのため、試験段階においては --test-cert オプションを付けて、テスト用のサーバで使ってみることをお勧めします。
※テスト用のサーバで発行したテスト用の証明書は、ブラウザにおいてセキュリティ警告が表示され、信頼された証明書として扱われません。

[root@host4 ~]# certbot certonly --webroot -w /var/www/html/awnet.jp -d awanet.jp -d www.awanet.jp -m *****@sudachi.jp --test-cert

サーバー証明書取得

[root@host4 ~]# certbot certonly --webroot -w /var/www/html/awanet.jp -d awanet.jp -d www.awanet.jp -m *****@sudachi.jp --agree-tos
・
・
・
What would you like to do?
-------------------------------------------------------------------------------
1: Keep the existing certificate for now
2: Renew & replace the cert (limit ~5 per 7 days)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2    ← 2を選択(証明書を更新し、交換する)

サーバー証明書設定

メインホスト用バーチャルホスト設定

ファイル編集

[root@host4 ~]# vi /etc/httpd/conf.d/virtualhost-awanet.jp.conf
<VirtualHost *:80>
    ServerName awanet.jp
    ServerAlias www.awanet.jp
    DocumentRoot /var/www/html/awanet.jp
    # SSL通信にリダイレクト
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

/etc/httpd/conf.d/ssl.confの<VirtualHost _default_:443>~</VirtualHost>をコピーしてここに貼り付けて下記のみ修正

<VirtualHost *:443> ← *に変更

ServerName awanet.jp:443 ← メインホストサーバー名を指定
ServerAlias www.awanet.jp:443 ← WWW 付メインホストサーバー名を指定
DocumentRoot "/var/www/html/awanet.jp" ← メインホスト用ドキュメントルートを指定
・
・
・
    ## Server Certificate:
    SSLCertificateFile /etc/letsencrypt/live/awanet.jp/cert.pem ← 公開鍵を指定

    ## Server Private Key:
    SSLCertificateKeyFile /etc/letsencrypt/live/awanet.jp/privkey.pem ← 秘密鍵を指定

    ## Server Certificate Chain:
    SSLCertificateChainFile /etc/letsencrypt/live/awanet.jp/chain.pem ← 中間証明書を指定

SSL Server Test対応

SSL Server TestでA+評価を得るための設定を行う。

[root@host4 ~]# vi /etc/httpd/conf.d/ssl.conf ← SSL設定ファイル編集
#   SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect.  Disable SSLv2 access by default:
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 ← SSLv2, SSLv3, TLSv1, TLSv1.1 を無効化

#   Speed-optimized SSL Cipher configuration:
#   If speed is your main concern (on busy HTTPS servers e.g.),
#   you might want to force clients to specific, performance
#   optimized ciphers. In this case, prepend those ciphers
#   to the SSLCipherSuite list, and enable SSLHonorCipherOrder.
#   Caveat: by giving precedence to RC4-SHA and AES128-SHA
#   (as in the example below), most connections will no longer
#   have perfect forward secrecy - if the server's key is
#   compromised, captures of past or future traffic must be
#   considered compromised, too.
#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
#SSLHonorCipherOrder on
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS ← 追加
SSLHonorCipherOrder on ← 追加


Header always set Strict-Transport-Security "max-age=15768000" ← 追加
</VirtualHost>

ssl.conf編集

[root@host4 ~]# vi /etc/httpd/conf.d/ssl.conf
--削除(ここから)--
<VirtualHost _default_:443>
・
・
・
</VirtualHost>
--削除(ここまで)--

「httpd.conf」の文法チェック

[root@host4 ~]# apachectl configtest
Syntax OK

Apache設定反映

[root@host4 ~]# systemctl reload httpd

Firewalld の許可

Firewalld を有効にしている場合は、HTTPS サービスの許可が必要です。HTTPS は [443/TCP] を使用します。

[root@host4 ~]# firewall-cmd --add-service=https --permanent
success
[root@host4 ~]# firewall-cmd --reload
success

サーバー証明書をまとめて更新

取得済みの証明書をまとめて更新する場合は、[renew] サブコマンドを使用します。
有効期限が 30日未満の証明書が全て更新されます。
有効期限の残り日数に関わらず更新したい場合は [--force-renew] を合わせて指定します。
ただし、当例のように Snapd からインストールした [certbot] クライアントであれば、更新の手動実行は必要ありません。更新は自動で実行されます。

  • ただし、この機能では、証明書を使用するWebサーバーの再起動は行われません。

Snapd の Certbot パッケージはタイマーが付属

[root@host4 ~]# systemctl status snap.certbot.renew.timer
● snap.certbot.renew.timer - Timer renew for snap application certbot.renew
   Loaded: loaded (/etc/systemd/system/snap.certbot.renew.timer; enabled; vendor preset: disabled)
   Active: active (waiting) since Wed 2021-05-19 10:21:15 JST; 10min ago
  Trigger: Wed 2021-05-19 13:50:00 JST; 3h 18min left

 5月 19 10:21:15 host3.awanet.jp systemd[1]: Started Timer renew for snap application certbot.renew.

デフォルトでは以下のように毎日 2回 [renew] 実行

[root@host4 ~]# systemctl cat snap.certbot.renew.timer
# /etc/systemd/system/snap.certbot.renew.timer
[Unit]
# Auto-generated, DO NOT EDIT
Description=Timer renew for snap application certbot.renew
Requires=var-lib-snapd-snap-certbot-1150.mount
After=var-lib-snapd-snap-certbot-1150.mount
X-Snappy=yes

[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 08:49
OnCalendar=*-*-* 13:50

[Install]
WantedBy=timers.target

手動で更新する場合

[root@host4 ~]# certbot renew

Webサーバーの自動的再起動

  • 日数不足で動作の確認が出来てません。
[root@host4 ~]# vi /etc/letsencrypt/renewal-hooks/deploy/web_restart.sh
#!/bin/bash
systemctl restart httpd
[root@host4 ~]# chmod 755 /etc/letsencrypt/renewal-hooks/deploy/web_restart.sh

Let's Encrypt(certbot)では証明書更新時の追加動作に関する仕組みが拡張され、/etc/letsencrypt/renewal-hooks/以下のディレクトリ(pre, post, deploy)以下に実行ファイルを置くことで、証明書更新の処理を行う際にこれを実行してくれます。

ディレクトリ 処理のトリガー
pre certbotが全部の証明書更新の処理を始める前に証明書の更新の有無に関わらず必ず実行されます
post certbotが全部の証明書更新の処理を終わった後で証明書の更新の有無に関わらず必ず実行されます
deploy certbotが個々の証明書更新の処理を終わった後で証明書の更新された場合にだけ実行されます

ファイルの場所

設定ファイルの場所

[root@host4 ~]# ll /etc/letsencrypt/renewal/
合計 4
-rw-r--r-- 1 root root 629 10月 20 13:15 sudachi.jp.conf

証明書の場所(シンボリックリンク)

[root@host4 ~]# ll /etc/letsencrypt/live/
合計 0
drwxr-xr-x 2 root root 88 10月 20 13:15 sudachi.jp 

[root@host4 ~]# ll /etc/letsencrypt/live/sudachi.jp/
合計 4
-rw-r--r-- 1 root root 682 10月 20 13:15 README
lrwxrwxrwx 1 root root  34 10月 20 13:15 cert.pem -> ../../archive/sudachi.jp/cert1.pem
lrwxrwxrwx 1 root root  35 10月 20 13:15 chain.pem -> ../../archive/sudachi.jp/chain1.pem
lrwxrwxrwx 1 root root  39 10月 20 13:15 fullchain.pem -> ../../archive/sudachi.jp/fullchain1.pem
lrwxrwxrwx 1 root root  37 10月 20 13:15 privkey.pem -> ../../archive/sudachi.jp/privkey1.pem

証明書の場所

[root@host4 ~]# ll /etc/letsencrypt/archive/                                                                      
合計 0
drwxr-xr-x 2 root root 152  9月 10 07:52 sudachi.jp


[root@host4 ~]# ll /etc/letsencrypt/archive/sudachi.jp
合計 32
-rw-r--r-- 1 root root 1785  9月 10 03:43 cert1.pem
-rw-r--r-- 1 root root 1805  9月 10 07:52 cert2.pem
-rw-r--r-- 1 root root 1647  9月 10 03:43 chain1.pem
-rw-r--r-- 1 root root 1647  9月 10 07:52 chain2.pem
-rw-r--r-- 1 root root 3432  9月 10 03:43 fullchain1.pem
-rw-r--r-- 1 root root 3452  9月 10 07:52 fullchain2.pem
-rw-r--r-- 1 root root 1708  9月 10 03:43 privkey1.pem
-rw-r--r-- 1 root root 1708  9月 10 07:52 privkey2.pem

設定ファイル

[root@host4 ~]# ll /etc/letsencrypt/
合計 8
drwx------ 5 root root  115  7月 14 03:14 accounts
drwxr-xr-x 3 root root   23 10月 20 13:15 archive
drwxr-xr-x 2 root root 4096 10月 20 13:15 csr
drwx------ 2 root root 4096 10月 20 13:15 keys
drwx------ 3 root root   23 10月 20 13:15 live
drwxr-xr-x 2 root root   28 10月 20 13:15 renewal
drwxr-xr-x 5 root root   40 10月  7  2017 renewal-hooks

webrootの設定

webrootのディレクトリ名を記録のために書いておく。

.well-known

証明書を失効させる

最新の certbot コマンドには delete オプションが追加され、まとめてファイルを消せるようになりました。

[root@host4 ~]# certbot delete
Saving debug log to /var/log/letsencrypt/letsencrypt.log

Which certificate(s) would you like to delete?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: eeljp.net
2: sudachi.xyz
3: sudachinet.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 2

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Deleted all files relating to certificate sudachi.xyz.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -