nginx php7 fastcgi,Windows下搭建PHP7+FastCGI+Nginx环境
注意這里說的是FastCGI,不是FPM,FPM全稱是FastCGI Process Manager,它是FastCGI進(jìn)程管理器,在Windows下是沒有這個的,只能手動啟動FastCGI進(jìn)程由它自己維護(hù)(沒有管理器)。自己電腦上用的是PHP 5.6 + Apache 2.4,由于PHP 7.1都出來了,一些新特性得學(xué)習(xí),于是決定再搭一個環(huán)境。當(dāng)然的就選了標(biāo)題里邊的組合。
PHP官網(wǎng)下載PHP 7.1 的windows包,這里選擇的是php-7.1.0-nts-Win32-VC14-x64,解壓后可以看到跟php.exe同級的目錄下有php-cgi.exe,啟動FastCGI就需要使用它。
Nginx官網(wǎng)下載
server {
listen 8080;
server_name test.nginx.com;
root E:/amp/nginx-1.11.8/html/test;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php$is_args$args;#不需要單入口重寫,不需要這句
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log E:/amp/nginx-1.11.8/logs/test/access.log;
error_log E:/amp/nginx-1.11.8/logs/test/error.log;
}
由于Apache 2.4使用了80端口,這里便使用8080端口了。PHP FastCGI將會使用9123端口。用命令行進(jìn)入nginx.exe所有目錄,執(zhí)行:start nginx即可啟動Nginx。其他命令如nginx -s stop|quit|reopen|reload等跟Linux下是一樣的。
E:\amp\nginx-1.11.8>nginx -h
nginx version: nginx/1.11.8
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: NONE)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
下邊就是啟動PHP FastCGI了。也簡單,命令行進(jìn)入php-cgi.exe所在目錄,執(zhí)行以下代碼即可:
php-cgi.exe -b 127.0.0.1:9123 -c php.ini
-b 即是綁定哪個IP與端口,-c 使用哪個配置文件。這樣子啟動后,命令行窗口不能做其他 操作也不能關(guān)閉,一關(guān)閉進(jìn)程也關(guān)閉了。這時候需要借助一個小小的工具:
RunHiddenConsole.exe php-cgi.exe -b 127.0.0.1:9123 -c php.ini
或許覺得這樣子麻煩,那么可以把它寫成bat腳本,每次要啟動雙擊執(zhí)行就可以了,或者添加到開機(jī)啟動,有需要的可以參照參考鏈接來弄了。我覺得手動執(zhí)行一下也不是很麻煩。啟動后從任務(wù)管理器可以看到。
最后,添加域名到host文件,在網(wǎng)站根目錄創(chuàng)建index.php輸出phpinfo(),訪問http://test.nginx.com:8080,即可看到最終成果:
這樣,就可以愉快的使用PHP 7.1與PHP 5.6了。
update at 2017-06-27
為方便啟動,可以寫一個bat文件完成FastCGI與nginx的啟動。注意bat文件中定義變量=兩邊不能有空格
#啟動start.bat
@echo off
set nginx_path=E:\nginx-1.13.1
set php_path=E:\php-5.6.30-Win32-VC11-x86
set port=9123
echo Starging PHP FastCGI at 127.0.0.1:%port%...
%nginx_path%\RunHiddenConsole.exe %php_path%\php-cgi.exe -b 127.0.0.1:%port% -c %php_path%\php.ini
echo Starging nginx...
%nginx_path%\RunHiddenConsole.exe %nginx_path%\nginx.exe
echo Done !
exit
#關(guān)閉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
在windows計劃任務(wù)中添加一個任務(wù)在開機(jī)時運(yùn)行start.bat,這樣就更多方便了。
總結(jié)
以上是生活随笔為你收集整理的nginx php7 fastcgi,Windows下搭建PHP7+FastCGI+Nginx环境的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php ini include,php.
- 下一篇: oracle会话资源,oracle se