Windows下配置Nginx使之支持PHP

2016-06-27 09:03:58 9335

1. 首先,将 nginx.conf 中的 PHP 配置注释去掉。

01# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
02#
03#location ~ .php$ {
04#    root           html;
05#    fastcgi_pass   127.0.0.1:9000;
06#    fastcgi_index  index.php;
07#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
08#    include        fastcgi_params;
09#}
10
11location ~ .php$ {
12    root           html;
13    fastcgi_pass   127.0.0.1:9000;
14    fastcgi_index  index.php;
15    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
16    include        fastcgi_params;
17}

2. 这里使用的 PHP 是以 cgi 的形式,所以要启用 php-cgi,修改 php.ini,把注释去掉:

1;cgi.fix_pathinfo=1
2cgi.fix_pathinfo=1

3. 启动 php-cgi 和 nginx,下面介绍两个脚本:

start_nginx.bat

01@echo off
02
03REM set PHP_FCGI_CHILDREN=5
04set PHP_FCGI_MAX_REQUESTS=1000
05
06echo Starting PHP FastCGI...
07RunHiddenConsole D:/xampp/php/php-cgi.exe -b 127.0.0.1:9000 -c D:/xampp/php/php.ini
08
09echo Starting nginx...
10RunHiddenConsole D:/nginx/nginx.exe -p D:/nginx/

stop_nginx.bat

view sourceprint?

1@echo off
2echo Stopping nginx... 
3taskkill /F /IM nginx.exe > nul
4echo Stopping PHP FastCGI...
5taskkill /F /IM php-cgi.exe > nul
6exit

相关下载:RunHiddenConsole.zip

可以看看进程里,如果 nginx 和 php-cgi 都有,那么差不多要成功了。最后,可能会出现 "No input file specified" 的问题,那么修改一下 nginx.conf :

01location ~ .php$ {
02    #root           html;
03    root           D:/nginx/html;
04    fastcgi_pass   127.0.0.1:9000;
05    fastcgi_index  index.php;
06    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
07    #fastcgi_param  SCRIPT_FILENAME D:/nginx/html$fastcgi_script_name;
08    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
09    include        fastcgi_params;
10}

注意注释的地方,修改成下面那行,请根据你的具体文件配置路径。

编辑 test.php :

1<?php
2    phpinfo();
3?>

运行 http://www.landui.com:81/test.php,OK。


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: