php不执行成了下载文件,php - Nginx将.php文件作为下载提供,而不是执行它们 - SO中文参考 - www.soinside.com...

110

投票

试试这个:

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

取消注释两个侦听行,使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.php添加到index线

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。谷歌就此了解更多。

2

投票

上面的答案似乎对我达成的解决方案评论太多了。这是我的文件看起来像:

在/ etc / nginx的/网站可用/默认

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;

}

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

1

投票

对于任何遇到与PHP 7相同问题的人来说,这就是我在CentOS 7中正确执行nginx执行php文件所做的,这里发布了以防任何人遇到同样问题:

打开/etc/nginx/conf.d/default.conf(默认情况下,我没有启用站点,也没有站点可用,您可以相应地进行编辑)。

编辑location参数如下:

default.conf:

location ~ \.php$ {

try_files $uri =404;

#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

#instruct nginx execute php7 files instead download them :D

fastcgi_pass unix:/var/run/php-fpm/www.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

重启Nginx和PHP服务sudo systemctl restart php-fpm和sudo systemctl restart nginx。

最后但最重要的是,清除浏览器缓存或在incognito (Chrome)或Private Browsing (Firefox)等中运行...

希望这个有用且快乐的编码

1

投票

我的解决方案是添加

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;

到我的自定义配置文件,例如etc/nginx/sites-available/example.com.conf

添加到/etc/nginx/sites-available/default不适合我。

1

投票

我的情况我没有使用/etc/nginx/sites-available/default我使用的是不同的服务器块配置文件(例如example.com),我能解决此问题的唯一方法是删除默认的服务器块配置文件符号链接:

$ rm /etc/nginx/sites-enabled/default

然后重新加载Nginx:

$ sudo systemctl reload nginx

0

投票

Ubuntu 16.04对我有用,而php7正在删除这一行

fastcgi_split_path_info ^(.+\.php)(/.+)$;

之后它停止下载php文件。

0

投票

取消注释/ 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;

}

0

投票

如果其他任何事情对你没有帮助。也许早些时候你用info.php测试文件安装了apache2。只需清除localhost的App Data(缓存,cookie)即可。

0

投票

检查你的nginx配置文件扩展名是* .conf。

例如:/etc/nginx/conf.d/myfoo.conf

我遇到了同样的情况。在将我的配置文件从myfoo重命名为myfoo.conf之后,它已修复。不要忘记重命名后重启nginx。

0

投票

首先,你必须在浏览器中使用Remove cache

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

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重启nginx

然后去浏览器并享受...

0

投票

对我来说,这是一行:fastcgi_pass unix:/var/run/php5-fpm.sock;

这必须是:fastcgi_pass unix:/run/php5-fpm.sock;

39

投票

您需要将此添加到/ etc / nginx / sites-enabled / default以在Nginx Server上执行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;

}

}

0

投票

我正准备去解决这个问题,对我而言,问题是Cloudflare缓存了php文件并一直让我下载它。

我的修复是清除Cloudflare上的缓存。

0

投票

我遇到了同样的问题是什么解决了这个服务器块也有这个块高于其他位置块如果你有css没有加载问题。我添加到我的网站 - 可用的conf文件。

location ~ [^/]\.php(/|$) {

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_index index.php;

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

include fastcgi_params;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

}

0

投票

所以这就是最终在我的情况下作为重写规则的罪魁祸首

我更改了nginx重写规则如下..

location /vendors { rewrite ^/vendors/?$ /vendors.php break; }

成为...

location /vendors { rewrite ^/vendors/?$ /vendors.php last; }

显然没有最后一个关键字,请求没有重新启动,因此它从未到达.php位置段,并且只是被解释为下载 -

26

投票

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

11

投票

更新nginx config / 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;

}

访问这里获取完整的详细信息Detail here

8

投票

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

我跑了:

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

所以我进入配置文件,最后一行是

#}

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

6

投票

这对我有用。

1)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用户

更改

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

6

投票

我看到上面有很多解决方案,很多都适合我,但我不明白他们在做什么,并担心只是复制粘贴代码,特别是fastcgi。所以这是我的2美分,

nginx是一个Web浏览器(而不是应用程序浏览器),因此它只能提供静态页面。

无论何时,我们尝试渲染/返回一个.php文件,例如index.php,nginx不知道该怎么做,因为它只是无法理解.php文件(或者除了少数选择之外的任何扩展名)像.html,.js等是静态文件)

因此,为了运行其他类型的文件,我们需要一些位于nginx和应用程序之间的东西(这里是php应用程序)。这就是通用网关接口(CGI)的用武之地。它是管理这种通信的软件。 CGI可以用任何可能的语言Python(uWSGI),PHP(FPM)甚至C实现.FastCGI基本上是CGI的升级版本,它比CGI快得多。

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

This digital ocean link,解释了安装FPM的步骤相当不错,我没有编写解决php文件下载而不是渲染问题所需的步骤,因为其他答案恕我直言相当不错。

3

投票

如果任何建议的答案无效,请尝试以下方法:

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

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

在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.

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

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi_params;

}

4.restart nginx服务

sudo服务nginx重启

5.restart php服务

服务php5-fpm重启

6.enjoy

在/ usr / share / nginx / html中创建任何php文件并在“server_name / file_name.php”中运行(server_name取决于您的配置,normaly是localhost,file_name.php是在/ usr / share / nginx中创建的文件的名称/ html)。

我正在使用Ubuntu 14.04

3

投票

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

location / {

try_files $uri $uri/ /index.php?$query_string;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值