【问题标题】:Nginx + php fastcgi unable to open file, permission deniedNginx + php fastcgi 无法打开文件,权限被拒绝
【发布时间】:2014-10-20 12:37:56
【问题描述】:

在尝试访问 php 文件时,我遇到了 Nginx 和 Php fastcgi 的一些权限问题。我在 Redhat 7 中使用 5.5.15 和 Nginx 1.6.0。

我的 php 文件现在非常简单。

<?php
echo "\nscript owner : ".get_current_user()."\n";
$myFile = '/usr/share/nginx/html/test.log';
$fh = fopen($myFile, 'a') or die("can''t open file");
?>  

获取当前用户将导致:“myuser”

我得到的错误如下:

2014/08/26 22:47:14 [error] 6424#0: *16 FastCGI sent in stderr: "PHP message: PHP     Warning:  fopen(/usr/share/nginx/html/test.log): failed to open stream: Permission denied     in /usr/share/nginx/html/test.php on line 19" while reading response header from upstream, client: XXXXXX, server: XXXXXXX, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "XXXXXXX"

这里是目录/usr/share/nginx的权限(所有父目录都有x权限):

drwxrwsrwx. 4 myuser myuser 4096 Aug 26 22:32 html

运行以下命令:

$ ps aux | grep "nginx: worker process"
myuser  6423  0.0  0.3 111228  3880 ?        S    22:36   0:00 nginx: worker process
myuser  6424  0.0  0.5 111228  5428 ?        S    22:36   0:00 nginx: worker process
myuser  6480  0.0  0.0 112640   980 pts/0    R+   22:41   0:00 grep --color=auto nginx: worker process

$ ps aux | grep "php"
myuser 5930  0.0  0.1 128616  1860 pts/0    T    21:09   0:00 vi /etc/php-fpm.conf
myuser 5931  0.0  0.2 128628  2052 pts/0    T    21:09   0:00 vi /etc/php.ini
myuser  5933  0.0  0.1 128616  1864 pts/0    T    21:13   0:00 vi /etc/php-fpm.conf
myuser  5934  0.0  0.1 128616  1860 pts/0    T    21:14   0:00 vi /etc/php-fpm.d/www.conf
myuser  5935  0.0  0.1 128616  1864 pts/0    T    21:15   0:00 vi /etc/php-fpm.conf
root    6313  0.0  2.4 544732 25208 ?        Ss   22:25   0:00 php-fpm: master process (/etc/php-fpm.conf)
myuser  6314  0.0  0.8 544732  8356 ?        S    22:25   0:00 php-fpm: pool www
myuser  6315  0.0  0.8 544732  8328 ?        S    22:25   0:00 php-fpm: pool www
myuser  6316  0.0  0.9 545076  9892 ?        S    22:25   0:00 php-fpm: pool www
myuser  6317  0.0  0.9 544860  9452 ?        S    22:25   0:00 php-fpm: pool www
myuser  6318  0.0  0.9 544860  9212 ?        S    22:25   0:00 php-fpm: pool www
myuser  6483  0.0  0.0 112640   976 pts/0    R+   22:47   0:00 grep --color=auto php

我的服务器如下所示:

server {
    listen       80;       
    root   /usr/share/nginx/html;


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

}

在 nginx.config 中我使用的是同一个用户:“user ec2-user;” 我还更改了 /etc/php-fpm.d/www.conf 文件以具有相同的用户和组。

user = myuser
group = myuser

所以,Nginx 和 PHP 都在同一个用户“myuser”上运行。 日志文件和 php 文件所在的所有目录(/usr/share/nginx/html)都具有 x 访问权限,并且该用户对该 html 目录具有 777 访问权限。

不知道我错过了什么。我已经在网上搜索了 2 天,但没有运气。

【问题讨论】:

  • 嘿@user1653068,有什么解决办法吗?

标签: php nginx permissions redhat fastcgi


【解决方案1】:

我想权利是正确的,但你在 test.php 中有 3 行代码,但由于错误,这不是真的 :) 检查你运行的文件。

“在 /usr/share/nginx/html/test.php 第 19 行”

【讨论】:

  • 我实际上有空行,这就是为什么它说第 19 行。在文件中第 19 行是 "$fh = fopen($myFile, 'a') 或 die("can't open file ");"
猜你喜欢
  • 2015-12-13
  • 2015-11-15
  • 1970-01-01
  • 2013-01-22
  • 2010-11-14
  • 2014-10-28
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多