Php – Connect to unix:/var/run/php5-fpm.sock failed. What is wrong with the setup

nginxPHPWordpress

I have a 2GB VPS on DigitalOcean and I am hosting WordPress 3.9.1 under Debian 7 with NGINX, php-fpm and unix socket.

It was working perfectly until last week it started showing a "502 bad gateway" error. I checked the logs and found that:

php5-fpm log is showing pm.max_children was reached and
nginx log is showing the following:

[error] 3239#0: *15188 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: my.domain, request: "POST /xmlrpc.php HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xxx.xxx.xxx.xxx"

I manually changed pm with different settings with no luck. I always restart the daemons after every change.

pm settings are:

pm = dynamic 
pm.max_children = 100 
pm.start_servers = 10 
pm.min_spare_servers = 10 
pm.max_spare_servers = 10 
pm.max_requests = 200

www.conf has the listen = /var/run/php5-fpm.sock enabled.

Anyone with a similar experience?

Best Answer

The first problem is you are specifying 100 max_children, that is awfully high for 2GB. I would drop it to 25 children. See my post here on how to optimise your php-fpm configuration for your setup:

WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning

Also, using unix sockets is slightly faster since it provides you direct network access without any TCP/IP overhead. On the down side, it is not as scalable as TCP/IP. Nginx will throw 502 errors when the sockets have been depleted. In such a case you can either tweak the OS settings to accommodate the larger connection pool or just switch to switch to TCP/IP.

In your fastcgi conf change:

fastcgi_pass unix:/var/run/php5-fpm.sock;

to:

fastcgi_pass 127.0.0.1:9000;

Note that port 9000 is the default port set in php-fpm, if you have changed php-fpm to listen on another port then swap 9000 with that value. Make sure you restart both php-fpm and nginx.

Now, if after all of this, you still cannot get it to work and free -m returns high memory usage, then it is time to add more ram to your server.