Apache Webserver - 如何写入权限设置为 755 而不是 777 的目录/文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1155194/
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
Apache Webserver - How to write to dir/files with permissions set at 755 instead of 777
提问by John
I just learned to install Apache 2 on my ubuntu linux localhost for the first time. I'm making it work with PHP5.
我刚刚学会了第一次在我的 ubuntu linux localhost 上安装 Apache 2。我正在使它与 PHP5 一起工作。
I noticed that anytime I want to write to a file or directory, I have to chmod 777 the destination.
我注意到每当我想写入文件或目录时,我都必须 chmod 777 目标。
But from my experience working on 3rd party hosting solutions, I am generally allowed to write to files and dirs that have 755 permissions.
但是根据我在 3rd 方托管解决方案上工作的经验,我通常被允许写入具有 755 权限的文件和目录。
What can I do on my localhost so that I can write to files and dirs with 755 permissions? If the answer to this is very long, can someone send me a link to a step by step guide to do this?
我可以在我的本地主机上做什么,以便我可以使用 755 权限写入文件和目录?如果这个答案很长,有人可以给我发送一个链接到一步一步的指南来做到这一点吗?
采纳答案by Robert Greiner
You probably can't achieve this because the owner of the file is different than the user trying to perform an action on the file.
您可能无法实现这一点,因为文件的所有者与尝试对文件执行操作的用户不同。
the permissions are:
权限是:
owner-group-everyone
所有者-组-每个人
rwx-rwx-rwx
i.e. 111 = 7 which allows read/write and execute.
101 = 5 which is just read and execute
you can't write to the file because your logged in user isn't part of the owner/group that has access to the file.
您无法写入该文件,因为您的登录用户不是有权访问该文件的所有者/组的一部分。
the final 7 (i.e. rwx-rwx-111(7)) means that globally, everyone has read/write access to that file.
最后的 7(即 rwx-rwx-111(7))意味着在全球范围内,每个人都对该文件具有读/写访问权限。
how to fix this
In Linux, you can use the chown or chgrp command to achieve your desired results.
如何解决这个问题
在 Linux 中,您可以使用 chown 或 chgrp 命令来达到您想要的结果。
回答by slacy
Here are some simple rules for web site content management (under apache) that most people should follow:
以下是大多数人应该遵循的网站内容管理(在 apache 下)的一些简单规则:
- All content should be chown'd & chgrp'd to the same user that apache is running as. On new ubuntu installs , the user and group are both "www-data".
- If you want to administer the serving files under your own user group, then you should add youself to the www-data group, and make sure that users in this group have read/write access to all the serving files and directories. The caveat here is that you want to make sure not to create new files as your personal account. These should still be owned by www-data. The easiest way to accomplish this is to create the file as yourself, and then chown it to www-data:www-data.
- 所有内容都应该被 chown'd & chgrp'd 给运行 apache 的同一个用户。在新的 ubuntu 安装中,用户和组都是“www-data”。
- 如果您想在自己的用户组下管理服务文件,那么您应该将自己添加到 www-data 组,并确保该组中的用户对所有服务文件和目录具有读/写访问权限。这里需要注意的是,您要确保不要将新文件创建为您的个人帐户。这些仍应归 www-data 所有。完成此操作的最简单方法是以您自己的身份创建文件,然后将其 chown 为 www-data:www-data。
If you do these 2 things, then you should be able to write to files that are being served by apache. I'm not sure where your document root is, but something like this would likely work for most simple installs:
如果你做了这两件事,那么你应该能够写入由 apache 提供服务的文件。我不确定您的文档根目录在哪里,但是对于大多数简单安装来说,这样的事情可能适用:
$ sudo usermod $USER -a -G www-data
$ cd /var/www
$ sudo chown -R www-data:www-data .
回答by hillu
First, you will want to find out as which user your PHP code is running. If you are using mod_php5(package name libapache2-mod-php5) with Apache to run with the "worker" or the "prefork" MPM, this will probably be www-data.
首先,您需要了解您的 PHP 代码正在以哪个用户身份运行。如果您在Apache中使用mod_php5(package name libapache2-mod-php5) 以与“worker”或“prefork”MPM 一起运行,则这可能是www-data.
This is no big problem as long as you only run one web application within the server. However, if you run multiple applications (or scripts that are owned by more than one user), you are setting yourself up for all kinds of security-related "fun".
只要您在服务器中只运行一个 Web 应用程序,这不是什么大问题。但是,如果您运行多个应用程序(或由多个用户拥有的脚本),那么您就是在为各种与安全相关的“乐趣”做好准备。

