首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nginx php fastcgi无法写入,权限被拒绝,甚至使用自己的权限

nginx php fastcgi无法写入,权限被拒绝,甚至使用自己的权限
EN

Stack Overflow用户
提问于 2012-10-28 00:07:52
回答 4查看 17.4K关注 0票数 5

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

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

这是php代码

代码语言:javascript
运行
复制
<?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");
?>

结果如下:

代码语言:javascript
运行
复制
/var/www/html
script owner : nginx

script getmyuid : 496

script getmygid : 493

代码只是简单地写到文件/var/www/html/X (文件还没有创建),错误如下

代码语言:javascript
运行
复制
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/中的目录列表

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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;
      }
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-31 08:02:45

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

从…

代码语言:javascript
运行
复制
PHPUSER=php

代码语言:javascript
运行
复制
PHPUSER=nginx

然后重新启动

票数 5
EN

Stack Overflow用户

发布于 2017-04-27 21:32:13

(i)如果您已将user:group设置为nginx,并且权限为0755,则您是ok的(ii)如果您的权限仍被拒绝,请选中selinux。i.e

$sudo设置强制执行0

(iii)在禁用selinux的情况下,检查现在是否可以写入。

票数 2
EN

Stack Overflow用户

发布于 2018-05-29 18:02:45

PLease不会禁用您的SELinux。以下是克服这个问题需要小心采取的步骤:首先,selinux是系统的中心安全性。它处理所有对文件的读写操作。

1) /var/www中的所有文件夹都必须具有权限755。

你可以通过: find "folder“-type d -exec chmod 755 {} \;

2)所有文件必须具有权限644:

查找文件夹-type f -exec chmod 644 {} \;

3)您需要写入或创建文件的具体文件夹必须有0777权限。

chmod 0777“文件夹”

4)然后告诉SELinux允许在特定目录或特定文件中写入:

chcon -t httpd_sys_rw_content_t test.txt

既不设置你所有的文件夹权限为777,也不禁用你的Selinux。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13101820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档