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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25591040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:54:28  来源:igfitidea点击:

Nginx serves .php files as downloads, instead of executing them

phpnginxphp-5.5

提问by Apeiron

I am installing a website in a droplet (Digital Ocean). I have a issue for install NGINX with PHP properly. I did a tutorial https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04but when I try to run some .php file it's just downloading it... for example... http://5.101.99.123/info.phpit's working but... If I go to the main http://5.101.99.123it's downloading my index.php :/

我正在一个 Droplet (Digital Ocean) 中安装一个网站。我在正确安装带有 PHP 的 NGINX 时遇到问题。我做了一个教程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 :/

Any idea?

任何的想法?

-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

我的 /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
            }

... Others "location" are commented (#)

... 其他“位置”被注释 (#)

采纳答案by Hyman M.

Try this:

尝试这个:

  1. Edit /etc/nginx/sites-available/default

  2. Uncomment both listen lines to make nginx listen on port 80 IPv4 and IPv6.

    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
  3. Leave server_namealone

    # Make site accessible (...)
    server_name localhost;
    
  4. Add index.phpto the indexline

    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    
  5. Uncomment 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;
    }
    
  6. Edit /etc/php5/fpm/php.iniand make sure cgi.fix_pathinfois set to 0

  7. Restart nginx and php5-fpm sudo service nginx restart && sudo service php5-fpm restart

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

  2. 取消这两条监听行的注释,使 nginx 监听端口 80 IPv4 和 IPv6。

    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
  3. server_name独自离开

    # Make site accessible (...)
    server_name localhost;
    
  4. 添加index.phpindex

    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    
  5. 取消注释 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;
    }
    
  6. 编辑/etc/php5/fpm/php.ini并确保cgi.fix_pathinfo设置为0

  7. 重启 nginx 和 php5-fpm sudo service nginx restart && sudo service php5-fpm restart



I have just started using Linux a week ago, so I really hope to help you on this. I am using nano text editor to edit the files. run apt-get install nano if you don't have it. Google on it to know more.

我一周前才开始使用 Linux,所以我真的希望能在这方面帮助你。我正在使用 nano 文本编辑器来编辑文件。如果没有,请运行 apt-get install nano。谷歌上它了解更多。

回答by rootx

You need to add this to /etc/nginx/sites-enabled/default to execute php files on Nginx Server:

您需要将此添加到 /etc/nginx/sites-enabled/default 以在 Nginx 服务器上执行 php 文件:

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

回答by np8

I had similar problem which was resolved by emptying the browser cache(also worked fine with different browser).

我有类似的问题,通过清空浏览器缓存解决了(也适用于不同的浏览器)。

回答by M Arfan

Update nginx config /etc/nginx/sites-available/default or your config file

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

if you are using php7 use this

如果您使用的是 php7,请使用这个

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

if you are using php5 use this

如果您使用的是 php5,请使用这个

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

Visit here for complete detail Detail here

访问此处获取完整详细信息 此处详细信息

回答by Bjorn Lindholm Hansen

I had the same issue and none of the answers solved the problem.

我有同样的问题,没有一个答案解决了这个问题。

I ran:

我跑了:

sudo nginx -t

to test the config file at /etc/nginx/sites-available/default.

测试 /etc/nginx/sites-available/default 中的配置文件。

It gave me these errors:

它给了我这些错误:

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

So I went into the config file and on the last line there was

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

#}

I uncommented, ran the test command again and it worked

我取消注释,再次运行测试命令,它起作用了

回答by Sharif El Shobkshy

This workded for me.

这对我有用。

1) MyApp file

1) 我的应用程序文件

vi /etc/nginx/sites-available/myApp

vi /etc/nginx/sites-available/myApp

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 users

PHP5 用户

Change

改变

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

to

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


2) Configure cgi.fix_pathinfo

2)配置cgi.fix_pathinfo

Set cgi.fix_pathinfo to 0

将 cgi.fix_pathinfo 设置为 0

Location:

地点:

PHP5 /etc/php5/fpm/php.ini

PHP5 /etc/php5/fpm/php.ini

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

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



3) Restart services

3)重启服务

FPM

FPM

php5 sudo service php5-fpm restart

php5 sudo service php5-fpm restart

php7 sudo service php7.0-fpm restart

php7 sudo service php7.0-fpm restart

NGINX

NGINX

sudo service nginx restart

