iT邦幫忙

0

設定 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 的檔案,都會出現以下畫面:

其中我的 index.php 中的程式碼為:

<?php
  phpinfo();
?> 

我的 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  localhost;

        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;
        }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       include fastcgi_params;
}
    }
}
rtmp {
  server {
    listen 1935;
    ping 30s;
    notify_method get;
    application rtmp {
      live on;
    }
  }
}

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

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
老鷹(eagle)
iT邦高手 1 級 ‧ 2015-08-11 22:20:33
最佳解答

Windows設定方式可以參考這一

Linux設定方式可以參考這份簡報Windows to Fedora

看更多先前的回應...收起先前的回應...
bill20104 iT邦新手 5 級 ‧ 2015-08-14 11:48:58 檢舉

我的 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"
,但我看不太懂,麻煩您指點指點 !!!

SELinux有關閉嗎??

bill20104提到:
(13: Permission denied) while connecting to upstream

關於這個問題我有查到一個資料可以參考一下

還有這兩篇
1
2

chingfeng提到:
SELinux

關閉方法

bill20104 iT邦新手 5 級 ‧ 2015-08-17 10:00:06 檢舉

(因為回覆一次限 1000字,所以我只能把內容拆成兩個部分來回覆您)
chingfeng 您好,我看了您留下的連結後發現,關閉 SELinux 會有安全上的疑慮,

於是我上網查到了 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 改成 :

&lt;pre class="c" name="code">
owner = www-data  // 設定檔內原本就有的,不知用途,但似乎有關連
group = www-data   // 設定檔內原本就有的,不知用途,但似乎有關連
listen = /tmp/php5-fpm.sock
listen.owner = nginx
listen.group = nginx

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

bill20104 iT邦新手 5 級 ‧ 2015-08-17 10:00:27 檢舉

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

&lt;pre class="c" name="code">
owner = www-data  // 設定檔內原本就有的,不知用途,但似乎有關連
group = www-data   // 設定檔內原本就有的,不知用途,但似乎有關連
listen = /tmp/php5-fpm.sock
listen.owner = www-data
listen.group = nginx

結果 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/

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

只好再請您幫助小弟解惑了,小弟感激不盡 ! ! !

bill20104 iT邦新手 5 級 ‧ 2015-08-17 10:28:09 檢舉

Sorry,第二個回應的設定檔內容應為:

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

&lt;pre class="c" name="code">
owner = www-data  // 設定檔內原本就有的,不知用途,但似乎有關連  
group = www-data   // 設定檔內原本就有的,不知用途,但似乎有關連  
listen = /tmp/php5-fpm.sock  
listen.owner = www-data  
listen.group = www-data   

要不要先解除安裝,再重裝一次!
使用這個教學重裝看看
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04
因為看樣子好像是Ubuntu的指令,不然你可能要等我一下,
我需要開個VM去用Ubuntu裝裝看,
目前我裝過的Nginx&PHP-FPM,
是在CentOS 7 & Fedora,
Ubuntu我要試試看~!

bill20104提到:
關閉 SELinux 會有安全上的疑慮

有些情況是SELinux的問題,除非你會去設定,不然新手通常都是先拿掉

bill20104 iT邦新手 5 級 ‧ 2015-08-18 10:05:28 檢舉

chingfeng 您好,我用的作業系統是 Raspberry pi 上的 Raspbian 作業系統。

看一下這篇

bill20104 iT邦新手 5 級 ‧ 2015-08-20 11:30:39 檢舉

chingfeng你好,

1.我把 nginx.conf nginx.conf 的設定檔的 user 改成我的 raspberry pi 的帳號 pi
#user nobody =====> user pi;

2.然後用 sudo usermod -a -G www-data pi 把帳號 pi 加到 www-data 的群組裡,

3.接下來我把 /etc/php5/fpm/pool.d/www.conf 檔改成:

&lt;pre class="c" name="code">
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

listen = /var/run/php-fpm.sock

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = pi
listen.group = pi
;listen.mode = 0660
bill20104 iT邦新手 5 級 ‧ 2015-08-20 11:31:54 檢舉

4.之後我用 sudo service nginx reload

得到: Reloading nginx configuration: nginx.

sudo service nginx restart

得到: Restarting nginx: nginx.

sudo service php5-fpm reload

得到: [ ok ] Reloading PHP5 FastCGI Process Manager: php5-fpm.

sudo service php5-fpm restart

得到: [ ok ] Restarting PHP5 FastCGI Process Manager: php5-fpm.

  1. 接下來我在 瀏覽器 輸入我的 index.php 的 URL,
    但是瀏覽器的主視窗畫面 仍然是一片空白。
bill20104 iT邦新手 5 級 ‧ 2015-08-20 11:32:25 檢舉
  1. 我打開 nginx 的 error.log 查看錯誤訊息,
    只有得到

    <pre class="c" name="code">
    2015/08/20 11:04:07 [error] 4686#0: *11 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.78.27, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"
    2015/08/20 11:04:18 [error] 4686#0: *11 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 36.224.78.27, server: b0241030.dlinkddns.com, request: "GET /favicon.ico HTTP/1.1", host: "b0241030.dlinkddns.com"

我上網查,發現只是缺了 icon,似乎不是大問題,應該不至於造成 打開 index.php 沒有出現任何東西吧!
7.所以我只好再來請問您,請問遇到這種情形時,該怎麼辦呢 ?
是我有任何步驟做錯了嗎?
請您幫小弟解惑,感激不盡 ! ! !

bill20104 iT邦新手 5 級 ‧ 2015-08-20 11:35:48 檢舉

由於一次回覆限 1000字,所以這次我分成了 以上 3 段 ,

另外: 我的 index.php 的程式碼如下如下:

&lt;pre class="c" name="code">
&lt;?php
 phpinfo();
?>

[error] 4686#0

我用這個去查,查到這個網站,可以試試看!

bill20104 iT邦新手 5 級 ‧ 2015-08-27 15:09:48 檢舉

無論如何,還是感謝您的熱心幫忙了,我會全部重頭來過 ! ! !

我要發表回答

立即登入回答