[英]Nginx serves .php files as downloads, instead of executing them
我正在 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
它正在工作但是...如果我 go 到主要http://5.101.99.123
它正在下载我的索引。 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
我的/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
}
...
其他“位置”评论...
.
尝试这个:
编辑/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。 谷歌上它以了解更多信息。
我有类似的问题,通过清空浏览器缓存得到了解决(也适用于不同的浏览器)。
您需要将此添加到 /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;
}
}
我在上面看到了很多解决方案,其中很多对我来说都是正确的,但我不明白他们在做什么,并且担心只是复制粘贴代码,特别是fastcgi 。 所以这是我的 2 美分,
对于某些服务器,如 Apache,内置了对解释 PHP 的支持,因此不需要 CGI。
这个数字海洋链接很好地解释了安装 FPM 的步骤,我没有编写解决 php 文件下载而不是渲染问题所需的步骤,因为其他答案恕我直言。
更新 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;
}
访问此处了解完整详情详情请点击此处
我有同样的问题,没有一个答案能解决问题。
我跑了:
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
所以我进入了配置文件,在最后一行有
#}
我取消注释,再次运行测试命令,它工作
这对我有用。
1) 我的应用程序文件
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
对我来说,它有助于在 /index.php 的末尾添加?$query_string
query_string,如下所示:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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 的情况下,上面的代码片段对我有用
如果任何建议的答案都不起作用,请尝试以下操作:
listen = 127.0.0.1:9000;(delete all line contain listen= )
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.
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }
sudo 服务 nginx 重启
服务 php5-fpm 重启
在 /usr/share/nginx/html 中创建任何 php 文件并在“server_name/file_name.php”中运行(server_name 取决于您的配置,通常是 localhost,file_name.php 是在 /usr/share/nginx 中创建的文件的名称/html )。
我正在使用 Ubuntu 14.04
上面的答案似乎对我达到的解决方案发表了太多评论。 这是我的文件的样子:
/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;
}
希望这可以帮助一些在周日下午感到沮丧的人(c:
对于任何与 PHP 7 有相同问题的人,这是我为使 nginx 在 CentOS 7 中正确执行 php 文件所做的工作,张贴在这里,以防万一有人遇到同样的问题:
在Digital Ocean上逐步按照此文档进行操作。
打开/etc/nginx/conf.d/default.conf
(默认情况下我没有启用站点或站点可用,您可以相应地进行编辑)。
编辑location
参数如下:
默认.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)
等方式运行...
希望这个有用和快乐的编码
我的解决方案是添加
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
对我不起作用。
我的情况是我没有使用/etc/nginx/sites-available/default
我使用的是不同的服务器块配置文件(例如 example.com),我能够解决此问题的唯一方法是删除默认服务器块配置文件符号链接:
$ rm /etc/nginx/sites-enabled/default
然后重新加载 Nginx:
$ sudo systemctl reload nginx
所以这就是在我的案例中最终起作用的重写规则的罪魁祸首
我改变了nginx重写规则如下..
location /vendors { rewrite ^/vendors/?$ /vendors.php break; }
变成……
location /vendors { rewrite ^/vendors/?$ /vendors.php last; }
显然没有last关键字,请求没有被重新启动,所以它从来没有命中.php
位置段,并且被简单地解释为下载——
作为记录,我发现我的 php-fpm 没有运行,我用service php7.2-fpm start
修复了它。
我现在用这段代码解决了我的问题(更改你的 IP):
location / {
access_log off;
log_not_found off;
client_max_body_size 2000m;
client_body_buffer_size 512k;
proxy_buffering on;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_buffer_size 64k;
proxy_buffers 32 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_connect_timeout 300s;
proxy_http_version 1.1;
proxy_set_header Range "";
proxy_pass https://123.123.123.123:444;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
Ubuntu 16.04 对我有用,而 php7 删除了这一行
fastcgi_split_path_info ^(.+\.php)(/.+)$;
之后它停止下载 php 文件。
取消注释 /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;
}
如果还有什么对你没有帮助。 也许更早你安装了带有 info.php 测试文件的 apache2。 只需清除本地主机的应用程序数据(缓存、cookie)。
检查您的 nginx 配置文件扩展名是 *.conf。
例如:/etc/nginx/conf.d/myfoo.conf
我遇到了同样的情况。 在我将我的配置文件从 myfoo 重命名为 myfoo.conf 后,它修复了。 重命名后不要忘记重新启动nginx。
首先,您必须在浏览器中
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
然后转到浏览器并享受...
对我来说就是这条线:fastcgi_pass unix:/var/run/php5-fpm.sock;
必须是:fastcgi_pass unix:/run/php5-fpm.sock;
我正准备尝试解决这个问题,对我来说,问题是 Cloudflare 缓存了 php 文件并不断让我下载它。
我的解决方法是清除 Cloudflare 上的缓存。
我遇到了同样的问题,如果你有 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;
}
还有一件事要检查:如果你在设置 PHP之前设置了 HTTPS 访问——我使用了 certbot——你需要在 /etc/nginx/sites-available/default 中进行两次更改,因为会有两个服务器块(一个监听端口 80,一个监听端口 443)。
(我主要是为电子邮件设置这个服务器,当我第一次安装 nginx 时并没有使用 PHP,只是为了更轻松地运行 certbot。)
我在这个问题上苦苦挣扎了很长时间,这些步骤对我有用。
第 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;
}
第 2 步:在配置文件中添加 fastcgi_param 我们只需要打开 /etc/nginx/fastcgi_params 文件并在文件末尾添加以下行。
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
然后重启服务,
systemctl restart php7.3-fpm
systemctl restart nginx
清空浏览器缓存对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.