简体   繁体   English

来自root用户的PHP,mkdir权限被拒绝

[英]PHP from root user, mkdir permission denied

Today I have encountered a strange behaviour which I do not understand. 今天,我遇到了一种我不理解的奇怪行为。
I am trying to create a directory on the local disk using a PHP 7.0 script using mkdir() function but it produces a "permission denied" error. 我正在尝试使用mkdir()函数使用PHP 7.0脚本在本地磁盘上创建目录,但是会产生“权限被拒绝”错误。

In my experience (although very little) this always meant that the user which is executing the PHP script is not authorized to write to the directory. 以我的经验(尽管很少),这总是意味着执行PHP脚本的用户无权写入该目录。 This makes sense to me and is usually not a problem when developing web applications . 这对我来说很有意义, 在开发Web应用程序时通常不是问题。 The default PHP user when using Apache is www-data and I don't encounter problems with it. 使用Apache时,默认的PHP用户是www-data ,我没有遇到任何问题。

In this case though I am using Zend Framework 2 and in particular I am using it's console routes so I am not going through Apache (correct me if I am wrong). 在这种情况下,尽管我正在使用Zend Framework 2 ,尤其是正在使用它的控制台路由,所以我不会通过Apache(如果我输入错了,请更正我)。 I am calling my script as: 我称我的脚本为:

php index.php route name [--options]

with my user (which is not root but is a sudoer ). 与我的用户( 不是root,而是sudoer )一起使用。 The problems start if I output the result of PHP's get_current_user() function because I get 'root' instead of my expected user name. 如果我输出PHP的get_current_user()函数的结果,问题就开始了,因为我得到的是“ root”而不是期望的用户名。 Not only this, but I get 'permission denied' when using mkdir() in the following directory: 不仅如此,在以下目录中使用mkdir()时, mkdir() “权限被拒绝”的情况:

drwxr-xr-x 2 www-data www-data 4096 Aug 19 21:21 logs

What I understand from this is that I probably am not the root user as PHP seems to suggest. 我从中了解到,我可能不是PHP所建议的root用户。 If I then run the script with 'sudo' in front I am able to create the desired folders but the permissions do not match the ones I specify in my mkdir() function. 如果然后使用“ sudo”在前面运行脚本,则可以创建所需的文件夹,但权限与我在mkdir()函数中指定的权限不匹配。 If I write mkdir('path', 0777) I then get: 如果我写mkdir('path', 0777)我会得到:

drwxr-xr-x 3 root root

This I do not understand. 这我不明白。 So, if someone could help me figure out what I am doing wrong I would be very thankful. 因此,如果有人可以帮助我弄清楚我在做什么错,我将非常感激。 Keep in mind that the fact that I am going through Zend Framework 2 might influence this behaviour (although I am not keen on thinking so). 请记住,我正在通过Zend Framework 2的事实可能会影响此行为(尽管我并不热衷于这样思考)。

Thank you in advance for your time. 预先感谢您的宝贵时间。

Edit. 编辑。

I just realized I didn't tell you what my final goal is so I will now put things in context, sorry. 我只是意识到我没有告诉你我的最终目标是什么,所以我现在将内容放在上下文中,对不起。

What I am trying to do is to use this script (run either from root or from my user , preferably from my user ) to create these folders inside 我正在尝试使用此脚本(从root用户 (最好是从用户 )运行)在内部创建这些文件夹

/var/www

and to then be able to read and write files (and possibly other folders) to those directories whith the standard PHP user which in my case is www-data 然后能够使用标准PHP用户(在我的情况下为www-data )将文件(可能还有其他文件夹)读写到这些目录中

your php runned as apache extension, and using apache permissions. 您的PHP运行为apache扩展名,并使用apache权限。 If you need to create folder as root, you can create cron script for it, which will monitor change in some file, and create directory as root. 如果需要以root用户身份创建文件夹,则可以为其创建cron脚本,该脚本将监视某些文件中的更改,并以root用户身份创建目录。 Another way to do it, using suid extension, but it a bit complicated. 使用suid扩展名的另一种方法,但这有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM