提供:すだちWiki
移動先: 案内検索

バーチャルホストの通信内容暗号化にCertbotを導入(AlmaLinux9)

メインページ > Category:自宅サーバー > AlmaLinux 9 で自宅サーバー構築  > バーチャルホストの通信内容暗号化にCertbotを導入(AlmaLinux9)

  • バーチャルホスト構築を前提にしてます。
  • 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@host2 ~]# dnf -y install mod_ssl

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

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

ドメイン名に対する SSL/TLS サーバ証明書を取得する場合。

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

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

[root@host2 ~]# yum --enablerepo=epel -y install certbot

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

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

[root@host2 ~]# certbot certonly --webroot -w /var/www/html/awajp.com -d awajp.com -m *****@awajp.com --test-cert

サーバー証明書取得

[root@host2 ~]# certbot certonly --webroot -w /var/www/html/awajp.com -d awajp.com -m *****@awajp.com --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@host2 ~]# vi /etc/httpd/conf.d/virtualhost-awajp.com.conf
<VirtualHost *:80>
    ServerName awajp.com
    ServerAlias www.awajp.com
    DocumentRoot /var/www/html/awajp.com
    # 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> ← *に変更

DocumentRoot "/var/www/html/sudachi.jp" ← メインホスト用ドキュメントルートを指定
ServerName sudachi.jp:443 ← メインホストサーバー名を指定

追加ホスト用バーチャルホスト設定

設定ファイル編集

[root@host2 ~]# vi /etc/httpd/conf.d/virtualhost-awajp.com.conf
<VirtualHost *:80>
    ServerName awajp.com
    DocumentRoot /var/www/html/awajp.com
    ErrorLog logs/awajp.com-error_log
    CustomLog logs/awajp.com-access_log combined env=!no_log
    # SSL通信にリダイレクト
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
    Protocols h2 http/1.1
    ServerName awajp.com:443 ← 追加ホストサーバー名を指定
    DocumentRoot "/var/www/html/awajp.com" ← 追加ホスト用ドキュメントルートを指定
    ErrorLog logs/awajp.com-error_log ← 追加ホスト用SSLエラーログファイル名を指定
    CustomLog logs/awajp.com-access_log combined env=!no_log ← 追加ホスト用SSLアクセスログファイル名を指定
    Alias /awstatsreport /var/www/html/awajp.com/awstatsreport
    <Location "/awstatsreport">
        Require all denied
        Require ip 127.0.0.1
        Require ip 192.168.1.0/24
    </Location>

    #   SSL Engine Switch:
    SSLEngine on

    ## Server Certificate:
    SSLCertificateFile /etc/letsencrypt/live/awajp.com/cert.pem ← 追加ホスト用公開鍵を指定

    ## Server Private Key:
    SSLCertificateKeyFile /etc/letsencrypt/live/awajp.com/privkey.pem ← 追加ホスト用秘密鍵を指定

    ## Server Certificate Chain:
    SSLCertificateChainFile /etc/letsencrypt/live/awajp.com/chain.pem ← 追加ホスト用中間証明書を指定

    ## SSL Protocol Adjustments:
    BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

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

ssl.conf編集

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

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

[root@host2 ~]# apachectl configtest
Syntax OK

Webサーバー再起動

[root@host2 ~]# systemctl restart httpd

動作確認

[root@host2 ~]# systemctl status httpd
 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running)  since 土 2017-05-06 13:45:58 JST; 46s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 29305 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 29311 (httpd)
   Status: "Total requests: 9; Current requests/sec: 0.111; Current traffic: 1.3KB/sec"
   CGroup: /system.slice/httpd.service
           ├─29311 /usr/sbin/httpd -DFOREGROUND
           ├─29312 /usr/sbin/httpd -DFOREGROUND
           ├─29313 /usr/sbin/httpd -DFOREGROUND
           ├─29314 /usr/sbin/httpd -DFOREGROUND
           ├─29315 /usr/sbin/httpd -DFOREGROUND
           ├─29316 /usr/sbin/httpd -DFOREGROUND
           ├─29317 /usr/sbin/httpd -DFOREGROUND
           ├─29318 /usr/sbin/httpd -DFOREGROUND
           ├─29322 /usr/sbin/httpd -DFOREGROUND
           ├─29323 /usr/sbin/httpd -DFOREGROUND
           └─29324 /usr/sbin/httpd -DFOREGROUND

 5月 06 13:45:57 host3.sudachi.jp systemd[1]: Starting The Apache HTTP Server...
 5月 06 13:45:58 host3.sudachi.jp systemd[1]: Started The Apache HTTP Server.