Tuning Worker MPM settings

trainingcity's picture

I am thinking of configuring my Centos 6 server to use Worker MPM. I am pretty sure I know how to complete the install thanks to posts here in the group and elsewhere.

I have a Centos 6 LAMP server running multisite drupal. 2GB RAM. APC & memcached running, planning to install varnish, although most visitors are registered members.

My question is what should the settings look like in httpd.conf?

This is what I have at the moment. Will this even work as worker.c settings if I make the transition from prefork?

StartServers 3
MaxClients 50
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 2150

Comments

Prefork settings

trainingcity's picture

By way of comparison, here is my prefork settings in httpd.conf:

StartServers 3
MinSpareServers 2
MaxSpareServers 4
ServerLimit 7
MaxClients 7
MaxRequestsPerChild 75

I find this quickly chews up over 1 GB of RAM as the server gets busy. When coupled with the server requirements and around 312 MB assigned to APC and another 312 MB assigned to memcached, I quickly approach my 2 GB of RAM.

I am hoping with worker to be able to handle a higher volume of concurrent visitors and/or lower my RAM requirements.

Worker MPM is way to go , now

urwithraj's picture

Worker MPM is way to go , now coming back to thread settings is totally depend on amount of traffic your site is going to experience . For heavy traffic sites you can always increase the settings for MaxRequestsPerChild , ThreadsPerChild and MaxSpareThreads .

This is what we are using for site where we are getting heavy traffic .

ServerLimit 50
StartServers 5
ThreadLimit 120
MaxClients 2500
MinSpareThreads 500
MaxSpareThreads 2500
ThreadsPerChild 100
MaxRequestsPerChild 20000

With worker MPM you can use PHP-FPM which will give you better performance and then you can play with process manager settings ( which control the number of child processes ) and PHP thread settings. This can also help in reducing the memory footprint and give better performance. For sites which has less traffic you can go with OnDemand settings and if you have site which has high traffic then go with Dynamic PM settings and play around with thread settings. Static is good where you have enough memory on server . So these settings are entirely dependent on the application you are hosting . My recommendation is go with worker MPM + PHP-FPM and use Dynamic PM.

Hope this helps :) .

Serverlimit settings

trainingcity's picture

Thanks urwithraj, much appreciated. I guess I am confused about RAM requirements with worker MPM (with PHP-FPM).

I configured my devel replicated server to run worker MPM today and I am not seeing a decrease in RAM requirements.

A few topics for discussion:

  1. I turned on KeepAlive (MaxKeepAliveRequests 80 and KeepAliveTimeout 15). KeepAlive is Off on my production Prefork server. I "think" this is preferred with worker?

  2. Here are my worker settings:

StartServers 3
MaxClients 8
MinSpareThreads 5
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 2150

I am already at 1.4 GB of RAM with no load on server (granted I have replicated APC/Memecached at 300 MB each)

How could I set the ServerLimit 50 without destroying the server when it gets a 'real' load?

Are you using PHP or PHP-FPM

urwithraj's picture

Are you using PHP or PHP-FPM , i will recommend to use PHP-FPM and then you can control your thread settings in better way. We faced similar memory issue and when we moved to PHP-FPM and fine tuned PHP-FPM threads reduce memory usage to 60%

Are you using PHP or PHP-FPM

urwithraj's picture

Are you using PHP or PHP-FPM , i will recommend to use PHP-FPM and then you can control your thread settings in better way. We faced similar memory issue and when we moved to PHP-FPM and fine tuned PHP-FPM threads reduce memory usage to 60%

Settings for PHP-FPM

trainingcity's picture

I guess the other part of this question is what are viable settings for php-fpm?

As far as I can tell, there are two config files by default in Centos deployment

/etc/php-fpm.conf

and

/etc/php-fpm.d/www.conf

At the moment I have both at default settings.

Can you tell me what are the

urwithraj's picture

Can you tell me what are the settings you are using pm = dynamic or static and also

