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

Rav*_*ngh 6 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/*;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Rav*_*ngh 9

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

替换以下行:

default_type        application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

有:

default_type        text/html;
Run Code Online (Sandbox Code Playgroud)

  • 我碰到了这个.为什么nginx默认情况下首先会这样做!? (2认同)

Pra*_*ota 6

Nginx 可作为 Ubuntu 16.04 的软件包使用,我们可以安装它。

apt-get -y install nginx
Run Code Online (Sandbox Code Playgroud)

之后启动nginx:

service nginx start
Run Code Online (Sandbox Code Playgroud)

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

安装 PHP 7

我们可以通过 PHP-FPM 使 PHP 在 nginx 中工作(PHP-FPM(FastCGI 进程管理器)是另一种 PHP FastCGI 实现,具有一些对任何规模的站点,尤其是繁忙站点有用的附加功能),我们安装如下:

apt-get -y install php7.0-fpm
Run Code Online (Sandbox Code Playgroud)

PHP-FPM 是一个守护进程(带有初始化脚本 php7.0-fpm),它在套接字 /run/php/php7.0-fpm.sock 上运行 FastCGI 服务器。

nginx 配置在我们现在打开的 /etc/nginx/nginx.conf 中:

nano /etc/nginx/nginx.conf
Run Code Online (Sandbox Code Playgroud)

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

首先(这是可选的)将 keepalive_timeout 调整为一个合理的值:

[...]
    keepalive_timeout   2;
[...]
Run Code Online (Sandbox Code Playgroud)

虚拟主机在服务器 {} 容器中定义。默认的 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;
 }
}
[...]
Run Code Online (Sandbox Code Playgroud)

服务器名称 _; 使其成为默认的通用虚拟主机(当然,您也可以在此处指定主机名,例如 www.example.com)。

根 /var/www/html; 意味着文档根目录是目录/var/www/html。

PHP 的重要部分是位置 ~ .php$ {} 节。取消注释以启用它。

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

service nginx reload
Run Code Online (Sandbox Code Playgroud)

接下来打开/etc/php/7.0/fpm/php.ini ...

nano /etc/php/7.0/fpm/php.ini
Run Code Online (Sandbox Code Playgroud)

... 和 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
[...]
Run Code Online (Sandbox Code Playgroud)

重新加载 PHP-FPM:

service php7.0-fpm reload
Run Code Online (Sandbox Code Playgroud)

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

nano /var/www/html/info.php

<?php
phpinfo();
?>
Run Code Online (Sandbox Code Playgroud)

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

  • 感谢@Pradeep 的回复。不幸的是它不起作用.. :( (2认同)