Windows11 Apache+PHP+SSL バーチャルホスト化

Apache

このページでは「Apache」と「NGINX」を同じPCに導入した場合のパスを指定しています
パスに関しては一例としてみてください

ApacheNGINX
サーバーc:/srv/Apache24c:/srv/nginx
Webファイルc:/srv/html
PHPc:/srv/Apache24/php8.4(8.3)c:/srv/nginx/php8.4(8.3)
MariaDBc:/srv/MariaDB
SSLc:/srv/SSL

Apacheでのバーチャルホスト有効化

htpd.confを開きます
183行目付近「LoadModule vhost_alias_module modules/mod_vhost_alias.so」コメントアウトを削除
510行目付近「Include conf/extra/httpd-vhosts.conf」コメントアウトを削除

「c:/srv/Apache24/conf/extra/httpd-vhost.conf」 を開きます
23行目以降を適宜変更していきます。

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "${SRVROOT}/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "${SRVROOT}/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "C:/srv/html"
    ServerName localhost
    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "C:/srv/html/test"
    ServerName test
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

必要なのは以下2つ
・DocumentRoot Webファイルがあるフォルダまでのパス
・SeverName ドメイン名 http://localhost http://test など
 ※「localhost」や「test」の部分がドメイン名になります

localhostなど省きたい時などに便利です。例えば「http://phpmyadmin/」などもできます

SSLでのバーチャルホスト有効化

※Apache+PHPでSSLを有効化していない場合はこの項目は不要です

「c:/srv/Apache24/conf/extra/httpd-ssl.conf」を開きます

 124行目 DocumentRoot
 125行目 ServerName
 144行目 SSLCertificateFile “${SRVROOT}/conf/server.crt”
 154行目 SSLCertificateKeyFile “${SRVROOT}/conf/server.key”

の部分を変更します

DocumentRoot "${SRVROOT}/htdocs"
ServerName www.example.com:443
↓
DocumentRoot "C:/srv/html"
ServerName localhost

SSLCertificateFile "${SRVROOT}/conf/server.crt"
↓
SSLCertificateFile "C:/srv/SSL/server.crt"

SSLCertificateKeyFile "${SRVROOT}/conf/server.key"
↓
SSLCertificateKeyFile "C:/srv/SSL/server.key"

DocumentRootは Webファイルがある場所までのパス
ServerNameは デフォルトのドメイン

「SSLCertificateFile」と「SSLCertificateKeyFile」はWindows11 Apache+PHP SSL化 で作成した証明書までのパス

 ※バーチャルホストでSSLを使う場合は上記部分の変更が必要

テキスト(httpd-ssl.conf)末尾に以下を追加

<VirtualHost *:443>
    DocumentRoot "C:/srv/html"
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "C:/srv/SSL/server.crt"
    SSLCertificateKeyFile "C:/srv/SSL/server.key"
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "C:/srv/html/test"
    ServerName test
    SSLEngine on
    SSLCertificateFile "C:/srv/SSL/server.crt"
    SSLCertificateKeyFile "C:/srv/SSL/server.key"
</VirtualHost>

DocumentRootとServerNameは「httpd-vhost.conf」と同じにする

Windowsの hosts を編集

メモ帳を「管理者として実行」
「C:\Windows\System32\drivers\etc\hosts」を開きます
末尾に以下を追加

127.0.0.1 localhost
127.0.0.1 test

PCを再起動 ※hostsの設定を有効化する為

SSL証明書を書き換える

Windows11 Apache+PHP SSL化 と同じやり方になりますが、一部追加しならが書き換えが必要になります

SSL設定ファイルの編集

「c:/srv/SSL/openssl-san.cnf」を開きます

[ SAN ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = localhost

は既に書かれていると思うので、SSLに適用したいドメインを追加していきます

[ SAN ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = localhost
DNS.2 = test

サーバー証明書を作成

Apache+PHP+SSL サーバー証明書を作成 以降同じ作業をします

 ※古い証明書を削除するのを忘れないように

動作確認

念のため、PCを再起動

ブラウザで「https://localhost」や「https://test」にアクセスしエラーが出なければ成功です

コメント

タイトルとURLをコピーしました