[英]nginx php fastcgi unable to write , permission denied even using his own permission
嗨,我在使用像 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
然后重新启动
(i) 如果您已将 user:group 设置为 nginx 并且权限为 0755,那么您没问题 (ii) 如果您仍然有权限被拒绝,请检查 selinux。 IE
$sudo setenforce 0
(iii) 禁用 selinux,检查您现在是否可以写入。
请不要禁用您的 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。
您必须向 nginx:nginx (chown nginx:nginx) 或您启动 nginx 服务的任何用户授予访问您的 web 文件夹的权限。 然后重新启动服务 web。 更多信息请在此处输入链接描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.