Tweaking FPM config

You may also need to tweak PHP-FPM config to match new sysctl.conf settings.

Open PHP-FPM pool config file:

vim /etc/php5/fpm/pool.d/www.conf

Look for line:

;listen.backlog = 128

Change it to:

listen.backlog = 65536

Restart php5-fpm service.

service php5-fpm restart

Additionally, make sure that operating system limit allows for above value.

Run:

sysctl net.core.somaxconn

Should display value 65536 or higher.

If no, then run following commands to verify.

echo "net.core.somaxconn=65536" >> /etc/sysctl.conf
sysctl -p

Test

Check FPM status page via web interface or command-line:

curl localhost/status

It will display parameters like:

pool:                 www
process manager:      ondemand
start time:           06/Nov/2016:19:02:09 +0800
start since:          2
accepted conn:        4
listen queue:         0
max listen queue:     0
listen queue len:     65536
idle processes:       1
active processes:     2
total processes:      3
max active processes: 3
max children reached: 0
slow requests:        0

In the output, listen queue len value should  match listen.backlog value set in the config.

Using TCP/IP for FPM

Sockets are slightly faster as compared to TCP/IP connection. But they are less scalable by default.

If you start getting errors like below (as faced ovidiu here

connect() to unix:/var/run/php5-fpm.sock failed or **apr_socket_recv: Connection reset by peer (104)**

Then it means you need to either switch to TCP/IP or tweak with linux-system parameter so that your OS can handle large number of connections.

Open PHP-FPM pool config file

vim /etc/php5/fpm/pool.d/www.conf

Replace line:

listen = /var/run/php5-fpm.sock

by line:

listen = 127.0.0.1:9000

Changes to Nginx

Next, open Nginx virtual-host config file(s).

Look for line

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

Replace it with

fastcgi_pass 127.0.0.1:9000;

Important: Reload php-fpm and nginx so that changes can take effect.

service php5-fpm reload && service nginx reload

Sysctl.conf Tweaking

Finally, don’t forget to tweak Linux’s sysctl values by following this – https://rtcamp.com/tutorials/linux/sysctl-conf/

12 comments

    1. There is no mathematical formula. I randomly increase limits till I stop getting limit-related errors.

      May not be good idea, but finding a logical method to calculate values is on my to-do list.

      1. I randomly increase limits till I stop getting limit-related errors.

        Just like pros. 😉

        I came here looking for a tuning solution for Symfony+nginx+php-fpm setup, my lighty+spawn-fcgi was faster and I couldn’t believe it, just switched Symfony to prod mode and then nginx+php-fpm was faster than lighty, odd.

    1. @Tarjei

      Not all limit related errors will be logged. In some cases, process will just crash.

      In some cases, errors can be seen in logs like: “Too many open files (24)” (covered in this post directly)
      Then there are socket-related errors if system is subjected to, too many concurrent connections.

      Not all settings in above article are related to error. For example, I don’t thnik any process will complain about values like net.core.rmem_max

  1. Hi Rahul,

    When I apply these configuration changes as described above for a WordPress site with Nginx + PHP5-FPM my site stops loading entirely. Are there any other changes I should be making to a Digital Ocean box currently setup for sockets for TCP? I’ve done nothing to configure TCP, other than what you advise in the sysctl.conf article above (I made those changes as well).

    Thanks,
    Paul

    1. This is really strange. Can you remove changes made to sysctl and check if site starts working back?

      If removing changes from sysctl work, you may try adding lines 1-by-1.

      All our config and tutorials are tested on DigitalOcean only.

      1. I’ve removed the changes to sysctl completely, and still no dice. Here are my various nginx configurations (as they stand prior to implementing the information you provided above):

        I’ve pasted nginx.conf here: http://pastebin.com/fD9HjqpB
        General wordpress configuration file: http://pastebin.com/CVkHZtvU
        And the site-specific config here: http://pastebin.com/KsVECwEa

        Perhaps you might see something that would conflict with the changes I’m trying to implement?

        The entire goal of making this switch is to overcome this error on a site with growing traffic:

        connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream

        1. Got it sorted! The offending line in my nginx configuration was the shared declaration for phpmyadmin and .php:

          location ~* /phpmyadmin/.*\.php$ {

          Split them up and your configurations work great. Thanks Rahul!

    1. You need to make sure that fastcgi_pass in nginx matched listen in php-fpm config. You can use any of TCP/IP or Unix-Socket as long as nginx and php5-fpm values match.

      In your case, looks like there is a mismatch between 2 values.

Comments are closed.