如何给apache写入主目录的权限?

use*_*608 12 php apache

我的服务器在/ var/www/html我在/var/www/html/fileio_test/io_test.php中有一个php脚本

<?php

$logging = <<< LOG
This is a test
LOG;

$testfile = fopen('/home/djameson/test.txt', 'a'); 
fwrite ($testfile, $logging);
fclose($testfile);

?>
Run Code Online (Sandbox Code Playgroud)

当我尝试运行这个脚本时,我得到了

Warning: fopen(/home/djameson/test.txt): failed to open stream: Permission denied in   /var/www/html/fileio_test/io_test.php on line 7

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/fileio_test/io_test.php on line 8

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/fileio_test/io_test.php on line 9
Run Code Online (Sandbox Code Playgroud)

如何让apache写入我的主目录?服务器在fedora 20上运行.

kul*_*boj 16

由于您的文件位于主目录中,我建议使用以下方法之一.

  1. 授予0777文件本身的权限.

    chmod 0777 /home/djameson/test.txt
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将所有权更改为apache用户www-data并授予所有者写入权限.

    sudo chown www-data:www-data /home/djameson/test.txt
    chmod 0744 /home/djameson/test.txt
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将您的用户添加到www-data组或反之亦然将www-data用户添加到您的组.然后分组写权限.

    sudo usermod -a -G www-data djameson
    chmod 0764 /home/djameson/test.txt
    
    Run Code Online (Sandbox Code Playgroud)

注意:我假设apache用户名和组名分别是www-data和www-data.您必须相应地更改服务器apache用户名/组名称.


Osk*_*ers 14

默认情况下,Ubuntu上的Apache作为www-data运行.

假设您的文件夹位于/ var/www/mysite中.

你可以这样做:

chown -R www-data:www-data /var/www/mysite

chmod -R og-r

/var/www/mysite执行此操作后,www-data(Web服务器)将具有对站点文件的完全访问权限,而其他非root用户将根本无法访问.

如果您希望允许选定用户访问该站点,您可以使该文件夹组可读,并将这些用户添加到组www-data.

为您的apache文件设置正确的权限