Question

PHP-FPM sock failing to connect on Nginx

Hey there,

I recently switched over from Apache to Nginx. There’s this one issue I just can’t seem to fix. My website has been really slow and it’s receiving a lot of 502 bad gateway errors. My CPU usage is also around 90-100% even though there’s only around 100 users on the website.

The reason I switched to Nginx is because I hoped it’d make things load faster.

My droplet specs: Shared CPU

  • 4 vCPUs
  • 8 GB memory
  • 50 GB SSD
  • 5 TB transfer

In /var/log/nginx/error.log it’s showing thousands of the same error:

2020/07/02 14:51:14 [error] 2344#2344: *61933 connect() to unix:/run/php/php7.2-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET /get_dynamic HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "example.com", referrer: "https://example.com/earn"

This is what I have in my /etc/nginx/sites-available/example.com file:


# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
	try_files $uri $uri/ /index.php?$query_string;
	include snippets/fastcgi-php.conf;

	# With php-fpm (or other unix sockets):
	fastcgi_pass unix:/run/php/php7.2-fpm.sock;
	# With php-cgi (or other tcp sockets):
	#fastcgi_pass 127.0.0.1:9000;
}

And my /etc/php/7.2/fpm/pool.d/www.conf file:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www-data
group = www-data

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php/php7.2-fpm.sock

How am I able to fix this issue?


Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
July 17, 2023

Hello,

From the error message you’ve posted and the high CPU usage, it seems like your PHP-FPM worker pool is getting exhausted.

This can happen when PHP-FPM can’t process requests quickly enough, due to either insufficient resources or the workers being tied up by slow scripts.

When the pool gets exhausted, new connections have to wait, which can lead to 502 errors and high CPU usage.

Here are a few things you can try to fix the problem:

  1. Increase the number of PHP-FPM workers: You can do this by editing the www.conf file in your PHP-FPM pool configuration. Look for the following directives:

    • pm.max_children
    • pm.start_servers
    • pm.min_spare_servers
    • pm.max_spare_servers

    You can experiment with higher values for these settings to see if it helps. Remember, though, that each worker uses a certain amount of memory, so be careful not to set pm.max_children too high, or you might run out of memory. A good starting point for pm.max_children could be (total memory - memory used for other services) / average memory used per php process.

  2. Optimize your PHP code: Slow or inefficient PHP code can tie up workers, preventing them from handling other requests. Use a profiling tool like Xdebug or Blackfire to find bottlenecks in your code.

  3. Enable and fine-tune the PHP opcache: The opcache can significantly speed up PHP by storing precompiled script bytecode in memory. This avoids the overhead of parsing and compiling the code every time it’s run. If you haven’t already done so, enable the opcache by setting opcache.enable=1 in your php.ini file. You can also fine-tune the opcache settings to better suit your workload.

  4. Switch from unix socket to TCP/IP for PHP-FPM: In some cases, switching from a Unix socket to a TCP/IP socket for PHP-FPM can help, especially if you’re running into file descriptor limits. To do this, you would set listen = 127.0.0.1:9000 (or another unused port) in your www.conf file, and fastcgi_pass 127.0.0.1:9000; in your Nginx site configuration.

  5. Add a reverse proxy cache: If your site serves a lot of static or cacheable content, adding a reverse proxy cache like Varnish can help offload work from PHP and Nginx.

Make sure to restart the PHP-FPM service (sudo service php7.2-fpm restart) and Nginx (sudo service nginx restart) after each configuration change. Also, remember to monitor your site closely after making these changes to ensure that they’re having a positive effect.

Also, consider upgrading to a more powerful server if your site consistently requires more resources than your current server can provide. However, optimizing your application and server configuration can often go a long way toward improving performance, so I recommend trying that first.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel