安裝 nginx

前往 nginx 官網,下載nginx/windows 的檔案

這裡下載的版本是 Stable version - nginx/Windows-1.12.2

在 C 槽新增一個 nginx_php/ 資料夾

將下載的nginx 壓縮檔解壓縮後放入

C:\nginx_php\nginx-1.12.2\

下載 PHP

前往PHP官網 下載頁

我這裡使用的環境是一般的 windows10

如果你的環境是windows server,則可以參考下方

More recent versions of PHP are built with VC11, VC14 or VC15 (Visual Studio 2012, 2015 or 2017 compiler respectively) and include improvements in performance and stability.

– The VC11 builds require to have the Visual C++ Redistributable for Visual Studio 2012 x86 or x64 installed

– The VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 x86 or x64 installed

– The VC15 builds require to have the Visual C++ Redistributable for Visual Studio 2017 x64 or x86 installed

在 windows10,我選擇的版本是 Old Stable PHP 7.1.14 - Windows downloads - VC14 x64 Thread Safe (2018-Jan-31 23:23:00) 的 Zip[23.23MB]

在 C:\nginx_php\nginx-1.12.2\ 建立一個 php 資料夾,並將 php7.1 檔案都解壓縮在這裡面

C:\nginx_php\nginx-1.12.2\php\

修改 nginx.conf

開啟 nginx 資料夾裡的 nginx.conf

並且將php 設定的區塊註解(#)拿掉

接著,將 fastcgi_param 修改成 $document_root$fastcgi_script_name; (也可以改成 C:/nginx_php/nginx-1.12.2/html$fastcgi_script_name;)

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

另外,nginx預設port 為80

由於機組上還有其他軟體會使用到80 port,因此我要把nginx執行的port改成3000

一樣在 nginx.conf 找到 server , listen 值80變更為 3000

    server {
        listen       3000;

設定 php.ini

將 nginx-1.12.2/php 資料夾中的 php.ini.production 或 php.ini.development 複製並命名為 php.ini

並且增加以下設定

extension_dir = "C:\nginx_php\nginx-1.12.2\php\ext"

開啟 mysql 及 pdo 功能

extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll

運行 php

開啟 windows cmd 介面,前往 C:\nginx_php\nginx-1.12.2\php\ 執行下方指令,啟用 php

php-cgi -b 127.0.0.1:9000

若要停止,可直接用 ctrl+c

運行 nginx

另外再開啟兩個 windows cmd 介面,分別用來處理啟用 nginx 及停止運行 nginx

首先,前往 C:\nginx_php\nginx-1.12.2\php\

執行下方命令,啟用nginx

nginx

在另 cmd 介面,執行下指令停止nginx

nginx -s quit

//or

nginx -s stop

如果有變更設定檔,可執行下指令重新載入配置

nginx -s reload

啟用 web 並新增 phpinfo

網頁的主要檔案都放在 html 資料夾中,並且裡面會放一支 index.html 歡迎頁

C:\nginx_php\nginx-1.12.2\html

執行 nginx ,啟用成功後,可以開啟瀏覽器並前往 http://localhost:3000 ,查看是否出現歡迎頁面畫面

接著,測試php是否能正常運行

在html/裡新增一個 test.php

<?php phpinfo();?>

檢視 http://localhost:3000/test.php

製作啟動/關閉 nginx 批次檔

下載 RunHiddenConsole 這套小程式

我們要用它來寫啟用及停用nginx+php的批次檔

啟用服務 nginx_server_start.bat

@echo off
set php_home_dir=C:/nginx_php/nginx-1.12.2/php
set nginx_home_dir=C:/nginx_php/nginx-1.12.2

echo Starting PHP FastCGI...
RunHiddenConsole %php_home_dir%/php-cgi.exe -b 127.0.0.1:9000
 
echo Starting nginx...
RunHiddenConsole %nginx_home_dir%/nginx.exe

停止服務 nginx_server_stop.bat

@echo off
echo Stopping nginx...  
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit

將檔案放入 nginx 資料夾中

就能點選的方式來啟用或關閉nginx

或者,也能將批次檔加入 windows 啟動項目中,就能開機自動執行

問題排解

出現 No Input file specified 錯誤

請檢查 nginx 資料夾裡的 nginx.conf

  1. php 設定的區塊註解(#)拿掉
  2. fastcgi_param 是否有正確設定為 $document_root$fastcgi_script_name; (也可以改成 C:/nginx_php/nginx-1.12.2/html$fastcgi_script_name;)
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            
        }