php-fpm.conf for server hostname

ditto

Verified User
Joined
Apr 27, 2009
Messages
2,354
It seems maybe that /usr/local/directadmin/data/templates/custom/php-fpm.conf is only used for user accounts. However for server hostname, like when running roundcube on server hostname, wich php-fpm.conf should I edit?

When I look at this path /usr/local/php70/etc/php-fpm.conf - it seems it has active configurations like:
Code:
pm = ondemand
pm.max_children = 10
pm.process_idle_timeout = 60
pm.max_requests = 1000

So it is /usr/local/php70/etc/php-fpm.conf that is used for server hostname? If not, what are those settings used for? And how can I make a custom version of them wich won't get overwritten?
 
And a follow up question: I have read all documentation I have found, but still I have not found the answer on:

Regarding pm.max_children, is one php request the same as one children? Or can ONE children server many php request?
 
max_children - the maximum number of processes that the php-fpm main process can fork.

max_requests - the maximum number of requests that each php-fpm forked process can handle.
 
Last edited:
I have already read that and all other documentation that I found. It does not answer my question: Is one php request the same as one children?
 
No. One request will be managed by one thread in one of the childs.

In your config, you have 10 childs, each of which can handle up to 1000 requests each.
 
Thank you! By the way, it is not my config, it is the default of DirectAdmin at this path: /usr/local/php70/etc/php-fpm.conf I still do not know when those settings is used? Is it by server hostname only?

I am modifying /usr/local/directadmin/data/templates/custom/php-fpm.conf for all my users.

So does your answer really mean that the children in pm.max_children each can handle 1000 requests? It is further confusing because /usr/local/directadmin/data/templates/custom/php-fpm.conf does not contain the value pm.max_requests, that value is only present in /usr/local/php70/etc/php-fpm.conf - so does that mean pm.max_requests value in /usr/local/php70/etc/php-fpm.conf is used for all users/domains in DirectAdmin? Or should I add the missing value pm.max_requests to /usr/local/directadmin/data/templates/custom/php-fpm.conf

Also I am struggling to desicde if I am going to continue to use DirectAdmin ondemand default, or if I should change to dynamic. The servers has 64GB RAM, however there is up to 600 domains on each server, so I am not sure if I have enough resources when using dynamic - but I would like to use dynamic to get faster loading times.

Also I am very much in doubt if I should enable opcache or not when this is shared hosting servers. What do others do when running shared hosting servers, do they do it with opcache endabled?
 
Now I guess we must clarify what a "request" is.

If you understand a request like "a user connects to Apache and wants to load a page with a PHP script - eg request is a making of a page load", then it's what you said earlier - one hit from a browser will be served by one PHP-FPM process, so one child will be occupied for one page load.

The actual term "request" that is in the FPM terminology is something slighly different - it's a connection from Apache to PHP-FPM. One page load with Apache may cause more than one connection from Apache to PHP. They are, as far as I know, served by the same FPM child. They can be however served simulataneously (again, as far as I know).

So why 1000? That's the max limit which kills the process. It's there to prevent memory leaks.
 
Last edited:
Does I understand you correct if I say that pm.max_children = 10 can handle max 10 simulataneously page loads?

Should I specify pm.max_requests in /usr/local/directadmin/data/templates/custom/php-fpm.conf (it is not present there), or is the pm.max_requests from /usr/local/php70/etc/php-fpm.conf automatically used globally for all users/domains?
 
I guess you are questioning about how to set these variables to make your web server to be able to serve as much concurrent clients as possible.

That's done by increasing max_children. You shouldn't bother max_requests - you can leave it high like 1000 (the default is 0, which is unlimited, but I won't recommend it).

A good recipe for estimating the max_children is to calculate how much RAM memory you can spend for the PHP. Then you do this:

max_children = Ram for PHP / Typical big process size

So look at your top, see what the process sizes are (they do differ, but usually they are 80-100MB each). Then calc your typical Apache HTTPD process size... and finally tune all in such way that both Apache and PHP will serve the same amount of users without exceeding the RAM they can use (which is total ram - mysql max ram - exim ram - dovecot ram, etc).
 
Does I understand you correct if I say that pm.max_children = 10 can handle max 10 simulataneously page loads?

Yes, that's how I understand it.

More on max_requests - it's not only for memory leaks and concurrent requests from Apache to PHP. Requests are actually reused between connections.

Example:

user1 connects and make a page load for a php page. He connects Apache which connects to PHP-FPM child1. One request is made.

user2 connects and make another page load for a php page. He connects to Apache which connects again to the same child1 (it stays alive, not like in SUPHP). That counts as a second request for child1, no matter that its made from another user.

In 1000th request, child1 will be closed and another one will be opened. It's like closing the program and reopening it "just in case" a memory leak occured in it.

PHP-FPM is better than SuPHP exactly because of that process reusing - it's not creating new request for each new request, it reuses old processes (up to max_requests).
 
Thank you very much for making me understand this better. After your new reply, I am wondering if I don't need to use dynamic, but can continue to use ondemand. I considered dynamic because I wanted better performance.

But after your reply, I am thinking that I can increase pm.process_idle_timeout from directadmin default 20 to something like 60, that way each child can be reused upto 60 seconds after it was last used.
 
This is what I consider to do for 64GB RAM server with about 600 domains:

pm = ondemand
pm.max_children = 500
pm.process_idle_timeout = 60 (or maybe only 30)

It is hard, because I want to test in advance, but of course my test server does not have any big number of clients. :)
 
