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;

        }

#省略部分

}

 

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