FeaturesWP CloudPricingEE CLIContactMy Account

Features

Team Access & User Permissions

Server Setup & Management

Site Creation & Management

Deployments & Developer Tools

Firewall Protection

Speed & Caching

Backups & Recovery

Logs & Monitoring

Create WP Cloud Sites

All Features

Read Documentation

Install

Installation Guide

FAQ

Roadmap

Explore EE CLI

Resources

Commands

Handbook

Blog

Community

Forum

EasyExperts

Tutorials

Subscribe

Github

CLEAR

Start For Free

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

Topics

On this page

Tweaking FPM config

Test

Using TCP/IP for FPM

Changes to Nginx

Sysctl.conf Tweaking

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
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 lenvalue 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 – http://rtcamp.com/tutorials/linux/sysctl-conf/

Nginx – Enable PHP-FPM Status Page

PREVIOUS

APC Cache Optimization & Monitoring Using Web Interface

NEXT


Comments

  1. Renê Dettenborn Avatar

    Renê Dettenborn

    June 17, 2013

    How to calculate values of /etc/sysctl.d/custom.conf for my hardware?

    1. Rahul Bansal Avatar

      Rahul Bansal

      August 7, 2013

      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. xr09 Avatar

        xr09

        September 19, 2013

        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.

  2. Tarjei Huse Avatar

    Tarjei Huse

    October 23, 2013

    Where do you expect the limit-related errors to be logged?

    1. Rahul Bansal Avatar

      Rahul Bansal

      October 23, 2013

      @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

  3. PaulIsLoud Avatar

    PaulIsLoud

    February 10, 2014

    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. Rahul Bansal Avatar

      Rahul Bansal

      February 11, 2014

      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. PaulIsLoud Avatar

        PaulIsLoud

        February 12, 2014

        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. PaulIsLoud Avatar

          PaulIsLoud

          February 13, 2014

          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. Rahul Bansal Avatar

            Rahul Bansal

            February 14, 2014

            Glad to know your issue got fixed. 🙂

  4. Amit Avatar

    Amit

    February 17, 2014

    I came across this post while configuring <a href="https://github.com/afaqurk/linux-dash&quot; rel="nofollow">linux-dashboard</a> which requires php-fpm to be <a href="https://gist.github.com/sergeifilippov/8909839&quot; rel="nofollow">configured</a>. I modified www.conf to use TCP/IP. After restarting nginx and php-fpm services the dashboard does not fetch the actual values. What changes are required in sysctl-conf?

    1. Rahul Bansal Avatar

      Rahul Bansal

      February 17, 2014

      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.

Features

Useful Links

Product

Other Products

Subscribe for curated product updates, tips, and announcements.

"*" indicates required fields

Name

This field is for validation purposes and should be left unchanged.

Email*

SIGN ME UP

Δ


© rtCamp, since 2009. All rights reserved.

Powered by