php - Nginx 不提供 PHP 文件

标签 php linux nginx

我安装了全新的 CentOS7 并作为虚拟机运行。我一直在玩 Nginx,因为我一生都在使用 Apache,现在只是为了好玩和学习,我决定切换到 Nginx。我遵循这两个指南:

作为我之前研究的一部分,在我摆脱想法之前,我确实阅读了 this这根本没有帮助。

在继续之前,我应该说我已经为他们每个人准备了我需要的东西,因为我想使用 PHP 7.0.x 而不是 CentOS 7 存储库附带的默认版本(我认为是 5.4)。

所以,这就是我的配置文件的样子:

/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  centos7.localdomain;  
    root   /var/www/html;
    index  index.php index.html index.htm;

    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_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

/etc/php-fpm.d/www.conf

[www]
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

...

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = nobody
listen.group = nobody

对于 www.conf,您在此处看到的那些值以外的值都是默认值。完整文件是 shared here

我创建了文件/var/www/html/index.php,除了:

<?php
    phpinfo();

只要我尝试 URL http://centos7.localdomain/index.php(或没有 index.php),文件就会被下载而不是显示它的内容。

当然,在所有这些更改之后,我已经重新启动了 nginxphp-fpm 服务,并通过运行 systemctl status nginx.service 和检查它们systemctl status php-fpm.service

/var/www/html的权限如下:

$ ls -la /var/www/html/
total 4
drwxr-xr-x. 2 root root 22 Oct  9 20:53 .
drwxr-xr-x. 3 root root 17 Oct  9 20:24 ..
-rw-r--r--. 1 root root 18 Oct  9 20:53 index.php

这是我运行的 PHP 版本:

$ php -v
PHP 7.0.11 (cli) (built: Sep 14 2016 08:28:52) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.11, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans

我在这里遗漏了什么?如果有,那是什么?或者我正在玩的这个设置有什么问题?

最佳答案

从最后一段中删除“try_files $uri =404”。这可能会解决您的问题。

location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

关于php - Nginx 不提供 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39950129/

相关文章:

c - 响应 isatty(3) 的文件

java - 在 Ubuntu-Linux 上获取 Java 中给定端口的进程名称?

ruby-on-rails - 无法在 Linode Ubuntu 10.04 框中通过 sudo mate 命令打开 nginx.conf 文件

nginx - API网关与反向代理

java - 为什么我的长整数会稍微改变一点?

PHP 转换数组键

php - 执行更新触发器后如何获取之前的记录?

php - 将 JavaScript 变量发送到 PHP 文件

c++ - 如何设置QXmlStreamReader atStart?

nginx 维护页面有内容问题