Php

AWS 寫入權限,httpd 2.4

  • August 7, 2015

Amazon Web Services,伺服器版本:Apache/2.4.6 (Red Hat Enterprise Linux)

apache日誌報告:

[Fri Aug 07 13:53:40.793562 2015] [:error] [pid 10730] [client 149.88.114.40:40800] PHP Warning:  file_put_contents(./test.txt): failed to open stream: Permission denied in /var/www/html/vraim/index.php on line 18

這是嘗試寫入目前目錄的 PHP 程式碼:

<?php
file_put_contents("./test.txt", "Test"); // <=== This cannot write!!!
$uid = posix_getuid();
echo "PosixGetUID: " . $uid . "<br/>";
$userinfo = posix_getpwuid($uid);
print_r($userinfo);
?>

目錄權限:

# namei -movl /var/www/html/vraim/
f: /var/www/html/vraim/
dr-xr-xr-x root   root /
drwxr-xr-x root   root var
drwxr-xr-x root   root www
drwxr-xr-x apache root html
drwxrwxrwx apache root vraim

虛擬主機的 httpd.conf:

DocumentRoot /var/www/html/vraim
<Directory />
  Require all granted
</Directory>

我做錯了什麼?為什麼 PHP 不能寫入 Apache 擁有的文件夾?

PHP 腳本本身會輸出以下有關其有效使用者 ID 的資訊:

PosixGetUID: 48
Array ( [name] => apache [passwd] => x [uid] => 48 [gid] => 48 [gecos] => Apache [dir] => /usr/share/httpd [shell] => /sbin/nologin )

可能來自selinux,嘗試將其設置為允許檢查:

setenforce 許可

當您將某物從一個地方移動到另一個地方時,可能會發生這種情況。當您移動它時,它會保留原始的 selinux 上下文

引用自:https://serverfault.com/questions/711663