And a bit more clarification. Apache with HTTP2 and Apache with HTTP1.1+Keep Alive are able to execute multiple browser page load requests in one TCP/IP connection. That means that one browser can occupy one httpd process and then request multiple files through it. When some of these files are actually PHP scripts (for example captcha image, images built with GD in PHP, etc), then one httpd process will do multiple requests to php-fpm.

Now here is what I am pretty sure it's true, but I am not 100% behind it. In such cases the httpd process will make the multiple requests to the same PHP-FPM child and eventually that will be done by executing multiple requests simultaneously to it (in threads). I am almost 100% sure about the first one, because the PHP requests are involving chrooting the process (to make it run with the user credentials) and that takes resources. It won't be logical to perform it second time to different child when one is already chrooted. But is it able to make it in threads? That's just a guess. It will be very optimal if it can.

P.S. max_children = 500 sounds like a huge overkill to me.
 
Last edited:
Please note that you should adjust the Apache MaxRequestWorkers variable accordingly. It's senseless to have large max_children in PHP-FPM and low MaxRequestWorkers in Apache. The defaults are usually low.

If Apache is in Event MPM, that's the max number of threads that will be spawned. Each thread serves one connection.

If APache is in Prefork MPM, that's the max number of processes that will be forked. Each process servers one connection.
 
Last edited:
Thank you! I need to sleep on this, and study even more before I decide to use ondemand or dynamic, and what settings I should use.

I can mention that I run Apache with mpm event, wich is default in DirectAdmin if you use PHP-FPM. My main goal is to provide the best possible performance within the hardware limits on my servers. Not so easy to decide settings in advance when you already has filled up servers with clients and hundreds of domains wich currently is running mod_php with mod_ruid2. It feels like a big big step to make this change to PHP-FPM on existing production servers.

The reason I want PHP-FPM, is because it is needed to be able to offer support for http/2 in the future, wich is not recommended to do using mpm prefork (so I need PHP-FPM to get to use MPM event).
 
Please note that you should adjust the Apache MaxRequestWorkers variable accordingly. It's senseless to have large max_children in PHP-FPM and low MaxRequestWorkers in Apache. The defaults are usually low.

I am not very familier with mpm event yet, previous I only used mpm prefork. Currently I am running the default mpm event settings from directadmin wich is:

Code:
    StartServers             6
    MinSpareThreads	    32
    MaxSpareThreads	   128
    ThreadsPerChild         64
    ServerLimit             32
    MaxRequestWorkers     2048
    MaxConnectionsPerChild   10000
 
You have lots of ram, so it won't be a problem to play with large numbers. I checked my Apache config too and it's the same as yours - the numbers are low on the other MPMs, the event one is pretty high (2048).

The problems are usually coming from the Disk I/O. If you are not using some fast RAID with SSD's, even if you reach 500, it will be equivalent to a DoS attack to your hard drive :) That's loading 500 files at the same time - pretty hard task for HDD.

That's what I experience with my server at the moment. My CPU load is around 10% but the disks are loaded like hell and that slows everything.
 
Last edited:
Thanks. Actually I have 4 enterprise SSD drives in hardware raid 10 on my servers. Thanks again. Your answers will be a big help for me. However, I must say, it was much more easy with mod_php and mod_ruid2. And the only reason I do this, is to be able to offer http/2 in the future. :)
 
Yes, pm.max_children = 500 might be to much. First I need to decide if I am going for ondemand or dynamic setting in php-fpm.
 
Check it out:

Code:
# systat
                    /0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10
     Load Average   >>>>>>>

                    /0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
root           idle XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
mysql        mysqld XX

while

Code:
# systat -iostat
          /0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100
cpu  user|XXXX
     nice|
   system|
interrupt|
     idle|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

          /0%  /10  /20  /30  /40  /50  /60  /70  /80  /90  /100
ada0  MB/sXXX
      tps|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX200.67
ada1  MB/sXXX
      tps|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX201.66
ada2  MB/sX
      tps|XXXXXXXXXXXXX
ada3  MB/s
      tps|XX
      tps|


ada0 and ada1 are SSDs in mirroring RAID, ada2 and ada3 are disks which I am using the the /home/admin/ (the backups are there).
 
Back
Top