Monday, November 18, 2013

Troubleshooting: Does PHP have write permission to a folder?

This article only talks about PHP installed alongside Apache under Linux (see here). Since PHP is run through libapache2-mod-php5. it inherits the permissions from Apache. So let's take a look about Apache. By default, it uses www-data user, to check this,

ps -u www-data

If it is so, you might get something similar:

PID TTY          TIME CMD
 1276 ?        00:00:00 php5-fpm
 1277 ?        00:00:00 php5-fpm
 1278 ?        00:00:00 php5-fpm
 1279 ?        00:00:00 php5-fpm
14934 ?        00:00:00 apache2
14935 ?        00:00:00 apache2
14936 ?        00:00:00 apache2
...

Now check user info and its group of www-data

cat /etc/passwd | grep www-data

The following shows up on my machine which says it doesn't belong to the root group or my administrator group.

www-data:x:33:33:www-data:/var/www:/bin/sh

We list the file permissions of the folder img/ to see apache's right:

ls -l | grep img

All right! Since Apache is in other group here, it doesn't have 'w' permission.

drwxrwxr-x 2 admin admin 4096 Nov 12 19:26 img

Then let's give him the permission. Notice that it is dangerous to do this, since in this way every user on the computer will have rights on this folder. Normally, we let the owner of the folder be Apache, and others can only read.

chmod o+w img
References

No comments:

Post a Comment