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

时间:2017-03-08 06:03:01

标签: nginx php-7 php-7.1

我最近在我的机器上安装了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;
}

有人可以提出建议,可能是什么问题?

5 个答案:

答案 0 :(得分:9)

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

替换为以下行:

default_type        application/octet-stream;

使用:

default_type        text/html;

答案 1 :(得分:4)

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

apt-get -y install nginx

之后启动nginx:

service nginx start

然后打开localhost页面,看看会出现什么。

安装PHP 7

我们可以通过PHP-FPM使PHP在nginx中工作(PHP-FPM(FastCGI Process Manager)是一种替代的PHP FastCGI实现,其中一些附加功能对任何大小的站点都有用,特别是繁忙的站点)我们按如下方式安装:

apt-get -y install php7.0-fpm

PHP-FPM是一个守护进程(使用init脚本php7.0-fpm),它在socket /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 _;使这成为一个默认的catchall vhost(当然,你也可以在这里指定一个主机名,如www.example.com)。

root / 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

现在在文档root / var / www / html中创建以下PHP文件:

nano /var/www/html/info.php

<?php
phpinfo();
?>

现在我们在浏览器中调用该文件(例如http://localhost/info.php):

答案 2 :(得分:0)

使用php-fpm时,我在/ etc / nginx / sites-available / default中取消注释了这个bloc

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

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

#php_admin_value engine Off

应该可以。