設定 Nginx 伺服器支援 PHP

小弟根據這篇文章: http://blog.gtwang.org/iot/raspberry-pi-nginx-rtmp-server-live-streaming/ 完成 Nginx 伺服器的架設和串流視訊影像,
然後我又參考了另一篇文章 http://tonyhack.familyds.net/wordpress/?p=4173
,想要設定 Nginx 支援 php ,
但不知為何,每次我用瀏覽器連結到 index.php 或其他附檔名為 php 的檔案,都會出現以下畫面:

設定 Nginx 伺服器支援 PHP

我的 nginx.conf 的內容是:

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name b0241030.dlinkddns.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /var/www;
index index.html index.htm index.php;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
# root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

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

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini

# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application rtmp {
live on;
}
}
}

不知是哪個環節出了錯,還請大家指點指點,小弟感激不盡 ! ! !


我的 error.log 的內容是:

2015/08/14 11:15:36 [error] 2067#0: *1 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"
2015/08/14 11:16:40 [crit] 2067#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "b0241030.dlinkddns.com"
2015/08/14 11:16:41 [error] 2067#0: *1 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"

,但我看不太懂,麻煩各位指點指點 !!!
2015-08-11 18:15 發佈
Nginx 沒玩過,但那訊息不代表你的 Nginx 不支援 PHP。
你應該要詳讀 error log 給你的訊息。

John wrote:
Nginx 沒玩過...(恕刪)


您好,我的 error.log 的內容是:
2015/08/14 11:15:36 [error] 2067#0: *1 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"
2015/08/14 11:16:40 [crit] 2067#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "b0241030.dlinkddns.com"
2015/08/14 11:16:41 [error] 2067#0: *1 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"
,但我看不太懂,麻煩您指點指點 !!!
看了一下你的記錄檔,可能原因是權限不足。請檢查一下 /var/run/php5-fpm.sock 的存取權限。

2015/08/14 11:16:40 [crit] 2067#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 36.224.87.207, server: b0241030.dlinkddns.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "b0241030.dlinkddns.com"

其他兩筆錯誤可以忽略,那只是告訴你找不到 /var/www/favicon.ico 這個圖示檔而已。
John 您好,我上網查到了 3 個相關的網站,如下:
1. http://www.nginxtips.com/php5-fpm-sock-failed-13-permission-denied-error/
2. http://websistent.com/fix-connect-to-php5-fpm-sock-failed-13-permission-denied-while-connecting-to-upstream-nginx-error/
3.http://blog.itist.tw/2014/06/nginx.html

我先照著 1號 網站提供的 第 1 個解決方法,結果 reload nginx 、php5-fpm 和 restart nginx、php5-fpm
都沒有問題,但我用瀏覽器連我的 index.php (程式碼為 <?php phpinfo(); ?>),
瀏覽器主視窗卻是一片空白,而且沒有回報錯誤,

於是我又用了 1號 網站提供的 第 2 個解決方法,我使用 ls -ahl /tmp/php5-fpm.sock,得到的結果是:
srw-rw---- 1 www-data www-data ,和 1號 網站 的 srw-rw---- 1 root root不一樣,
然後我照著把 nginx.conf 的 user 改成 nginx,
把 php5-fpm.d/www.conf 改成 :

owner = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
group = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
listen = /tmp/php5-fpm.sock
listen.owner = nginx
listen.group = nginx

但這次 nginx reload 失敗,php5-fpm 也 reload 失敗,

於是我參考了 第 2、第 3 網站中 和 第 1網站 有相關聯的部分,
把 nginx.conf 的 user 改成 www-data,
把 php5-fpm.d/www.conf 改成 :

owner = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
group = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
listen = /tmp/php5-fpm.sock
listen.owner = www-data
listen.group = www-data

結果 reload nginx 、php5-fpm 和 restart nginx、php5-fpm
都沒有問題,但我用瀏覽器連我的 index.php (程式碼為 <?php phpinfo(); ?>),
瀏覽器主視窗是一片空白,而且沒有回報錯誤,和我用 1號 網站的第1個方法結果相同,

於是我只好再上網查,查到了一個網站:
http://www.cagataygurturk.com/fix-connect-to-php5-fpm-sock-failed-13-permission-denied-while-connecting-to-upstream-nginx-error/

好像有幫助,但小弟看不太懂所以不敢照做,

麻煩您幫助小弟解惑,小弟感激不盡 ! ! !
阿明111 wrote:
owner = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
group = www-data // 設定檔內原本就有的,不知用途,但似乎有關連
listen = /tmp/php5-fpm.sock
listen.owner = nginx
listen.group = nginx

先就設定檔的部份來看,這些似乎都是在指定使用者、群組的權限。

先從你提到的
於是我又用了 1 號 網站提供的 第 2 個解決方法,我使用 ls -ahl /tmp/php5-fpm.sock,得到的結果是:
srw-rw---- 1 www-data www-data ,和 1 號 網站 的 srw-rw---- 1 root root 不一樣

再對照 http://www.cagataygurturk.com/fix-connect-to-php5-fpm-sock-failed-13-permission-denied-while-connecting-to-upstream-nginx-error/ 中提到的
So, the right solution would be permitting nginx’s running user to socket file by this command. So, first check which user runs nginx. As of Ubuntu 12.04 nginx runs by nginx user which is not a member of www-data group, but the socket file can be accessed by this group. So, let’s simply add the user to this group:

usermod -a -G www-data nginx

簡單說,就是要先檢查 nginx 是以哪個使用者的身份執行。
後文提到 Ubuntu 12.04 是以 nginx 這使用者執行,但它不屬於 www-data 這個群組,而 www-data 可以存取 socket 檔案,所以只要把 nginx 這個使用者加入 www-data 這個群組就可以了。它最後的指令,就是將 nginx 這使用者加入 www-data 這個群組裡。

John wrote:
先就設定檔的部份來看...(恕刪)


John,請問要如何知道自己系統的 nginx 是以哪個使用者的身份執行???
我找到了一篇文章

http://seanlin0324.blogspot.tw/2013/07/nginx_3.html

在第一行,有指定要以哪個使用者執行。
但你的設定檔,似乎沒特別指定,沒特別指定,我就不知該如何去查詢了。
Unix,Linux 這類的系統,我涉獵不深,所以不知該下什麼樣的指令查詢。

但,大致上來說,我認為還是和使用者及權限有關係。
John wrote:
我找到了一篇文章
http...(恕刪)



還是謝謝你了 ! ! !

阿明111 wrote:
http://tonyhack.familyds.net/wordpress/?p=4173





1. ps aux | grep nginx
2. top
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?