【问题标题】:php file write permission denied on aws ec2php文件写入权限在aws ec2上被拒绝
【发布时间】:2018-11-21 10:56:55
【问题描述】:

我尝试通过 php 在 aws ec2 上写入文件。

使用 nginx 和 php-fpm。

nginx www.conf

$ vi /etc/php-fpm-7.0.d/www.conf 

; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or @php_fpm_prefix@) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

文档根是...

$ ll
total 56
drwxrwxr-x 20 apache apache 4096 Jun 12 06:34 ...
... 
...

我的来源和结果是……

我试过了

$fileName = 'test.txt';
$contents = 'hello ';
$currentPath = dirname(__FILE__);

$fgc = file_get_contents($currentPath.'/'.$fileName);
file_put_contents($contents, $fgc);

结果

警告:file_get_contents(/xxx/test.txt):打开流失败:/xxx/test.php 第 xx 行没有这样的文件或目录

试过

$fp = fopen($currentPath.'/'.$fileName, "w");
fwrite($fp, $contents);
fclose($fp);

结果

警告:fopen(/xxx/test.txt):打开流失败:/xxx/test.php 第 xx 行的权限被拒绝

【问题讨论】:

  • 看来您需要修复文件\目录权限
  • nginx 将(我认为)以www-data 用户身份运行,因此这些文件需要由该用户和/或组读取和写入。
  • 错误信息似乎很清楚。在第一种情况下,您试图打开一个不存在的文件,而在第二种情况下,您没有正确的文件/文件夹写入权限。
  • aws ec2(ami) 的正确权限是什么?

标签: php amazon-web-services amazon-ec2


【解决方案1】:

在PHP中,检查文件夹是否可写,如果不是,

if ( ! is_writable($currentPath.'/'.$fileName)){

    echo 'Not writable!!!';
}

然后,去ec2 via terminal,需要访问权限,使用命令

sudo chown -R www-data:www-data /var/www/html/YOUR-PROJECT-FOLDER

【讨论】:

  • 你是怎么知道操作系统的?
  • 我输入了 $ sudo chown -R www-data:www-data /xxx/ 结果 chown: invalid user: ‘www-data:www-data’
  • 请尝试 - sudo chown -R www-data /xxx/ ,好像是 www-data 组不存在,只有用户 www-data 存在。
  • 代码结果显示'not writeable'和chown显示无效用户'www-data'
  • 请尝试 - sudo chown -R www-data /var/www/html/YOUR-PROJECT-FOLDER
猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2017-07-11
  • 2017-03-24
  • 2021-11-09
相关资源
最近更新 更多