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

Nginx

このページでは「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

Nginxでバーチャルホストを有効化

nginx.confを開きます
82行目付近にある「another virtual host~~」のブロックしたに追加したいドメインのserver情報を追加
「lsiten 80」のブロック

# another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    server {
        listen       80;
        server_name  test;

        location / {
            root   c:/srv/html/test;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           c:/srv/html/test;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

server_name ドメイン名
root Webファイルがある場所
※php用のロケーションの追加も忘れずに
更に増やすには「server_name」「root」の部分を変更し追加します

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

nginx.confの末尾に移動します
追加したドメインのSSL用server情報を追加
https://enj-blog.com/nginx/windows11-nginx-php-ssl/#toc1 これと同じものを追加します

server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      c:/srv/SSL/server.crt;
        ssl_certificate_key  c:/srv/SSL/server.key;

        #ssl_session_cache    shared:SSL:1m;
        #ssl_session_timeout  5m;

        #ssl_ciphers  HIGH:!aNULL:!MD5;
        #ssl_prefer_server_ciphers  on;

        location / {
            root   c:/srv/html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           c:/srv/html/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

##以下が 追加した部分
 
   server {
       listen       443 ssl;
       server_name  test;

        ssl_certificate      c:/srv/SSL/server.crt;
        ssl_certificate_key  c:/srv/SSL/server.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

        location / {
            root   c:/srv/html/test;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           c:/srv/html/test;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

「server_name」「root」の部分をそれぞれ変更し追加します

以降の作業は https://enj-blog.com/apache/windows11-apache-php-ssl-virtualhost/ を既に実行済みなら不要です

Nginxを再起動した時の注意

「nginx.conf」を編集して、「Nginx」を再起動しても設定が反映されない時があります
編集ミスによりファイルがダウンロードされたり、 Nginxのデフォルトページが表示されたりすることがあります

その場合の対処法として

  • Nginxを強制終了する。コマンドプロンプトにて「taskkill /f /im nginx.exe」を実行する
  • PCを再起動する
  • ブラウザのキャッシュを削除する(これも忘れがち)

動作確認

ブラウザにて「https://localhost」「https://test」でそれぞれのページが表示されれば成功です

コメント

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