只今、サイトリニューアル中です。過去記事はこちらから確認できます
PR

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

Webサーバー

このページでは「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のSSL化が済んでる条件で進めます

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

バーチャルホストエリアを追加

nginc.confを編集 「c:/srv/nginx/conf/nginx.conf」

79行目以降に

    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_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

上記のようにドメイン毎のブロックを追加

「http://test」でアクセスできるようになります

    server {
        listen       80;
        server_name  phpmyadmin;

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

上記を追加して「http://phpmyadmin」でアクセスできるようになります

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   c:/srv/html;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           c:/srv/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    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_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    server {
        listen       80;
        server_name  phpmyadmin;

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

SSLホストエリアを追加

「nginx.conf」の末尾に移動
追加したドメインのSSL用のserverエリアを追加

これと同じものを追加したいドメイン毎 末尾に追加します

    server {
        listen       443 ssl;
        server_name  localhost;

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

         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  localhost;

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

         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;

         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 {
        listen       443 ssl;
        server_name  phpmyadmin;

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

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

バーチャルホストエリアと同じドメイン分、SSLホストエリアも追加する

phpmyadminエリアはお好みで

動作確認

サーバー「Nignx」「PHP」を起動、若しくは再起動しエラーがないことを確認

ブラウザにて「https://localhost」「https://test」「https://phpmyadmin」などでアクセスしエラーがないことを確認

コメント

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