PHP-FPM: Socket vs TCP/IP and sysctl tweaking

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</strong>
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/