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

ヘルプ:httpsでwwwなしにリダイレクトする方法(Certbot)(Apache)(CentOS8)

メインページ > ヘルプ:目次 > ヘルプ:自宅サーバー構築(CentOS8)  > httpsでwwwなしにリダイレクトする方法(Certbot)(Apache)(CentOS8)

httpsでwwwなしにリダイレクトする方法をご紹介します。

すでに、www無しで稼働しています。

他にいい方法があると思うのですが、いろいろ試した結果この方法にしました。

バーチャルホスト設定

バーチャルホスト設定ファイル作成

[root@host3 ~]# vi /etc/httpd/conf.d/virtualhost-www.sudachi.jp.conf

<VirtualHost *:80>
    ServerName www.sudachi.jp
    #ServerAlias sudachi.jp
    DocumentRoot /var/www/html/sudachi.jp
</VirtualHost>

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

[root@host3 ~]# apachectl configtest
Syntax OK

Webサーバー再起動

[root@host3 ~]# systemctl restart httpd

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

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

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

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

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

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

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

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

複数ドメインで取得すると、www有り、www無し、両方で使える証明書が取得できます。

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

サーバー証明書取得

複数ドメインで取得すると、www有り、www無し、両方で使える証明書が取得できます。

[root@host3 ~]# certbot certonly --webroot -w /var/www/html/sudachi.jp -d www.sudachi.jp -d sudachi.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を選択(証明書を更新し、交換する)

暗号化通信用バーチャルホスト設定

バーチャルホスト設定

  • http、httpsでwwwなしにリダイレクトする
[root@host3 ~]# cat /etc/httpd/conf.d/virtualhost-www.sudachi.jp.conf

<VirtualHost *:80>
    ServerName www.sudachi.jp
    DocumentRoot /var/www/html/sudachi.jp
 
    # www無し、SSL通信にリダイレクト
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName www.sudachi.jp:443
    DocumentRoot "/var/www/html/sudachi.jp"

    # www無し、SSL通信にリダイレクト
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    # 公開鍵指定
    SSLCertificateFile /etc/letsencrypt/live/www.sudachi.jp/cert.pem
    # 秘密鍵指定
    SSLCertificateKeyFile /etc/letsencrypt/live/www.sudachi.jp/privkey.pem
    # 中間証明書指定
    SSLCertificateChainFile /etc/letsencrypt/live/www.sudachi.jp/chain.pem
</VirtualHost>

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

[root@host3 ~]# apachectl configtest
Syntax OK

Webサーバー再起動

[root@host3 ~]# systemctl restart httpd

動作確認

[root@host3 ~]# 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.