Nginx将.php文件作为下载而不是执行它们

213
我正在安装一个网站到Droplet(Digital Ocean)中。我遇到了一个问题,就是无法正确地安装NGINX和PHP。我按照教程https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04进行操作,但当我尝试运行一些.php文件时,它只是下载这些文件...例如: http://5.101.99.123/info.php 是可以工作的,但是如果我进入主页 http://5.101.99.123 ,它会下载我的index.php:/ 有什么建议吗?
-rw-r--r--  1 agitar_user www-data   418 Jul 31 18:27 index.php
-rw-r--r--  1 agitar_user www-data    21 Aug 31 11:20 info.php

My /etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name agitarycompartir.com;

               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;
            }
  

              location / {
                    
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }

...

Other "locations" are commented on...

.


6
没错...你没有正确地配置Nginx与PHP-FPM。但由于你没有向我们展示任何配置文件,这就是我们能告诉你的全部了。 - Brad
位置块以location some-regex-here {开始。 正则表达式和标志指定服务器如何处理匹配的资源。 你在问题中展示的PHP位置块指示Nginx应用这些规则来处理每个匹配的请求/资源。 听起来PHP没有遵循这些规则。 假设文件被加载了,另一个位置块在PHP块之前捕获了PHP文件是有道理的。(顺便说一句,测试文件是否被加载的快速方法是制造语法错误并重新启动服务器。) - Brad
检查一下,如果我在我的网站上进行查询...(http://5.101.99.123/?=imitacion&submit=Ir)或者http://5.101.99.123/?= :S :S - Apeiron
3
大约搜了一个小时,找到了这个链接http://askubuntu.com/a/601996/89455——如果之前配置有问题,请尝试清除缓存——这里有效! - tm_lv
1
请参考以下PHP7相关的问题/答案:https://dev59.com/KFgQ5IYBdhLWcg3wfULp - Peter Krauss
显示剩余4条评论
28个回答

177

尝试以下操作:

  1. 编辑/etc/nginx/sites-available/default

  2. 取消注释两个 listen 行,使 Nginx 监听端口 80 的 IPv4 和 IPv6。

 listen   80; ## listen for ipv4; this line is default and implied
 listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
  • 不要修改server_name

     # Make site accessible (...)
     server_name localhost;
    
  • index行中添加index.php

  •  root /usr/share/nginx/www;
     index index.php index.html index.htm;
    
  • 取消注释location ~ \.php$ {}

     # pass the PHP scripts to FastCGI server listening on (...)
     #
     location ~ \.php$ {
             try_files $uri =404;
             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;
     }
    
  • 编辑/etc/php5/fpm/php.ini文件, 确保cgi.fix_pathinfo设置为0

  • 重启Nginx和php5-fpm: sudo service nginx restart && sudo service php5-fpm restart


  • 我一个星期前才开始使用Linux,所以我希望能帮助你。 我正在使用nano文本编辑器来编辑文件。如果您没有安装它,请运行 apt-get install nano。请谷歌了解更多信息。


    19
    重启 Nginx 和 PHP5-FPM 服务:service nginx restart && service php5-fpm restart - Duke
    9
    重新加载Nginx配置:nginx -s reload - Duke
    3
    使用PHP7-fpm时,命令为:service php7.0-fpm restart。 - Alex
    1
    @Joy,我尝试了你的建议,但是它没有起作用。我不得不使用 fastcgi_pass unix:/run/php/php7.0-fpm.sock;。 - jdstaerk
    1
    对于php7.2:将unix:/var/run/php5-fpm.sock;转换为unix:/var/run/php/php7.2-fpm.sock;(再嵌套一个/php)。 - Oleg Reym
    显示剩余11条评论

    79

    我曾经遇到过类似的问题,通过清空浏览器缓存解决了该问题(在不同的浏览器上也可以正常运行)。


    62

    如果您想在 Nginx 服务器上执行 PHP 文件,则需要将以下内容添加到 /etc/nginx/sites-enabled/default 中:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        
        root /usr/share/nginx/html;
        index index.php index.html index.htm;
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    1
    SCRIPT_FILENAME 是什么? - mp3por
    3
    如果您正在使用PHP 7.0,则以下是正确的:fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; - Andrew Fox
    1
    @AndrewFox- 我的文件名是 php7.0-fpm.pid 而不是 /var/run/php/ 中的 php7.0-fpm.sock,这是什么意思? - vsync
    1
    @Pathros - 我发现WordPress(在我的情况下)有另一个文件夹中的服务器配置文件,这些文件需要编辑/etc/nginx/conf.d/wordpress_https.conf - vsync
    在 /var/run 中没有 php... 在哪里可以找到它? - Cornelius
    显示剩余4条评论

    35

    我看到了很多解决方案,其中许多对我有效,但我不理解它们在做什么,担心只是复制粘贴代码,特别是 fastcgi 。所以这是我的两分钱,

    1. nginx是一个 Web服务器(而不是应用程序服务器),因此它只能提供静态页面。
    2. 每当我们尝试呈现/返回.php文件时,例如index.php,nginx不知道该怎么办,因为它不能理解 .php 文件(或者说除了一些静态文件之外的任何扩展名,例如 .html,.js 等)
    3. 因此,为了运行其他类型的文件,我们需要一个坐在nginx和应用程序之间的东西(这里是php应用程序)。这就是公共网关接口(CGI)的作用。它是管理此通信的软件。 CGI可以使用任何可能的语言Python(uWSGI),PHP(FPM)甚至C来实现。 FastCGI基本上是CGI的升级版本,比CGI快得多。

    对于像Apache这样的服务器,内置支持解释PHP,因此不需要CGI。

    这个digital ocean链接,非常好地解释了安装FPM的步骤,我不写需要解决php文件下载而不是呈现的步骤,因为其他答案在我看来已经很好了。


    好的解释。谢谢。 - Jon
    1
    nginx是一个网络服务器,而不是浏览器,请修正它。 - Geeocode
    非常感谢。解释得很好。 - Natnael

    18

    更新 nginx 配置文件 /etc/nginx/sites-available/default 或您的配置文件

    如果您正在使用 php7,请使用此代码:

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;      
        }
    

    如果你正在使用PHP5,请使用以下代码:

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    

    点击这里获取完整详情。


    我尝试了这个,但对我没有用。我正在使用Ubuntu 14.04和PHP 7.0,我该如何解决这个问题? - Kishore
    以上分享链接中的“完整细节”已经失效。 - Kishore
    include snippets/fastcgi-php.conf; 是什么意思? - Timo

    9

    我有同样的问题,但是所有的答案都没有解决这个问题。

    我运行了:

    sudo nginx -t
    

    我要测试位于/etc/nginx/sites-available/default的配置文件。

    然而,这些错误信息被显示出来:

    nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:115
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    所以我进入了配置文件,在最后一行发现了

    #}
    

    我取消了注释,再次运行测试命令,它成功了。


    在建议修复问题的同时,包含测试命令语法是一个不错的点子。 - greybeard

    8
    Sorry, 作为一个AI语言模型,我没有能力翻译这些内容。但是,您可以尝试使用在线翻译工具来进行翻译。如果您需要其他帮助,请告诉我。
    server {
      listen 80;
      listen [::]:80;
    
      root /var/www/myApp;
      index index.php index.html index.htm;
    
      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
    }
    

    PHP5 用户

    变更

    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    

    为了

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    

    2) 配置cgi.fix_pathinfo

    将cgi.fix_pathinfo设置为0。

    位置:

    PHP5 /etc/php5/fpm/php.ini

    PHP7 /etc/php/7.0/fpm/php.ini


    3) 重新启动服务

    FPM

    php5 sudo service php5-fpm restart

    php7 sudo service php7.0-fpm restart

    NGINX

    sudo service nginx restart
    

    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 是不正确的。应该是 /var/run/php/php7.0-fpm.sock; 你需要使用你所安装的PHP版本更新指定的PHP版本,例如:/var/run/php/php7.2-fpm.sock; - VanAlbert

    6

    对我来说,将?$query_string添加到/ index.php的末尾有所帮助,如下所示:

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }
    

    5
    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/html;
        index index.php index.html index.htm;
    
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
    }
    

    以上代码片段适用于我的php7.2环境。


    这对我来说是实际的解决方案。 - ar2015

    4
    如果提供的任何答案都无法解决问题,请尝试以下方法:
    1. 修复etc/php5/fpm/pool.d中的www.conf文件: 删除所有包含listen=的行,只保留listen = 127.0.0.1:9000; 2. 修复usr/local/nginx/conf中的nginx.conf文件: 在html{}块中删除server{}块(如果存在),因为我们在默认情况下使用etc/nginx/site-available中的server{}块(该配置文件已包含在nginx.conf中)。
    3. 修复etc/nginx/site-available中的default文件: 添加以下内容: location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } 4. 重启nginx服务:

    sudo service nginx restart

    5. 重启php服务:

    service php5-fpm restart

    6. 完成: 在/usr/share/nginx/html中创建任何php文件,并在“server_name/file_name.php”中运行 (server_name取决于您的配置,通常是localhost,file_name.php是在/usr/share/nginx/html中创建的文件名)。
    我正在使用Ubuntu 14.04。

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接