Take the 2-minute tour ×

I have a PHP script that creates a directory and outputs an image to the directory. This was working just fine under Apache but we recently decided to switch to NGINX to make more use of our limited RAM. I'm using the PHP mkdir() command to create the directory:

mkdir(dirname($path['image']['server']), 0755, true);

After the switch to NGINX, I'm getting the following warning:

Warning: mkdir(): Permission denied in ...

I've already checked all the permissions of the parent directories, so I've determined that I probably need to change the NGINX or PHP-FPM 'user' but I'm not sure how to do that (I never had to specify user permissions for APACHE). I can't seem to find much information on this. Any help would be great!

(Note: Besides this little hang-up, the switch to NGINX has been pretty seamless; I'm using it for the first time and it literally only took about 10 minutes to get up and running with NGINX. Now I'm just ironing out the kinks.)

share|improve this question

migrated from stackoverflow.com Sep 29 '12 at 15:37

This question came from our site for professional and enthusiast programmers.

If nginx was installed by a package manager, the best is probably to just use 'ps' to see what user nginx is running as and change the owner of the directory to that user. Usually security is set up pretty well by default by the packages, changing user may upset something else. –  Joachim Isaksson Sep 29 '12 at 15:18
nginx.conf and www.conf by default IIRC. –  PeeHaa Sep 29 '12 at 15:21
If you're using fastcgi, check if you can suexec the phpscripts. that will allow you to run different sites under their specific user. I'm sure some setup instructions are available online. –  hakre Sep 29 '12 at 15:22
add comment

1 Answer

up vote 4 down vote accepted

Run nginx & php-fpm as www:www

1. Nginx

Edit nginx.conf and set user to www www;



Edit php-fpm.conf and set user and group to www.

user - Unix user of processes. Default "www-data"

group - Unix group of processes. Default "www-data"

share|improve this answer
Okay I just changed nginx.conf (it was set to user www-data). However, I don't see any user defined in php-fpm.conf. Should I just add it to the top using the same syntax (user www www)? –  David Sep 29 '12 at 15:29
php-conf syntax is not the same os nginx.conf. Find [www] part and add user=www in next line, and group=www in the next. –  glavić Sep 29 '12 at 15:30
I got the following error when restarting nginx: Restarting nginx: [emerg] getpwnam("www") failed in /etc/nginx/nginx.conf:1 nginx: configuration file /etc/nginx/nginx.conf test failed. –  David Sep 29 '12 at 15:45
Then you can either run Nginx and php-fpm as the user that has appropriate permissions for the appropriate directories or create a new user called www by running "sudo useradd -g www www". Please let us know, if you need more detailed explanation. –  Roman Prykhodchenko Sep 29 '12 at 16:01
add comment

Your Answer


