Nginx – Make PHP-FPM Listen at ‘IPAddress:Port’ Instead of ‘/var/run/php5-fpm.sock;’

debian-wheezyfastcginginxPHPphp-fpm

As I was load testing my site today (using blitz.io); despite lots of RAM (more than 50%) and CPU power (over 70%) available, results showed that my site started timing out at a certain number of concurrent users per second.

Nginx error log for my site (/var/log/nginx/example.com.error.log) showed something like this:

2013/02/12 19:03:57 [error] 13749#0: *3175 connect() to
unix:/var/run/php5-fpm.sock failed (11: Resource temporarily
unavailable) while connecting to upstream, client: 54.123.456.46,
server: example.com, request: "GET / HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "example.com"

Googling the error led me to this answer which states using TCP\IP connection instead of unix socket as the solution to the problem; as unix socket's "problems on high-load cases is well-known".

So, as suggested by the answer:

  • I replaced listen = /var/run/php5-fpm.sock with listen 127.0.0.1:9000 in /etc/php5/fpm/pool.d/www.conf

  • As there's no /etc/nginx/php_location on my distrio (Debian Wheezy), I did nothing about it.

  • Since I use fastcgi_pass unix:/var/run/php5-fpm.sock; in the Nginx configuration file for my site, i.e., /etc/nginx/sites-available/example.com, I replaced it with fastcgi_pass 127.0.0.1:9000;

Now the problem is, I get a 502 Bad Gateway error when I visit my website. Yes, I did reload Nginx and PHP-FPM. What am I doing wrong? (A total newbie here, doing my best to learn by doing.)

In case this is relevant, when I do sudo service php5-fpm restart, I get this error:

[FAIL] Restarting PHP5 FastCGI Process Manager: php5-fpm failed!

And this is happening only since I made the aforementioned changes. How can I fix this?

Please let me know if I should get more information.


UPDATE

The file /etc/nginx/sites-available/default says this:

#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;

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

So, does that mean, if my server is running PHP-FPM, it SHOULD, without a choice, use /var/run/php5-fpm.sock?

Best Answer

I used $ sudo php5-fpm -t command to test if PHP-FPM's settings are fine (if not, it'll show me some error/info).

So, here's what the output looked like:

[13-Feb-2013 18:35:00] ERROR: [/etc/php5/fpm/pool.d/www.conf:33] value is NULL for a ZEND_INI_PARSER_ENTRY
[13-Feb-2013 18:35:00] ERROR: Unable to include /etc/php5/fpm/pool.d/www.conf from /etc/php5/fpm/php-fpm.conf at line 33
[13-Feb-2013 18:35:00] ERROR: failed to load configuration file '/etc/php5/fpm/php-fpm.conf'
[13-Feb-2013 18:35:00] ERROR: FPM initialization failed

The error says, something's wrong in Line 33 of of /etc/php5/fpm/pool.d/www.conf, which happens to be this: listen 127.0.0.1:9000 (not so much of a coincidence, is it?).

After seeing it, I immediately compared it with other lines, and then it struck me, an = (equal-to sign) is missing!

So, this is what it's supposed to be: listen = 127.0.0.1:9000 and that fixed everything!