提供:あわWiki
移動先: 案内検索

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

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

  • バーチャルホスト構築を前提にしてます。
  • 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日以内に更新作業を再度実施する必要があります。

メインホスト設定

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

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

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

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

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

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

Apache設定反映

[root@host4 ~]# systemctl reload httpd

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

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

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

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

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

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

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

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

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

サーバー証明書取得

[root@host4 ~]# certbot certonly --webroot -w /var/www/html/sudachi.jp -d sudachi.jp -d www.sudachi.xyz -m *****@sudachi.xyz --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-sudachi.jp.conf
<VirtualHost *:80>
    ServerName sudachi.jp
    ServerAlias www.sudachi.jp
    DocumentRoot /var/www/html/sudachi.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 sudachi.jp:443 ← メインホストサーバー名を指定
ServerAlias www.sudachi.jp:443 ← WWW 付メインホストサーバー名を指定
DocumentRoot "/var/www/html/sudachi.jp" ← メインホスト用ドキュメントルートを指定
・
・
・
    ## Server Certificate:
    SSLCertificateFile /etc/letsencrypt/live/sudachi.jp/cert.pem ← 公開鍵を指定

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

    ## Server Certificate Chain:
    SSLCertificateChainFile /etc/letsencrypt/live/sudachi.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

サーバー証明書自動更新設定

Let's Encrypt の証明書の有効期限は、90日間と比較的短いため、定期的に更新する必要があります。これは自動更新を前提としているためのようです。自動更新の方法は「certbot renew」コマンドを実行するだけです。証明書の有効期限をチェックして、期限が近づいていれば更新してくれます。

また、指定した日時に証明書を更新したい場合は「--force-renewal」オプションを付けて実行します。ただ、このオプションを付けて毎日コマンドを実行してしまうと取得数制限に引っかかってしまうため、実行するのは月1回程度にしておきましょう。

これらをふまえて以下を crontab に登録します。毎月1日の朝4時に証明書を自動更新しApacheをリロードします。更新日時はお好みで設定してください。

[root@host4 ~]# vi /etc/crontab

00 04 01 * * root /bin/certbot renew --force-renewal && /bin/systemctl reload httpd

ファイルの場所

設定ファイルの場所

[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 -d sudachi.xyz
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -