我安装了全新的 CentOS7 并作为虚拟机运行。我一直在玩 Nginx,因为我一生都在使用 Apache,现在只是为了好玩和学习,我决定切换到 Nginx。我遵循这两个指南:
- How to install LEMP on CentOS7通过 digital ocean
- How to install LEMP on CentOS7通过名为 IfNotTrueThenFalse 的博客
作为我之前研究的一部分,在我摆脱想法之前,我确实阅读了 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
),文件就会被下载而不是显示它的内容。
当然,在所有这些更改之后,我已经重新启动了 nginx
和 php-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/