简体   繁体   English

Centos 7 / Apache / PHP - mkdir():权限被拒绝

[英]Centos 7 / Apache / PHP - mkdir(): Permission denied

Before you yell DUPLICATE! 在你大喊大叫之前! and banish me into the abyss, hear me out. 把我赶进深渊,听我说。 :) :)

I've been developing an intranet site for the company I work with, and originally I was developing it directly from my workstation running Windows 7 / IIS . 我一直在为我合作的公司开发一个Intranet站点,最初我是从运行Windows 7 / IIS的工作站直接开发它。 As I've neared completion of the site/applications, I was given a Centos 7 box that's running Apache , so I've begun the process of migrating it over to that system. 当我接近完成站点/应用程序时,我得到了一个运行ApacheCentos 7盒子,所以我开始将它迁移到该系统。 I've sorted out most of the minor issues, and got the site running now. 我已经整理了大部分小问题,现在让网站运行了。 However, there is a portion of the site that interacts with our database and creates a log directory/file based upon the person that's logged in. This portion utilizes the mkdir() function, but I'm getting permission issues with it. 但是,该站点的一部分与我们的数据库交互,并根据登录的人创建一个日志目录/文件。这部分使用mkdir()函数,但我收到了权限问题。

mkdir(): Permission denied

Here is what I've already done: 这是我已经做过的事情:

  • Since I'm using Centos 7, the Apache user/group is apache:apache. 由于我使用的是Centos 7,因此Apache用户/组是apache:apache。 I've utilized chown and changed the owner of /var/www/html/ and the Logs folder to the apache user/group. 我已经使用了chown并将/ var / www / html /和Logs文件夹的所有者更改为apache用户/组。 To be on the safe side, I double checked the httpd.conf and also checked the running processes to ensure I had the correct user/group. 为了安全起见,我仔细检查了httpd.conf并检查了正在运行的进程以确保我拥有正确的用户/组。
  • I've attempted, just for testing purposes, to change the permissions of all of those folders to chmod 777, but getting the same error. 为了测试目的,我试图将所有这些文件夹的权限更改为chmod 777,但是得到了同样的错误。
  • Searched StackOverflow from top to bottom, and only getting answers for things that I've already tried, but to no avail. 从上到下搜索StackOverflow,只获得我已经尝试过的东西的答案,但无济于事。

So whether I use chmod or chown to change the permissions for the folders being accessed, I'm getting the same type of error. 因此,无论我使用chmod还是chown来更改正在访问的文件夹的权限,我都会遇到相同类型的错误。 To be on the safe side, I checked the PHP user also, and it's using the apache user as well. 为了安全起见,我也检查了PHP用户,并且它也使用了apache用户。

If anyone may have additional insight as to why it isn't working, even with the permissions changed, then please enlighten me. 如果有人可能有额外的洞察力,为什么它不工作,即使权限已更改,那么请赐教。 If I happened to miss the one article that explained this particular situation, then yell duplicate and banish me to the abyss. 如果我碰巧错过了一篇解释这种特殊情况的文章,那么大喊大叫并将我驱逐到深渊。 lol :) 大声笑 :)


EDIT Okay, so upon further testing, I've discovered 2 issues that seem to be causing this overall issue. 编辑好的,经过进一步的测试,我发现了两个似乎导致这个整体问题的问题。

  1. PHP is trying to set permissions for the folder when it's created from the script, and it seems the server doesn't like that even if apache is the owner. PHP正在尝试在从脚本创建文件夹时设置文件夹的权限,即使apache是​​所有者,服务器似乎也不喜欢它。 Upon removing that portion of the code, the permissions error goes away, and I get an error with "no such file or directory." 删除该部分代码后,权限错误消失,我收到“没有这样的文件或目录”的错误。
  2. When testing creating directories with the user Apache through sudo, it was able to make a directory without a problem. 当通过sudo测试用户Apache创建目录时,它能够创建一个没有问题的目录。 However, my PHP script is creating 2 directories, and then a log file in the last one created. 但是,我的PHP脚本创建了2个目录,然后创建了最后一个目录中的日志文件。 It seems that you can't create two directories at the same time?? 看来你不能同时创建两个目录? For example, there is a Logs folder already created, and PHP is trying to create two directories under it, one for the user's ID and then another folder inside of that one with the date. 例如,已经创建了一个Logs文件夹,PHP正在尝试在其下创建两个目录,一个用于用户的ID,然后是另一个带有日期的文件夹。 So once the PHP script runs, it should create something like "Logs/5235/3-3-2015/" 所以一旦PHP脚本运行,它应该创建类似“Logs / 5235 / 3-3-2015 /”的东西

Just to note, I've attempted to set the recursive value to true on mkdir in PHP, but that's when I get the permission issues originally noted. 请注意,我试图在PHP中的mkdir上将递归值设置为true,但是当我得到最初提到的权限问题时。 When recursive/mode is removed, it doesn't get the permission issue, but it isn't able to create nested directories. 删除递归/模式时,它不会获得权限问题,但无法创建嵌套目录。


EDIT2 EDIT2

To test my theories, I removed the nesting and tried to make mkdir create just 1 directory, and it's generating the same errors as before. 为了测试我的理论,我删除了嵌套并尝试使mkdir只创建一个目录,并且它产生与以前相同的错误。 Although it's owned by Apache, and even if I set it to 777, it throws back permission issues. 虽然它由Apache拥有,即使我将其设置为777,它也会抛出权限问题。

Could be that although you have 755/777 permissions, SELinux is blocking httpd from writing/creating dirs. 可能是因为你有755/777权限,SELinux阻止httpd编写/创建dirs。

Try: 尝试:

chcon -R -t httpd_sys_content_t /path/to/www
chcon -R -t httpd_sys_content_rw_t /path/to/www/dir/for/rw

Further info: http://wiki.centos.org/TipsAndTricks/SelinuxBooleans 更多信息: http//wiki.centos.org/TipsAndTricks/SelinuxBooleans

Not sure but your Centos's PHP binaries may have broken file permissions. 不确定,但Centos的PHP二进制文件可能已经破坏了文件权限。 There are two ways to fix this up. 有两种方法可以解决这个问题。

  • Compiling PHP from scratch. 从头开始编译PHP。 I would prefer this since all the control will be yours. 我更喜欢这个,因为所有的控制权都属于你。
  • Or Changing your php script to use Umask() function of PHP. 或者更改php脚本以使用PHP的Umask()函数。 Documentation link 文档链接

According to DRU Response 根据DRU Response

This issues is due to SELINUX. 这个问题是由SELINUX引起的。 Use bellow command 使用bellow命令

chcon -R -t httpd_sys_content_rw_t /path/to/www/dir/for/rw chcon -R -t httpd_sys_content_rw_t / path / to / www / dir / for / rw

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

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