Windows 下Nginx+PHP 的安裝與配置
? 一.軟體準備
1.Nginx:http://nginx.org/download/nginx-1.2.0.zip
2.php:http://windows.php.net/download/php-5.2.17-Win32-VC6-x86.zip 這裡需要注意一下,如果使用Apache或Nginx運行php,要選擇VC6的安全執行緒版本,使用IIS選擇VC9版本。所以這裡使用前者。
3.RunHiddenConsole:http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip 用於隱藏命令視窗。
二、安裝Php
首先將上面下載的PHP解壓到php的安裝目錄PHP_PATH,並將PHP_PATH以及PHP_PATH/ext添加到Path環境變數中。
將PHP_PATH下的php.ini-dist重新命名為php.ini,這個就是PHP的設定檔。然後根據需要修改這個檔案。
三、安裝Nginx
解壓下載到的nginx-1.2.0.zip,並將其檔案夾命名為nginx-1.2?,可以通過命令列的方式或者指令碼的方式啟動nginx,下面介紹指令碼方式
分別建立一個start_nginx.bat和stop_nginx.bat檔案放置在nginx的根目錄下
start_nginx.bat內容如下:
@echo off
echo Starting nginx...
d:/nginx1.2/nginx.exe
stop_nginx.bat內容如下:
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
exit
啟動nginx,如果出現nginx的歡迎介面,則說明nginx安裝成功
四、Nginx與PHP的配置
1、在Windows環境下,Nginx通過守護進行fast-cgi與php進行互動,這個守護進程是以php-cgi.exe,需要通過命令來執行,格式如下:
php-cgi.exe -b 127.0.0.1:
這樣,php-cgi.exe就可以監聽用戶端的連結請求,但是這個開啟一個命令列視窗。可以通RunHiddenConsole工具來隱藏這個視窗,把RunHiddenConsole.zip解壓,並把其中的RunHiddenConsole.exe複製到nginx的根目錄下。
2、把啟動和停止php-cgi.exe的指令碼添加到上述的start_nginx.bat和stop_nginx.bat檔案(藍色部分):
start_nginx.bat內容如下:
?
@echo off
echo Starting PHP FastCGI...
RunHiddenConsole D:/PHP5.2.17/php-cgi.exe -b 127.0.0.1:9000 -c D:/PHP5.2.17/php.ini
echo Starting nginx...
d:/nginx1.2/nginx.exe
?
stop_nginx.bat內容如下:
?
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
3、修改nginx的設定檔nginx.conf
首先根據需要修改連接埠號碼,在36行,修改listen ? ? ? 80; 為listen ? ? ? 8012;(可任意修改)
其次在45行增加支援預設尋找index.php檔案,修改index ?index.html index.htm;為index ?index.php index.html index.htm;
最後,修改65行到71行
原始代碼為:
location ~ \.php$ {
? ? ? ? ? # ? root ? ? ? ? ? html;
? ? ? ? ??#? fastcgi_pass ? 127.0.0.1:9000;
? ? ? ? ??#? fastcgi_index ?index.php;
? ? ? ? ??#? fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name;
? ? ? ? ? #?include ? ? ? ?fastcgi_params;
? ? ? ? }
修改為:
location ~ \.php$ {
? ? ? ? ?# ? root ? ? ? ? ? html;
? ? ? ? ? ? fastcgi_pass ? 127.0.0.1:9000;
? ? ? ? ? ? fastcgi_index ?index.php;
? ? ? ? ? ? fastcgi_param ?SCRIPT_FILENAME ?D:/nginx1.2/html$fastcgi_script_name;
? ? ? ? ? ? include ? ? ? ?fastcgi_params;
? ? ? ? }
其中,藍色字型需要修改為自己的PHP檔案所在目錄
五、雙擊start_nginx.bat,以啟動nginx和php
在瀏覽器中輸入http://localhost:8012/index.php,如果出現php的歡迎介面,則說明nginx與php的配置成功。
?