WindowsのNginx PHP

Nginx + Windows上のPHP

image

この記事では、WindowsにNginxとPHPをインストールして統合する方法を示します。

テスト済み

  1. Nginx 1.12.1

  2. PHP 7.1.10

  3. ウィンドウズ10

1. Nginx + PHPをインストールします

基本的に、zipファイルをダウンロードして解凍するだけで、インストールは不要です。

Nginxをインストールするには

  1. http://nginx.org/en/download.htmlにアクセス

  2. nginx/Windows-1.12.2をダウンロード

  3. C: ginx-1.12.1に抽出

PHPをインストールするには

  1. http://windows.php.net/download/にアクセス

  2. PHP7.1をダウンロードVC14 x64 Non Thread Safeをダウンロード

  3. C:\php7.1.10に抽出

Note
このPHP VC14ビルドでは、Visual C++ Redistributable for Visual Studio 2015をインストールする必要があります

2. Nginx + PHPを統合する

Nginxはphp-cgi.exeを介してPHPと通信します

2.1 Start PHP at 127.0.0.1:9999

ターミナル

c:\php7.1.10>php-cgi.exe -b 127.0.0.1:9999

2.2 Edit Nginx conf file.

ginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  localhost;

        # Declares here, so that $document_root is able to find php files
        root www;

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

        # For PHP files, pass to 127.0.0.1:9999
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9999;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

2.3 Create a C: ginx-1.12.1\www folder.

2.4 Create a simple PHP file and put it into the www folder.

ginx-1.12.1 \ www \ print.php


2.5 Start Nginx

ターミナル

C:\nginx-1.12.1> start nginx

3. Demo

image

完了しました。