nginx php7 fastcgi,Windows下搭建PHP7+FastCGI+Nginx环境

注意这里说的是FastCGI,不是FPM,FPM全称是FastCGI Process Manager,它是FastCGI进程管理器,在Windows下是没有这个的,只能手动启动FastCGI进程由它自己维护(没有管理器)。自己电脑上用的是PHP 5.6 + Apache 2.4,由于PHP 7.1都出来了,一些新特性得学习,于是决定再搭一个环境。当然的就选了标题里边的组合。

PHP官网下载PHP 7.1 的windows包,这里选择的是php-7.1.0-nts-Win32-VC14-x64,解压后可以看到跟php.exe同级的目录下有php-cgi.exe,启动FastCGI就需要使用它。

b67d5fa9202d0b15d2ac57d645f85ade.png

Nginx官网下载

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端口。用命令行进入nginx.exe所有目录,执行: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了。也简单,命令行进入php-cgi.exe所在目录,执行以下代码即可:

php-cgi.exe -b 127.0.0.1:9123 -c php.ini

-b 即是绑定哪个IP与端口,-c 使用哪个配置文件。这样子启动后,命令行窗口不能做其他 操作也不能关闭,一关闭进程也关闭了。这时候需要借助一个小小的工具:

RunHiddenConsole.exe php-cgi.exe -b 127.0.0.1:9123 -c php.ini

或许觉得这样子麻烦,那么可以把它写成bat脚本,每次要启动双击执行就可以了,或者添加到开机启动,有需要的可以参照参考链接来弄了。我觉得手动执行一下也不是很麻烦。启动后从任务管理器可以看到。

d04a3f82fd41ebee099356cd62109371.png

最后,添加域名到host文件,在网站根目录创建index.php输出phpinfo(),访问http://test.nginx.com:8080,即可看到最终成果:

f59fb854a8824ff2d1219179c4128d80.png

这样,就可以愉快的使用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

#关闭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计划任务中添加一个任务在开机时运行start.bat,这样就更多方便了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值