提供 PHP 文件作为下载,而不是执行它们
Posted
技术标签:
【中文标题】提供 PHP 文件作为下载,而不是执行它们【英文标题】:Serving PHP files as downloads, instead of executing them 【发布时间】:2017-07-28 14:13:36 【问题描述】:我最近在我的机器上安装了 nginx 和 php 7.0.16,但由于某种原因,nginx 下载了 php 文件,而不是执行它们。我已经花了几天时间实施了所有在线可用的解决方案,但都是徒劳的。
我的 nginx.conf 是:
worker_processes 4;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.fedora.
include /usr/share/nginx/modules/*.conf;
events
worker_connections 1024;
http
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
conf.d 文件夹中没有文件,启用站点的只有默认文件,如下所示
server
listen 80;
server_name infrastructure;
root /home/infra/index;
index index.php index.html index.htm;
#return 301 https://$server_name$request_uri;
location /
try_files $uri $uri/ = 404;
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
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;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
有人可以请教,可能是什么问题?
【问题讨论】:
在这里查看。类似问题:***.com/questions/25591040/… 【参考方案1】:找到了解决方案。问题出在 nginx.conf 文件中。
替换以下行:
default_type application/octet-stream;
与:
default_type text/html;
【讨论】:
我遇到了这个。为什么 nginx 一开始就默认这样做!?【参考方案2】:Nginx 可作为 Ubuntu 16.04 的软件包提供,我们可以安装它。
apt-get -y install nginx
之后启动nginx:
service nginx start
然后打开 localhost 页面,看看会发生什么。
安装 PHP 7
我们可以通过 PHP-FPM 使 PHP 在 nginx 中工作(PHP-FPM(FastCGI 进程管理器)是一种替代的 PHP FastCGI 实现,具有一些对任何规模的站点,尤其是繁忙的站点有用的附加功能),我们安装如下:
apt-get -y install php7.0-fpm
PHP-FPM 是一个守护进程(使用初始化脚本 php7.0-fpm),它在套接字 /run/php/php7.0-fpm.sock 上运行 FastCGI 服务器。
nginx 配置在我们现在打开的 /etc/nginx/nginx.conf 中:
nano /etc/nginx/nginx.conf
该配置易于理解(您可以在此处了解更多信息:http://wiki.nginx.org/NginxFullExample 和此处:http://wiki.nginx.org/NginxFullExample2)
首先(这是可选的)将keepalive_timeout调整到一个合理的值:
[...]
keepalive_timeout 2;
[...]
虚拟主机在 server 容器中定义。默认 vhost 定义在文件 /etc/nginx/sites-available/default 中 - 我们修改如下:
nano /etc/nginx/sites-available/default
[...]
server
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location /
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht
deny all;
[...]
server_name _;使其成为默认的包罗万象的虚拟主机(当然,您也可以在此处指定主机名,例如 www.example.com)。
根 /var/www/html;表示文档根是目录/var/www/html。
PHP 的重要部分是位置 ~ .php$ 节。取消注释以启用它。
现在保存文件并重新加载 nginx:
service nginx reload
接下来打开/etc/php/7.0/fpm/php.ini...
nano /etc/php/7.0/fpm/php.ini
...和set cgi.fix_pathinfo=0:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
重新加载 PHP-FPM:
service php7.0-fpm reload
现在在文档根目录 /var/www/html 中创建以下 PHP 文件:
nano /var/www/html/info.php
<?php
phpinfo();
?>
现在我们在浏览器中调用该文件(例如http://localhost/info.php):
【讨论】:
感谢@Pradeep 的回复。不幸的是它没有用.. :(【参考方案3】:使用 php-fpm 时,我在 /etc/nginx/sites-available/default 中取消了对这个块的注释
location ~ \.php$
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
【讨论】:
【参考方案4】:您需要像第一个一样为 PHP 设置一个位置块
location ~ \.php$
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
location ~ \.php$
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
【讨论】:
【参考方案5】:无需删除 php 处理程序, 注释掉或删除该行
#php_admin_value engine Off
它应该可以工作。
【讨论】:
【参考方案6】:我无法发表评论,但我有类似的情况。除了我没有 .../sites-available 文件夹。 我在 ubuntu 20.04,我有 php7.4-FPM。 这是我的 nginx.conf :
## Main ##
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;
## Contexts (or Blocks) ##
events
worker_connections 1024;
http
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"request" $body_bytes_sent "http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
# tcp_nopush on;
keepalive_timeout 65;
# gzip on;
include /etc/nginx/conf.d/*.conf;
server
server_name my-domain.com www.my-domain.com; //I hid the name, it's not that in reality
#- deux blocs suivants en alternance
location /
root /www/wwwroot/gout-cha;
index index.html index.php home.html home.php;
#return 301 https://$server_name$request_uri;
location ~ \.php$
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4- fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/gout-cha.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/gout-cha.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
server
if ($host = www.gout-cha.com)
return 301 https://$host$request_uri;
# managed by Certbot
if ($host = gout-cha.com)
return 301 https://$host$request_uri;
# managed by Certbot
listen 80;
server_name gout-cha.com www.gout-cha.com;
return 404; # managed by Certbot
【讨论】:
以上是关于提供 PHP 文件作为下载,而不是执行它们的主要内容,如果未能解决你的问题,请参考以下文章