回答by coda

I see a lot of solutions above and many worked correctly for me, but I didn't understand what they were doing and was worried of just copy pasting the code, specifically, fastcgi. So here are my 2 cents,

我在上面看到了很多解决方案,其中许多解决方案对我来说都是正确的,但是我不明白他们在做什么,并且担心只是复制粘贴代码,特别是fastcgi。所以这是我的 2 美分,

  1. nginx is a web server(and not an application server) and thus, it can only serve static pages.
  2. whenever, we try rendering/returning a .php file, for example index.php, nginx doesn't know what to do, since it just can't understand a .phpfile (or for that matter any extension apart from a select few like .html, .jsetc. which are static files)
  3. Thus in order to run other kinds of files we need something that sits between nginx and the application (here the php application). This is where common gateway interface (CGI) comes in. It's a piece of software that manages this communication. CGIs can be implemented in any possible language Python (uWSGI), PHP (FPM) and even C. FastCGI is basically an upgraded version of CGI which is much much faster than CGI.
  1. nginx 是一个Web 服务器(而不是应用程序服务器),因此它只能提供静态页面。
  2. 无论何时,我们尝试渲染/返回一个 .php 文件,例如 index.php,nginx 不知道该怎么做,因为它无法理解.php文件(或者就此而言,除了少数几个扩展名之外的任何扩展名如.html、.js等,它们是静态文件)
  3. 因此,为了运行其他类型的文件,我们需要位于 nginx 和应用程序(这里是 php 应用程序)之间的东西。这就是通用网关接口 (CGI) 的用武之地。它是一个管理这种通信的软件。CGI 可以用任何可能的语言 Python (uWSGI)、PHP (FPM) 甚至 C 实现。FastCGI 基本上是 CGI 的升级版本,它比 CGI 快得多。

For some, servers like Apache, there is built in support to interpret PHP and thus no need for a CGI.

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

This digital ocean link, explains the steps to install FPM pretty well and I am not writing the steps needed to solve the issue of php files getting downloaded instead of rendering since the other answers IMHO pretty good.

这个数字海洋链接很好地解释了安装 FPM 的步骤,我没有编写解决 php 文件下载而不是渲染问题所需的步骤,因为其他答案恕我直言非常好。

回答by Tomeg

For me it helped to add ?$query_stringat the end of /index.php, like below:

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

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

回答by huuthang

If any of the proposed answers is not working, try this:

如果任何建议的答案不起作用,请尝试以下操作:

1.fix www.conf in etc/php5/fpm/pool.d:

1.修复etc/php5/fpm/pool.d中的www.conf:

listen = 127.0.0.1:9000;(delete all line contain listen= )

listen = 127.0.0.1:9000;(delete all line contain listen= )

2.fix nginx.conf in usr/local/nginx/conf:

2.修复usr/local/nginx/conf中的nginx.conf:

remove server block server{} (if exist) in block html{} because we use server{} in default (config file in etc/nginx/site-available) which was included in nginx.conf.

remove server block server{} (if exist) in block html{} because we use server{} in default (config file in etc/nginx/site-available) which was included in nginx.conf.

3. fix default file in etc/nginx/site-available

3.修复etc/nginx/site-available默认文件

location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }

location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }

4.restart nginx service

4.重启nginx服务

sudo service nginx restart

须藤服务 nginx 重启

5.restart php service

5.重启php服务

service php5-fpm restart

服务 php5-fpm 重启

6.enjoy

6.享受

Create any php file in /usr/share/nginx/html and run in "server_name/file_name.php" (server_name depend on your config,normaly is localhost, file_name.php is name of file which created in /usr/share/nginx/html ).

在 /usr/share/nginx/html 中创建任何 php 文件并在“server_name/file_name.php”中运行(server_name 取决于您的配置,通常是本地主机,file_name.php 是在 /usr/share/nginx 中创建的文件名/html )。

I am using Ubuntu 14.04

我正在使用 Ubuntu 14.04

回答by Benjamin McFerren

The answer above seemed to comment out too much for the solution I reached. This is what my file looked like:

上面的答案似乎对我达到的解决方案注释过多。这是我的文件的样子:

/etc/nginx/sites-available/default

/etc/nginx/sites-available/default

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

Hope this helps some folks who are frustrated on a sunday afternoon (c:

希望这能帮助一些在周日下午感到沮丧的人 (c: