微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

嗨’我使用写入文件功能,如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 /里面的dir列表

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

然后重启

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。