nginx 與 php-fpm 下載 php 檔案而不是在 mac os x 上執行它們(本地環境)

nginx 與 php-fpm 下載 php 檔案而不是在 mac os x 上執行它們(本地環境)

我在本機開發電腦上執行 PHP 檔案時遇到問題。我好像已經安裝成功了nginxphp-fpm似乎也已正確安裝並運行,但無法完全弄清楚為什麼 PHP 檔案被下載而不是執行。

經過很多很多小時挫折,我認為最好問問以前可能做過這件事的人!我已盡力提供所有信息,但如果有任何可能有幫助或我遺漏的信息,請隨時詢問在評論中。

請注意:仔細閱讀我遇到的問題。我這麼說的原因是因為我已經閱讀了谷歌可以給我的幾乎所有與此問題相關的文章,並嘗試了幾種不同的方法,修復,建議,重新安裝,配置等。都無法提供幫助修復甚至調試我遇到的問題。換句話說,這絕對不是一個重複的問題。我花了幾個小時閱讀以確保!

我已經安裝成功了nginxphp-fpm使用https://github.com/josegonzalez/homebrew-php。值得信賴的好人brew doctor確認一切都是最新的,並且我已經安裝了所有必要的東西(XQuartz、Xcode 命令列工具等)。

以下是一些文件摘錄,在嘗試了解我的設定時可能有用:

php-fpm 日誌

tail -f /usr/local/var/log/php-fpm.log

[24-Dec-2013 00:05:59] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[24-Dec-2013 00:05:59] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[24-Dec-2013 00:05:59] NOTICE: fpm is running, pid 31745
[24-Dec-2013 00:05:59] NOTICE: ready to handle connections

如果我錯了,請糾正我,但這似乎表明 php-fpm 運行正確

我的 php-fpm 設定檔中唯一的變化

/usr/local/etc/php/5.4/php-fpm.conf 從第 145 行開始

; 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 = myusername
group = staff

我之所以將其更改為是因為這就是我的目錄myusername:staff中設定 user:group 的方式。~這可能是問題的一部分,我不確定。

我確實遇到了所有常見的文件權限問題,並且可以確認 /clients/mywebsite/local 中的所有文件都已使用chown -R myusername:staff ./和修復了此問題chmod -R 0755 ./。考慮到這一點,希望這不應該是權限問題。

nginx 設定檔

/usr/local/etc/nginx/nginx.config

#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       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            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;
    #    server_name  localhost;

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}

我對位於 /usr/local/etc/nginx/nginx.conf.default 中的原始文件進行的唯一更改是將 index.php 添加到該location / {區塊並取消註釋該location ~ \.php$ {區塊以允許 php-fpm 處理.php文件

還值得一提的是,我創建了一個/usr/local/etc/nginx/conf.d/mywebsite.conf文件並添加127.0.0.1 mywebsite.local到我的主機文件中,該文件允許我訪問http://mywebsite.local.

Nginx 似乎設置正確,因為我可以訪問資料夾http://mywebsite.local/test.html中的絕對文件/clients/mywebsite/local/web/test.html,但對於 PHP 文件,這是一個不同的故事。它們只是被瀏覽器下載為 PHP 文件,根本沒有被執行。

我的網站設定檔

/usr/local/etc/nginx/conf.d/mywebsite.conf

server {
    listen   80;
    listen   [::]:80 default ipv6only=on;
    server_name  mywebsite.local;

    location / {
        root   /clients/mywebsite/local/web;
        index  index.php index.html index.htm;
    }

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

我假設這裡的任何部分都將被覆蓋,http://mywebsite.local並且這裡不存在的任何部分都取自普通/usr/local/etc/nginx/nginx.conf文件。

另外值得一提的是,我的/usr/local/var/log/nginx/error.log文件沒有回任何錯誤。每次我對檔案進行更改時.conf,我都會使用該命令重新啟動 nginx sudo nginx -s reload,並可以確認php-fpm進程nginx正在運行。

我還閱讀了有關更改127.0.0.1:9000似乎/var/run/php5-fpm/php5-fpm.sock不在我的配置中的信息。我嘗試使用find甚至使用ack來搜尋它的存在,但它不在我的機器上。我還閱讀了有關將此連接埠更改為:9000已使用連接埠以外的其他連接埠的資訊。由於這是我的第一次安裝,我很確定情況並非如此,但我將如何測試?

我還閱讀了有關位於的另一個文件的信息,/usr/share/nginx/html但同樣該文件不存在。

好了,您已經讀到這裡了,謝謝!如果您無論如何都能提供幫助,我真的很感謝您花時間這樣做。

答案1

您可以在預設檔案(在sites-available目錄中)中指定伺服器位置,然後重新載入或重新啟動nginx,而不是在nginx.conf檔案中指定PHP部分:

server { 
        listen 80;
        listen [::]:820; #ipv6only=on;
        root /var/www/;  ##assuming your PHP application is in /var/www/
        index index.php index.html index.htm;
        server_name PHPApp;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

另外,請確保您的 nginx.conf 檔案具有以下指令:

include /etc/nginx/sites-enabled/*;

答案2

我認為問題是你沒有上游告訴 nginx 將 php 檔案發送到哪裡,在你的 http 區塊中是這樣的:

upstream php {
    server unix:/var/run/php-fpm.socket;
}

unix:/var/run/php-fpm.socket必須在 php 配置的listen 指令中指出其中的位置。

順便一提:這不是一個好的做法位置塊內的根也不多個索引指令

相關內容