don128898
By:
don128898

Can't write/delete files using nginx + php5-fpm (nginx is www-data, same as folder with files)

April 9, 2014 22.9k views
I can't figure out why PHP can't write/delete files. I believe I have everything setup properly, but clearly I'm missing something obvious. Running Ubuntu 12.10. nginx: root@DonaldAllen:/# ps aux|grep nginx|grep -v grep root 24427 0.0 0.3 79236 1608 ? Ss 04:59 0:00 nginx: master process /usr/sbin/nginx www-data 24428 0.0 0.9 82372 4924 ? S 04:59 0:00 nginx: worker process www-data 24429 0.0 0.9 82372 4924 ? S 04:59 0:00 nginx: worker process www-data 24430 0.0 0.9 82372 4924 ? S 04:59 0:00 nginx: worker process www-data 24431 0.0 1.1 82708 5656 ? S 04:59 0:00 nginx: worker process php5-fpm: root@DonaldAllen:/# ps aux|grep php|grep -v grep root 24415 0.0 1.2 278244 6056 ? Ss 04:59 0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) 1000 24416 0.5 4.5 291636 22816 ? S 04:59 0:02 php-fpm: pool www 1000 24417 0.5 4.4 291328 22448 ? S 04:59 0:02 php-fpm: pool www 1000 24432 0.2 3.7 287480 18676 ? S 04:59 0:01 php-fpm: pool www /etc/nginx/sites-enabled/default: server { listen 80; root /var/www/subdomain.site.com; index index.html index.htm index.php; server_name subdomain.site.com; location / { try_files $uri $uri/ /index.php; } fastcgi_intercept_errors off; location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PYRO_ENV production; } } /etc/php5/fpm/pool.d/www.conf user = donaldallen listen = /var/run/php5-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0666 And the directory with my website files: drwxr-xr-x 9 www-data www-data 4096 Apr 9 04:43 subdomain.site.com/
1 comment
  • I am having the same problem. no matter what I do NGINX/PHP-FPM cant write to the folders. Did you figure this out??

    nginx:
    [root@instance-1 php-fpm.d]# ps aux|grep nginx|grep -v grep
    nginx 21916 0.1 0.2 416884 22652 ? S 19:00 0:01 php-fpm: pool www
    nginx 21917 0.1 0.2 416884 22604 ? S 19:00 0:01 php-fpm: pool www
    nginx 21918 0.1 0.6 445956 51532 ? S 19:00 0:01 php-fpm: pool www
    nginx 21919 0.1 0.7 450664 55956 ? S 19:00 0:01 php-fpm: pool www
    nginx 21920 0.1 0.7 450352 56100 ? S 19:00 0:01 php-fpm: pool www
    nginx 21948 0.1 0.6 431972 46848 ? S 19:00 0:01 php-fpm: pool www
    nginx 22079 0.0 0.4 415760 31640 ? S 19:05 0:00 php-fpm: pool www
    root 22463 0.0 0.0 47644 1248 ? Ss 19:12 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx 22464 0.0 0.0 48080 2296 ? S 19:12 0:00 nginx: worker process
    nginx 22465 0.0 0.0 48080 2056 ? S 19:12 0:00 nginx: worker process
    nginx 22466 0.0 0.0 48080 2056 ? S 19:12 0:00 nginx: worker process
    nginx 22467 0.0 0.0 48080 2056 ? S 19:12 0:00 nginx: worker process

    php-fpm:
    [root@instance-1 php-fpm.d]# ps aux|grep php|grep -v grep
    root 21914 0.0 0.1 393788 12436 ? Ss 19:00 0:00 php-fpm: master process (/etc/php-fpm.conf)
    nginx 21916 0.1 0.2 416884 22652 ? S 19:00 0:01 php-fpm: pool www
    nginx 21917 0.1 0.2 416884 22604 ? S 19:00 0:01 php-fpm: pool www
    nginx 21918 0.1 0.6 445956 51532 ? S 19:00 0:01 php-fpm: pool www
    nginx 21919 0.1 0.7 450664 55956 ? S 19:00 0:01 php-fpm: pool www
    nginx 21920 0.1 0.7 450352 56100 ? S 19:00 0:01 php-fpm: pool www
    nginx 21948 0.1 0.6 431972 46848 ? S 19:00 0:01 php-fpm: pool www
    nginx 22079 0.0 0.4 415760 31640 ? S 19:05 0:00 php-fpm: pool www

    root dir:
    drwxr-xr-x. 6 nginx nginx 4.0K Jul 24 03:53 public_html

    www.conf:

    ; Start a new pool named 'www'.
    [www]
    listen = /var/run/php-fpm/php-fpm.sock
    listen.allowedclients = 127.0.0.1
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0666
    user = nginx
    group = nginx
    pm = dynamic
    pm.max
    children = 50
    pm.startservers = 5
    pm.min
    spareservers = 5
    pm.max
    spareservers = 35
    slowlog = /var/log/php-fpm/www-slow.log
    php
    adminvalue[errorlog] = /var/log/php-fpm/www-error.log
    phpadminflag[logerrors] = on
    php
    value[session.savehandler] = files
    php
    value[session.save_path] = /var/lib/php/session

4 Answers
Are you seeing any error messages in the relevant log files? Check:

/var/log/nginx/error.log
/var/log/php5-fpm.log
What exactly is the situation, what is the script or app and what is it trying to do that requires write/delete? On Wordpress, for instance, I have to add group write permissions to the uploads folder before it will work, i.e. 775 instead of 755.
php-fpm needs write access, not nginx, and its process is running as uid 1000, not as www-data

so you could chown -R 1000 /var/www/subdomain.site.com

ideally you would limit what directory www-data and php-fpm have write access to, maybe limit it to an upload directory for example.

You have to set folder owner manually and recursivly I guess ?

chown -R www-data:www-data /var/www/subdomain.site.com

That will work for now, but own to make it work for new files in the future as well...? I don't know..

  • I succesfully found out how.

    1. set user / group permissions 774 (RWX own/group, read for others)
    chmod -R 774 /path/to/web/root
    
    1. Usure folder and files are properly owned by the user and www-data :
    chown -R your_username_here:www-data /path/to/web/root
    
    1. Finally set a umask so new files within this folder gets automatically this permission (but I'm not sure if that is secure though)
    umask 774 /path/to/web/root
    
Have another answer? Share your knowledge.