Question

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

  • Posted April 9, 2014

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/
Subscribe
Share

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.allowed_clients = 127.0.0.1 listen.owner = nginx listen.group = nginx listen.mode = 0666 user = nginx group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 slowlog = /var/log/php-fpm/www-slow.log php_admin_value[error_log] = /var/log/php-fpm/www-error.log php_admin_flag[log_errors] = on php_value[session.save_handler] = files php_value[session.save_path] = /var/lib/php/session


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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…

php-fpm needs write access, not nginx, and its process is running as uid 1000, not as www-data <br> <br>so you could chown -R 1000 /var/www/subdomain.site.com <br> <br>ideally you would limit what directory www-data and php-fpm have write access to, maybe limit it to an upload directory for example.

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.

Are you seeing any error messages in the relevant log files? Check: <br> <br>/var/log/nginx/error.log <br>/var/log/php5-fpm.log