php - nginx php fastcgi无法写入,即使使用他自己的权限也被拒绝

标签 php nginx centos fastcgi centos6

嗨,当我使用像 file_put_contents 这样的写入文本功能时,我的权限被拒绝了。我在centos 6环境下使用nginx,我使用php-fcgi

问题只能通过将 dir 权限设置为 777 来解决,但我不想要该解决方案。

这是PHP代码

<?php
    error_reporting(E_ALL);
    header("content-type:text/plain");
    if(isset($_GET['akarapaci'])) {phpinfo();}
    echo getcwd();
    echo "\nscript owner : ".get_current_user()."\n";
    echo "\nscript getmyuid : ".getmyuid()."\n";
    echo "\nscript getmygid : ".getmygid()."\n";

    file_put_contents(dirname(__FILE__)."/X","1");
?>

结果如下:

/var/www/html
script owner : nginx

script getmyuid : 496

script getmygid : 493

代码只是简单的写入文件/var/www/html/X(文件尚未创建),并有这样的错误

2012/10/27 19:51:59 [error] 1010#0: *32 FastCGI sent in stderr: "PHP Warning:  file_put_contents(/var/www/html/X): failed to open stream: Permission denied in /var/www/html/info.php on line 10" while reading response header from upstream, client: 111.94.49.72, server: vprzl.com, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vprzl.com"

这是/var/www/中的目录列表

total 40
drwxr-xr-x 2 root  root   4096 Oct 27 08:44 backup
drwxr-xr-x 2 root  root   4096 Feb 14  2012 cgi-bin
drwxrwxrwx 3 root  root  12288 Oct 27 08:47 devel
drwxr-xr-x 3 root  root   4096 Oct 20 04:48 error
drwxrwxr-x 2 nginx nginx  4096 Oct 27 19:24 html
drwxr-xr-x 3 root  root   4096 Oct 17 18:19 icons
drwxr-xr-x 5 root  root   4096 Oct 27 16:57 images
drwxr-xr-x 2 root  root   4096 Oct 26 14:28 secret

这是我的 nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    keepalive_timeout  65;

    #gzip  on;


    server {
      listen          80;
      server_name     vprzl.com www.vprzl.com;
      index           index.html;
      root            /var/www/html/;

      location / { 
          # Rewrite rules and other criterias can go here
          # Remember to avoid using if() where possible (http://wiki.nginx.org/IfIsEvil)
          index  index.html index.htm;
      }

      location ~ \.php$ {
          include fastcgi_params;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
    }

    server {
      listen          80;
      server_name     images.vprzl.com;
      index           index.html;
      root            /var/www/images/;

      location / { 
          # Rewrite rules and other criterias can go here
          # Remember to avoid using if() where possible (http://wiki.nginx.org/IfIsEvil)
          index  index.html index.htm;
      }
    }

    server {
      listen          80;
      server_name     secret.vprzl.com;
      index           index.html;
      root            /var/www/secret/;

      location / { 
          index  index.html index.htm;
      }
    }
}

最佳答案

找到了罪魁祸首! (我自己的回答)我只需要更改文件 /etc/init.d/php-fcgi

中的一行

来自

PHPUSER=php

PHPUSER=nginx

然后重启

关于php - nginx php fastcgi无法写入,即使使用他自己的权限也被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101820/

相关文章:

linux - 如何在 raspbian 上安装最新版本的 nginx?

php - 问题安装 php intl

php - 如何在登录表单中不区分用户名大小写?

python - Django:静态/媒体文件不同服务器

javascript - 如何从特定PHP结构中的 slider 获取值

ruby-on-rails - 连接()失败(111 : Connection refused) while connecting to upstream

linux - 在 Centos 8 上升级软件包后出现非法指令(核心转储)

linux - 使用 uuencode 发送附件时使用 sendmail 命令

PHP - 检测文件系统的字符编码

php - 使用 iframe 获取 $_SERVER ['HTTP_X_REQUESTED_WITH' ]