Apache Permissions, PHP File Create, Mkdir Fail

Apache permissions, PHP file create, MKDir fail

Please stop suggesting to use 777. You're making your file writeable by everyone, which pretty much means you lose all security that the permission system was designed for. If you suggest this, think about the consequences it may have on a poorly configured webserver: it would become incredibly easy to "hack" the website, by overwriting the files. So, don't.

Michael: there's a perfectly viable reason why your script can't create the directory, the user running PHP (that might be different from Apache) simply doesn't have sufficient permissions to do so. Instead of changing the permissions, I think you should solve the underlying problem, meaning your files have the wrong owner, or Apache or PHP is running under the wrong user.

Now, it seems like you have your own server installed. You can determine which user is running PHP by running a simple script that calls the 'whoami' program installed in most linuxes:

<?php
echo `whoami`;

If all is right, you should see the username PHP is running under. Depending on your OS, this might be 'www-data', 'nobody', 'http', or any variation. If your website is the only website running, this is easy to change by changing the user Apache runs under. If you have Debian, like I tend to, you can edit the file /etc/apache2/envvars (as root), and change the value for APACHE_RUN_USER. Depending on your OS, this variable might be set in a different configuration file, so if you can't find it in /etc/apache2/envvars, try to search for the variable declaration by using:

$ grep -R "APACHE_RUN_USER=" .

From the directory all apache-config files are in.

If you're not the only one on the server, you might want to consider creating user accounts for every website, and using something like Apache2-MPM-ITK to change the RUN_USER depending on which website is called. Also, make sure that the user the PHP process is running under is the owner of the files, and the directories. You can accomplish that by using chown:

% chown theuser:theuser -R /var/www/website/

If PHP is running with it's own user, and is the owner of the files and directories it needs to write in, the permission 700 would be enough. I tend to use 750 for most files myself though, as I generally have multiple users in that group, and they can have reading permissions. So, you can change the permissions:

% chmod 0750 -R /var/www/website/

That should be it. If you having issues, let us know, and please don't ever take up any advice that essentially tells you: if security is bothering you, remove the security.

PHP unable to create a directory with mkdir

The answer is staring right in front of me, but I miss it due to my unfamiliarity with SELinux.

The SELinux context type should be set as httpd_sys_content_rw_t instead of httpd_sys_content_t so that the folder is both readable and writable for apache. Changing the context recursively is done with the following command:

# chcon -R -t httpd_sys_content_rw_t /var/www/html/images

Good grief. Hope it helps others who come across this.

PHP cannot create directory even though parent folder is 777

By default SE Linux should be configured to block writes to any files by the web server (Apache). The httpd_sys_content_t shows that the directory is set to read only. You need to set it to read/write by using the httpd_sys_rw_content_t context. This can be done using the semanage tool. The command would look like this.

semanage fcontext -a httpd_sys_rw_content_t "/var/www/html/library(/.*)?"

After you set that policy, you can apply it by doing...

restorecon -Rv /var/www/

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

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

PHP Warning: mkdir(): Permission denied in Seperate filesystem

Edit your apache -> conf -> httpd.conf file & change User and Group from daemon to your machine owner name. (in your case: a-linux-user) See if that works.



Related Topics



Leave a reply



Submit