php 如何授予 apache 写入主目录的权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22062266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to give apache permission to write to home directory?
提问by user3247608
My server is in /var/www/html I have a php script in /var/www/html/fileio_test/io_test.php
我的服务器在 /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);
?>
When I try to run this script I get
当我尝试运行此脚本时,我得到
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
How do I let apache write to my home directory? The server runs on fedora 20.
如何让 apache 写入我的主目录?服务器在 fedora 20 上运行。
回答by kuldeep.kamboj
As your file residing in your Home directory, I would suggest one of following approaches.
由于您的文件驻留在主目录中,我建议使用以下方法之一。
Give 0777 permission to file itself.
chmod 0777 /home/djameson/test.txtChange Ownership to apache user www-data and give owner-write permission.
sudo chown www-data:www-data /home/djameson/test.txt chmod 0744 /home/djameson/test.txtAdd your user to www-data group or vice-verse add www-data user to your group. And then group write permission.
sudo usermod -a -G www-data djameson chmod 0764 /home/djameson/test.txt
授予 0777 文件本身的权限。
chmod 0777 /home/djameson/test.txt将所有权更改为 apache 用户 www-data 并授予所有者写入权限。
sudo chown www-data:www-data /home/djameson/test.txt chmod 0744 /home/djameson/test.txt将您的用户添加到 www-data 组,反之亦然将 www-data 用户添加到您的组。然后组写权限。
sudo usermod -a -G www-data djameson chmod 0764 /home/djameson/test.txt
NOTE : I am assuming apache user name & group name is www-data & www-data respectively. You must change accordingly your server apache username/group name.
注意:我假设 apache 用户名和组名分别是 www-data 和 www-data。您必须相应地更改您的服务器 apache 用户名/组名称。
回答by Oskars Pakers
By default, Apache on Ubuntu runs as www-data.
Let's assume your folder is located in /var/www/mysite.
You can do this:
chown -R www-data:www-data /var/www/mysite
chmod -R og-r
/var/www/mysiteAfter doing this,www-data(the Web server) will have full access to the site's files, while other non-root users will have no access at all.If you wish to allow select users to access the site, you can make the folder group-readable and add those users to the group www-data.
默认情况下,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。
回答by Flosi
In order for this file to be readable for both the user "djameson" as well as the webserver, you need to do 4 things:
为了使用户“djameson”和网络服务器都能读取此文件,您需要做 4 件事:
- Add the user apache uses (www-data on most systems) to a user group on your system. You might want to create a new one.
- Add the user "djameson" to that group as well.
- Change owner permissons (
chown)of the file, so that it is owned by the user django and the group you just set up. - Make sure the file permissions (
chmod) allow read permission by the group.
- 将 apache 使用的用户(大多数系统上的 www-data)添加到您系统上的用户组。您可能想要创建一个新的。
- 也将用户“djameson”添加到该组。
- 更改文件的所有者权限 (
chown),使其归用户 django 和您刚刚设置的组所有。 - 确保文件权限 (
chmod) 允许组的读取权限。
回答by Utsav Popli
if some of your files are outside of www directory and you would like to provide access to files outside of apache environment you may use chmod -R o+rx
如果您的某些文件在 www 目录之外,并且您想提供对 apache 环境之外的文件的访问权限,则可以使用 chmod -R o+rx
Above would give "other users(also include apache user)" permission to read/execute files
以上将授予“其他用户(也包括 apache 用户)”读取/执行文件的权限

