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

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

在经历了许多小时的挫折之后 ,我想最好先问问以前可能做过的人! 我已尽力提供所有的信息,但如果有什么可能有用的或我错过了, 请不要犹豫,在评论中提问

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

我已经使用https://github.com/josegonzalez/homebrew-php成功安装了nginxphp-fpm 。 良好的信任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-fpmconfiguration文件中唯一的改变

/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 ./进行了修复。 考虑到这一点,希望这不应该是一个权限问题。

nginxconfiguration文件

/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 / { block并取消注释location ~ \.php$ { block以允许php-fpm处理.php文件

还值得一提的是,我创build了一个/usr/local/etc/nginx/conf.d/mywebsite.conf文件,并在我的主机文件中添加了127.0.0.1 mywebsite.local ,这个文件允许我访问http://mywebsite.local

Nginx似乎设置正确,因为我可以在我的/clients/mywebsite/local/web/test.html文件夹中访问http://mywebsite.local/test.html绝对文件,但对于PHP文件,这是一个不同的故事。 他们只是被浏览器下载为一个PHP文件,他们根本没有被执行。

mywebsiteconfiguration文件

/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文件进行更改时,我都会使用命令sudo nginx -s reload重新启动nginx,并且可以确认php-fpmnginx进程正在运行。

我也读过关于将127.0.0.1:9000更改为/var/run/php5-fpm/php5-fpm.sock ,这似乎不在我的configuration中。 我试图使用find甚至使用acksearch它的存在,但它不是在我的机器上。 我还读了关于将此端口更改为以下内容的其他信息:9000如果已经被使用)。 由于这是我的第一次安装,我敢肯定事实并非如此,但是我该如何去testing呢?

我也读过/usr/share/nginx/html另一个文件,但是这个文件不存在。

那么,你已经读了这么远,所以谢谢你! 如果你能够帮忙,我真的很感谢你这样做。

不要在nginx.conf文件中指定PHP部分,而是可以在默认文件(站点可用目录中)中指定服务器位置,并重新加载或重新启动nginx:

 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/*; 

我认为问题是你没有一个上游告诉nginx在哪里发送php文件,就像你的http块:

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

其中unix:/var/run/php-fpm.socket必须在php config的指令listen中指明。

顺便说一句:不是一个好的做法,有一个位置块内的根,也不是多个索引指令 。