Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 695
Fata1Err0r Avatar asked Mar 04 '15 13:03

Fata1Err0r


People also ask

How do I fix permission denied mkdir?

[ErrorException] mkdir(): Permission denied Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project. To solve this problem, go into your laravel project, make your public directory writable.

How do I create a directory Permission denied in Linux?

There are a couple of possible solutions to this issue: Create a folder that the user running the build has permissions to. Change the ownership of the directory with the chown command before trying to write to it.


2 Answers

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

like image 89
drack Avatar answered Oct 28 '22 07:10

drack


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
like image 2
risyasin Avatar answered Oct 28 '22 07:10

risyasin