Question5:Windows10的nginx + php配置问题

Windows10的nginx + php配置问题

原本安装配置nginx和php没啥好说的问题(之前也没有遇到过)。最近开始重新安装配置新版本的出现一些“坑”。任何面向过程的“小问题”都是无法避开的大问题。所以开始关注这些“小问题”。

众所周知,操作系统文件系统路径分相对路径和绝对路径。其实,相对路径也是一种绝对的--简化写法的绝对路径。如果没有明确的地址是无法找到指定目标的。相对路径是以指定根目录为前提的:没有明确指定的根目录,就无法寻址相对于根目录的子目录。所以,在安装完nginx和php后,首先要注意的是它们的根目录配置问题:指定nginx站点和php的extension扩展库根目录。这个目录需是清晰的绝对路径。(当然相对路径也可以运行,但是在某些情景下会出现某明奇妙的问题。)这是关键的第一步,也是“千里之行”的开始。

Nginx配置站点根目录

server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

        charset utf-8;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   C:/data/nginx/html;

            index  index.html index.htm index.php;

        }

#省略部分

}

server_name设置网站域名地址,本地测试则为localhost,listen指定默认访问端口为80,charset设定默认的字符集编码,root配置网站根目录,index指定每个目录下作为主页的文件选择。

#省略部分中:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #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;

        #}

复制将#号去掉,将/scripts修改为$document_root:

location ~ \.php$ {

            root           C:/data/nginx/html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

root最好修改为绝对路径,这里为了全覆盖,将php脚本的root修改与网站的root相同。

这样nginx的配置好了?试试!

新建文件version.php:

<?php

phpinfo();

?>

打开浏览器输入http://localhost,显示非错误警告页面表示nginx安装成功。访问http://localhost/version.php,显示结果则表示nginx配置php成功;否则配置错误。这里显示的是:

An error occurred.

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

明显配置有误: 缺少某些设置或操作。从上面看到最多重复的字眼是fastcgi。(CGI也跟PHP一样可以用作网站编程,但是涉及更底层的操作,相对麻烦。)很明显,nginx是通过fastcgi模式访问php脚本的。也即是说fastcgi是一条通道。但是是谁建立维护的?(Who run it?)

在以上所有的配置中,只是配置了nginx通往php的通道fastcgi,但是并没有建立这条通道。(基于安全性和维护的问题,至少目前的配置还没有达到这样的自动化程度)所以缺少的是建立fastcgi通道的操作。这是一个对于不了解nginx执行php原理难以跨越的“坑”。操作系统建立什么通道/协议隧道之类,是通过进程控制的,而进程是通过命令启动的。这里显然缺少的是执行cgi命令的可执行文件。现在来看看资料。

PHP CGI:https://www.php.net/manual/zh/install.unix.commandline.php

里面提到:

默认为将 PHP 编译为 CLI 和 CGI 程序。这将建立一个命令行解释器,可用于 CGI 处理或非 web 相关的 PHP 脚本。如果用户运行着一个 PHP 模块支持的 web 服务器,那通常为性能考虑应该使用模块方式。

PHP执行有两种方式CLI和CGI,CLI为非web相关,CGI方式处理web相关的脚本。

PHP FastCGI:https://www.php.net/manual/zh/install.fpm.php

但是没有提及windows下如何启动FastCGI的进程,只了解FastCGI进程管理器FPM安装配置(最新是内置的)。又看PHP:运行时配置https://www.php.net/manual/zh/configuration.php

“cduke420 at gmail dot com ¶13 years ago

[ When php run as Apache Module ]

DOCUMENT_ROOT .htaccess

+======================================+

SetEnv PHPRC /home/user/dir-containing-phpinifile

+======================================+

 

[ When php run as CGI ]

Place your php.ini file in the dir of your cgi'd php binary, in this case /cgi-bin/

DOCUMENT_ROOT .htaccess

+======================================+

AddHandler php-cgi .php .htm

Action php-cgi /cgi-bin/php5.cgi

+======================================+

 

[ PHP run as cgi with wrapper (for FastCGI) ]

Your wrapper script should look something like:

+======================================+

#!/bin/sh

export PHP_FCGI_CHILDREN=3

exec /user/htdocs/cgi-bin/php.cgi -c /home/user/php.ini

+======================================+

 

original article:

http://www.askapache.com/2007/php/custom-phpini-tips-and-tricks.html”虽然里面不是Windows操作系统的,但其中提到CGI模式的“exec /user/htdocs/cgi-bin/php.cgi -c /home/user/php.ini

”说明有个php cgi相关的可执行文件。上网一查找“php cgi exe”相关内容,结果发现相关最大的是php-cgi.exe。Windows DOS输入:

>php-cgi --help

Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]

       php <file> [args...]

  -a               Run interactively

  -b <address:port>|<port> Bind Path for external FASTCGI Server mode

  -C               Do not chdir to the script's directory

  -c <path>|<file> Look for php.ini file in this directory

  -n               No php.ini file will be used

  -d foo[=bar]     Define INI entry foo with value 'bar'

  -e               Generate extended information for debugger/profiler

  -f <file>        Parse <file>.  Implies `-q'

  -h               This help

  -i               PHP information

  -l               Syntax check only (lint)

  -m               Show compiled in modules

  -q               Quiet-mode.  Suppress HTTP Header output.

  -s               Display colour syntax highlighted source.

  -v               Version number

  -w               Display source with stripped comments and whitespace.

  -z <file>        Load Zend extension <file>.

  -T <count>       Measure execution time of script repeated <count> times.

”最显眼的莫过于“FastCGI Server Mode”,这不跟所要解决的问题相关度最大?Windows DOS命令行输入:

php-cgi -b 127.0.0.1:9000

没有返回结果,界面“定格”: 进程正在运行。重新测试网页http://localhost/version.php:

看到蓝灰色的php信息页面显示成功。

其中的Server API为:

CGI/FastCGI

说明nginx配置php+fastcgi正式成功。

说好的一些“坑”怎么只说了一个?!

当Windows DOS输入一次start nginx.exe时:可以看见任务管理器发现启动2个nginx.exe进程。当连续两次输入同样命令时,可以发现启动4个进程。然后连续2次输入nginx.exe -s stop发现只关闭其中两个。打开浏览器访问PHP脚本正常。

 

 

 

这个看起来没有问题。

listen和fastcgi_pass的port端口搞混会怎么样,比如设置相同。listen port是nginx的server port,而fastcgi_pass port是php-cgi的server port。熟悉socket的都知道server port的意义。server port相同,会导致一方服务因端口被占用无法启动。加上上面的一个特性就会导致这个问题“复杂化”。Nginx的server port是提供浏览器这类客户端使用的。而fastcgi_pass的server port是php-cgi提供nginx使用的。假设fastcgi_pass 127.0.0.1:9000,如果使用http://localhost:9000这样去访问会发生错误。

无法访问此网站

连接已重置。

请试试以下办法:

ERR_CONNECTION_RESET

如果php脚本有浏览目录的行为活动,则应添加autoindex On;的设置。

server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

        charset utf-8;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   C:/data/nginx/html;

            index  index.html index.htm index.php;

            autoindex  On;

        }

#省略部分

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章