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个回答

1

请检查您的nginx配置文件扩展名是否为*.conf。
例如:/etc/nginx/conf.d/myfoo.conf

我遇到了同样的情况。将我的配置文件从myfoo重命名为myfoo.conf后,问题得到解决。 重命名后不要忘记重新启动nginx。


1
我快要疯了,一直在尝试修复这个问题。对我来说,问题在于Cloudflare缓存了php文件并一直让我下载它。解决方法是在Cloudflare上清除缓存。

也许你想告诉我们如何做到? - Thorsten Staerk

1
在我的Ubuntu 16.04和php7上,对我起作用的是删除这一行:fastcgi_split_path_info ^(.+\.php)(/.+)$;。之后就不再下载php文件了。

1

对我来说,重点是这一行:

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

它需要变成这样:

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


1

还有一件事需要检查:如果你在设置PHP之前已经设置了HTTPS访问(我使用了certbot),那么你需要在/etc/nginx/sites-available/default中进行两次更改,因为会有两个服务器块(一个监听端口80,另一个监听端口443)。

(我最初安装nginx只是为了更轻松地运行certbot,因为我主要是为了电子邮件而设置此服务器,所以并没有使用PHP。)


1

我曾经长时间苦恼于这个问题,而这些步骤对我有效。

步骤1:为所有PHP文件配置位置块

location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

步骤二:在配置文件中添加fastcgi_param 我们只需要打开/etc/nginx/fastcgi_params文件,并在文件末尾添加以下行。

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

然后重新启动服务。

systemctl restart php7.3-fpm
systemctl restart nginx

1

首先你需要在浏览器中清除缓存

然后打开终端并运行以下命令:

sudo apt-get install php-gettext
sudo nano /etc/nginx/sites-available/default

然后在default文件中添加以下代码:
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

如果有不匹配的情况,请进行更正并通过以下命令从终端重新启动Nginx:

sudo systemctl restart nginx

然后打开浏览器,享受吧...


1
请取消在 /etc/nginx/sites-available/default 中 .php 位置的注释。

sudo vi /etc/nginx/sites-available/default:

location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            # With php5-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

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