使用 php-fpm 的 nginx 下载 php 文件而不是在 mac os x(本地环境)上执行它们

Oli*_*pin 6 php nginx mac-osx php-fpm

我无法在本地开发机器上执行 PHP 文件。我似乎已经成功安装了nginx并且php-fpm似乎也已正确安装,并且正在运行,但无法弄清楚为什么 PHP 文件被下载而不是被执行。

在经历了很多很多小时的挫折之后,我认为最好问问以前可能做过这件事的人!我已尽力提供所有信息,但如果有任何可能有用或我遗漏的信息,请随时在评论中提问

请注意:请仔细阅读我遇到的问题。我之所以这么说是因为我已经通读了几乎所有与 Google 可以提供给我的与此问题相关的文章,并尝试了几种不同的方法、修复、建议、重新安装、配置等。但每个人都无法提供帮助修复甚至调试我遇到的问题。换句话说,这绝对不是一个重复的问题。我花了几个小时阅读以确保!

我已经成功安装了nginxphp-fpm使用https://github.com/josegonzalez/homebrew-php. 好的 ol' trustybrew 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
Run Code Online (Sandbox Code Playgroud)

如果我错了,请纠正我,但这似乎表明 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
Run Code Online (Sandbox Code Playgroud)

我将其更改为的原因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;
    #    }
    #}

}
Run Code Online (Sandbox Code Playgroud)

我从位于 /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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我假设这里的任何部分都将被覆盖,http://mywebsite.local并且这里没有的任何部分都取自普通/usr/local/etc/nginx/nginx.conf文件。

值得一提的/usr/local/var/log/nginx/error.log是,我的文件没有返回任何错误。每当我做出改变的.conf文件,我重启nginx的使用命令sudo nginx -s reload,可以确认php-fpmnginx进程正在运行。

我也看到了有关更改127.0.0.1:9000/var/run/php5-fpm/php5-fpm.sock这似乎并不在我的配置。我曾尝试使用findeven usingack来搜索它的存在,但它不在我的机器上。我还阅读了有关将此端口更改:9000为已被使用以外的其他内容的信息。由于这是我的第一次安装,我很确定情况并非如此,但我将如何进行测试?

我还阅读了位于 中的另一个文件,/usr/share/nginx/html但同样不存在。

好吧,你已经读到这里了,谢谢!如果您无论如何都能提供帮助,我真的很感激您花时间这样做。

ali*_*der 4

您可以在默认文件中指定服务器位置(在站点可用目录中),然后重新加载或重新启动 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;
        }
}
Run Code Online (Sandbox Code Playgroud)

另外,请确保您的 nginx.conf 文件具有以下指令:

include /etc/nginx/sites-enabled/*;
Run Code Online (Sandbox Code Playgroud)