我可以使用具有 644 文件权限的 php 的 fwrite 吗?
我正在尝试设置自动 .htaccess 更新。这显然需要尽可能安全,但是现在我能做的最好的文件权限是 666。
我可以做什么来设置我的服务器或 php 代码,以便我的脚本的 fwrite()
命令适用于 644 或更好的系统?例如,有没有办法将我的脚本设置为以所有者身份运行?
编辑:
我意识到我实际上只是遇到了权限问题,您应该能够使用 644 权限的 fwrite
没有问题。请参阅下面我的回答。
I am trying to set up automated .htaccess updating. This clearly needs to be as secure as possible, however right now the best I can do file permission-wise is 666.
What can I do to setup either my server or php code so that my script's fwrite()
command will work with 644 or better? For instance is there a way to set my script(s) to run as owner?
EDIT:
I realized I actually just had a permissions issue, you should be able to use fwrite
no problem with 644 permissions. See my answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
apache
进程应始终以apache:apache
运行 - 如果您必须在可执行文件(即 DocumentRoot)目录中启用写入权限,请创建一个组,添加 apache 并设置组写入权限(所以664
)。最好通过 cron 脚本从数据库读取配置数据来更新
.htaccess
,因为如果代码中的漏洞允许恶意用户写入新的内容,则授予 apache 对可执行目录的写入权限是不受欢迎的。文件到这些目录。The
apache
process should always run asapache:apache
- if you must enable write permissions in executable (i.e. DocumentRoot) directories, create a group, add apache and set group write permissions (so664
).It's best to have
.htaccess
updated by a cron script reading config data from a database, as giving apache write permissions to executable directories is frowned upon in case a vulnerability in your code allows a malicious user to write new files to those directories.您无法更改进程的所有者。如果您使用的是共享服务器,请查看他们是否有 suPHP 作为选项。
You can't change the process's owner. If you're on a shared server, see if they have suPHP as an option.
这些建议很棒,但我最终意识到我的问题的答案是是 - 并且您根本不需要做任何事情...只要您尝试写入的文件/目录与脚本运行的用户是同一用户。我的错误是我不小心让我的文件所有权失常,因此需要更高的权限 666 和 777 才能写入我的文件。这是有道理的,因为 WordPress 可以使用标准权限写入 .htaccess。
现在我已经进行了设置,其中以
user1
身份运行的文件正在写入user1:user1
拥有的文件,并且没有任何问题。目录设置为 755,.htaccess
文件设置为 644。These suggestions were great, however I ultimately realized that the answer to my question is YES - and you shouldn't have to do anything at all... as long as the Owner user of the file/directory you are trying to write to is the same user the script is running as. My mistake was that I accidentally had my file ownership out of whack therefore needed higher permissions 666 and 777 in order to write to my files. Which makes sense because Wordpress can write to .htaccess with standard permissions.
Now I have things setup where a file running as
user1
is writing to a file owned byuser1:user1
, and no problems whatsoever. Directories set to 755,.htaccess
file set to 644.