pm.max_children =
pm.start_servers =
pm.min_spare_servers =
pm.max_spare_servers =
pm.max_requests =

Link to info on tuning php-fpm

trainingcity's picture

Link to info on tuning php-fpm

Haven't had a chance to review closely, and this is intended for nginx, but thought I'd share here anyway:

http://blog.chrismeller.com/configuring-and-optimizing-php-fpm-and-nginx...

So here is what we are using

urwithraj's picture

So here is what we are using ( for heavy traffic site ) and memory consumption is arpprox 1 GB .

Memcache - ( 256 MB )
Apache - with worker MPM
PHP-FPM -
PM = Dynamic
pm.max_children = 100
pm.start_servers = 15
pm.max_requests = 100
Varnish

Again if you can see what is eating memory on your server it will be easy to fix .

Much improved RAM usage

trainingcity's picture

Thanks urwithraj

I am adjusting the /etc/php-fpm.d/www.conf file based on your input and the link I included.

I am noticing a significant drop in RAM and overall excellent performance. I am going to continue to search out optimization settings for php-fpm. I also unclear about the relationship between php-fpm conf settings and the worker MPM conf settings in httpd.conf?

pm.max_children seems too high?

trainingcity's picture

I don't think I can handle more than 10 for pm.max_children (and hence pm.max_spare_servers). With Drupal each php-fpm instance consumes at least 120MB each.

How did you determine 100?

Good to hear that RAM usage

urwithraj's picture

Good to hear that RAM usage is dropped .

pm.max_requests - The number of requests each child process should execute before respawning so you can decide on this based on your application usage. I want each thread which is spwaned should handle 100 requests atleast and then it will respawan and hence release memory.

Now if you don't see much traffic on your site i will suggest yo to play with OnDemand settings , here are the settings ( again you can adjust this based on traffic and application usage ) . We use these settings for those app where traffic is less and this saves more memory , you wont see any IDLE php thread on your system consuming memory .

pm = ondemand
pm.max_children = 25
pm.process_idle_timeout = 30s
pm.max_requests = 100

Server seems unstable?

trainingcity's picture

I seem to be getting unexpected server 500 errors after switching to worker/php-fpm. What is others experiences with complex Drupal/php driven sites when switching from prefork to worker?

Worker + FPM works well

kbahey's picture

Check the serves' error log to see if there are any further clues on why the 500 happen.

Check what max_execution_time in php.ini is set to, and change FPM request_terminate_timeout to match. Try setting both to 120 or 240 seconds.

For reference, here is /etc/php5/fpm/pool.d/www.conf from a very high traffic Drupal site:

[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children      = 40
pm.start_servers     = 20
pm.min_spare_servers = 2
pm.max_spare_servers = 20
pm.max_requests      = 5000
chdir = /

Drupal performance tuning, development, customization and consulting: 2bits.com, Inc..
Personal blog: Baheyeldin.com.

Check if you are using APC ,

urwithraj's picture

Check if you are using APC , if so remove that and move to ZendOpCache . APC module is not deprecated and you should move to ZendOpCache

Error in logs

trainingcity's picture

Thanks @kbahey. I checked the httpd logs and I seem to get the following error when I try to save a site change such as admin/modules changes:

FastCGI: incomplete headers (0 bytes) received from server "/usr/lib/cgi-bin/php5-fcgi", referer: ####/admin/modules/list/confirm

Could be truncation

kbahey's picture

It could be that the request is truncated, specially if you have too many modules.

Check the PHP part in this post Drupal not saving admin pages with large number of fields.

Drupal performance tuning, development, customization and consulting: 2bits.com, Inc..
Personal blog: Baheyeldin.com.

additional error entry

trainingcity's picture

FYI:

[Wed Apr 02 12:09:39 2014] [error] [client 99.224.163.209] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: idle timeout (30 sec)

Going to change request_terminate_timeout to 240 and see what happens.