18
votes

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. 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. 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(): Permission denied

Here is what I've already done:

  • Since I'm using Centos 7, the Apache user/group is apache:apache. I've utilized chown and changed the owner of /var/www/html/ and the Logs folder to the apache user/group. 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.
  • I've attempted, just for testing purposes, to change the permissions of all of those folders to chmod 777, but getting the same error.
  • Searched StackOverflow from top to bottom, and only getting answers for things that I've already tried, but to no avail.

So whether I use chmod or chown to change the permissions for the folders being accessed, I'm getting the same type of error. To be on the safe side, I checked the PHP user also, and it's using the apache user as well.

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. 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. However, my PHP script is creating 2 directories, and then a log file in the last one created. 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. So once the PHP script runs, it should create something like "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. When recursive/mode is removed, it doesn't get the permission issue, but it isn't able to create nested directories.


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. Although it's owned by Apache, and even if I set it to 777, it throws back permission issues.

3
Did you check all logs? eg: SElinux logs. try tail -f /var/log/* to see if there is anything else out there. also use sudo for testing. eg: sudo -H -u apache bash -c 'mkdir /tmp/testapache'risyasin
@Risyasin I've already checked all logs, and getting the same type of log errors. I tested creating the directory with Apache through sudo, and it creates the directory without an issue at all. I may have discovered the source of the issue though, I'll update my original post with the new information.Fata1Err0r
@Risyasin I've updated my original post with the new information. Basically when mkdir is trying to set recursive and default permissions, it gets denied. When I remove the recursive/mode, it doesn't get the permission issue, but it doesn't create the nested directories because it doesn't think that location exists since recursive is false by default. As mentioned, I've given the folders ownership to Apache and even tried to set them to 777 for testing.Fata1Err0r

3 Answers

53
votes

Could be that although you have 755/777 permissions, SELinux is blocking httpd from writing/creating 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

2
votes

Not sure but your Centos's PHP binaries may have broken file permissions. There are two ways to fix this up.

  • Compiling PHP from scratch. I would prefer this since all the control will be yours.
  • Or Changing your php script to use Umask() function of PHP. Documentation link
-3
votes

According to DRU Response

This issues is due to SELINUX. Use bellow command

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