搭建windows+Nginx+PHP配置指南 以及 之後的問題解決
阿新 • • 發佈:2019-01-02
Ps:本文是安裝了wamp後的操作。不過沒安裝也沒事,本文也有一定的思路啟迪。
本文重點講Nginx+PHP搭建完畢遇到的問題,搭建其實很簡單:
開啟nginx的配置檔案nginx.conf,找到
location / {
root html; #這裡是站點的根目錄
index index.php index.html index.htm;
}
改成:
location / {
root D:/wnmp/www;
index index.html index.htm;
}
# 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;
#}
改成:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root D:/wnmp/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
改完後,要連線下php-cgi,然後重啟下nginx即可。
如何連線php-cgi:
Ps:檔案的許可權要設定好,不然會出現403 Forbidden的。 Ps:如果不搞這個php-cgi會出現頁面刷不出來,"An error occured." Ps:因為很多教程中提到的;cgi.fix_pathinfo,我php.ini沒有,都不知道怎麼整了,一查資料發現這個;cgi.fix_pathinfo從 PHP 4.3.0 起可用。可是我用的是WAMP,裡面的PHP是5.2.5啊!!!算了,不管這個傻逼;cgi.fix_pathinfo。 ========================================================================
這個時候發現了我傻逼,我把網站根目錄寫成了"D:/wamp/php",應該是網站的根目錄:"D:/nginx/www"! 改成網站根目錄後還是出現了:403 Forbidden,很明顯我的許可權不足。我想到了可能是要加一些“Everyone“之類的東西吧。但是不知道怎麼配置,果斷把nginx資料夾放置到wamp(我堅信它一定配置好了各種許可權的。)中,並把網站根目錄設定為:"D:/wamp/www:",這樣子就能夠不出現了403 Forbidden了。
檢視error.log發現:
那麼這個fastcgi是幹什麼吃的呢? 原來是是個cgi(Common GatewayInterface)通用閘道器介面,它用來連線nginx和PHP的,和php-cgi.exe有關係。 所以很明顯,我以上的動作並沒有搞過php-cgi.exe,所以導致沒人CGI去處理nginx的連線吧,所以一直才會TIMEOUT。。。 用以下語句:
location / {
root html; #這裡是站點的根目錄
index index.php index.html index.htm;
}
改成:
location / {
root D:/wnmp/www;
index index.html index.htm;
}
# 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;
#}
改成:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root D:/wnmp/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
改完後,要連線下php-cgi,然後重啟下nginx即可。
D:\wamp\php>php-cgi.exe -b 127.0.0.1:9000 -c D:/wamp/php/php.ini
Ps:檔案的許可權要設定好,不然會出現403 Forbidden的。 Ps:如果不搞這個php-cgi會出現頁面刷不出來,"An error occured." Ps:因為很多教程中提到的;cgi.fix_pathinfo,我php.ini沒有,都不知道怎麼整了,一查資料發現這個;cgi.fix_pathinfo從 PHP 4.3.0 起可用。可是我用的是WAMP,裡面的PHP是5.2.5啊!!!算了,不管這個傻逼;cgi.fix_pathinfo。 ========================================================================
一.修改完後nginx.conf,出現了403 Forbidden
查看了下log檔案中的error.log:2012/11/05 14:45:19 [error] 4704#4740: *6 directory index of "D:/wamp/php/" is forbidden, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:83"
這句話應該就是告訴我這個目錄沒許可權吧。這個時候發現了我傻逼,我把網站根目錄寫成了"D:/wamp/php",應該是網站的根目錄:"D:/nginx/www"! 改成網站根目錄後還是出現了:403 Forbidden,很明顯我的許可權不足。我想到了可能是要加一些“Everyone“之類的東西吧。但是不知道怎麼配置,果斷把nginx資料夾放置到wamp(我堅信它一定配置好了各種許可權的。)中,並把網站根目錄設定為:"D:/wamp/www:",這樣子就能夠不出現了403 Forbidden了。
二. upstream timed out 未啟動fastcgi
然後重新整理,發現還是有問題,網頁打不開:檢視error.log發現:
2012/11/05 15:17:19 [error] 4992#1528: *10 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /php.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:83"
簡單來說就是TIME-OUT,和這個fastcgi有關係那麼這個fastcgi是幹什麼吃的呢? 原來是是個cgi(Common GatewayInterface)通用閘道器介面,它用來連線nginx和PHP的,和php-cgi.exe有關係。 所以很明顯,我以上的動作並沒有搞過php-cgi.exe,所以導致沒人CGI去處理nginx的連線吧,所以一直才會TIMEOUT。。。 用以下語句:
D:\wamp\php>php-cgi.exe -b 127.0.0.1:9000 -c D:/wamp/php/php.ini
啟動了php-cgi,然後重新啟動nginx,就看到了: