aws ec2上的php文件写入权限被拒绝

时间:2018-06-12 06:23:36

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

我尝试通过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行上/xxx/test.php中没有此类文件或目录

尝试

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

结果

  

警告:fopen(/xxx/test.txt):无法打开流:xxx行上/xxx/test.php中的权限被拒绝

1 个答案:

答案 0 :(得分: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