将PHP文件作为下载文件提供,而不是执行它们

10

我最近在我的机器上安装了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文件夹中没有文件,而sites-enabled只有一个类似下面的默认文件:

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

有人可以给些建议吗,可能出了什么问题?


在此处检查。类似问题:https://dev59.com/GF8e5IYBdhLWcg3wlbPx - Apeiron
5个回答

12

找到了解决方案。问题出在nginx.conf文件中。

将以下行替换为:

default_type        application/octet-stream;

与:

default_type        text/html;

2
我遇到了这个问题。为什么nginx默认情况下会这样做呢?! - lkahtz

6

Nginx 可以作为 Ubuntu 16.04 的软件包供我们安装。

apt-get -y install nginx

之后启动nginx:

service nginx start

打开本地主页,查看出现了什么。

安装PHP 7

我们可以通过PHP-FPM(PHP快速CGI进程管理器)让PHP在nginx中运行。 PHP-FPM是一种替代的PHP FastCGI实现,具有一些适用于各种规模网站的附加功能,特别是繁忙的网站。以下是安装步骤:

apt-get -y install php7.0-fpm

PHP-FPM是一个守护进程(使用init脚本php7.0-fpm),在套接字/run/php/php7.0-fpm.sock上运行FastCGI服务器。

nginx配置文件位于/etc/nginx/nginx.conf中,我们现在打开它:

nano /etc/nginx/nginx.conf

这个配置很容易理解(你可以在这里了解更多信息:http://wiki.nginx.org/NginxFullExamplehttp://wiki.nginx.org/NginxFullExample2

首先(可选),将keepalive_timeout调整为合理的值:

[...]
    keepalive_timeout   2;
[...]

虚拟主机在 server {} 容器中定义。默认的虚拟主机定义在文件 /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)。

root /var/www/html; 表示文档根目录是/var/www/html目录。

对于PHP来说,重要的部分是location ~ .php$ {} stanza。取消注释以启用它。

现在保存文件并重新加载nginx:

service nginx reload

下一步打开 /etc/php/7.0/fpm/php.ini...
nano /etc/php/7.0/fpm/php.ini

... and 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):

2
谢谢你的回复,@Pradeep。不幸的是,它没有起作用.. :( - Ravi Singh

0
使用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;
    }

0

无需删除php处理程序, 注释或删除该行

#php_admin_value engine Off

它应该可以工作。


0
你需要像在第一个中那样为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;
}

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