Why I have 300 process apache sleeping if I have MaxClients = 200?

8,424

Solution 1

First you should figure out how much RAM each Apache process uses on average. Run ps -ylC apache2 --sort:rss to get a rough estimate -- total up the RAM and divide by the number of processes. You might want to exclude any values that are wildly different.

Then take a look at the total RAM usage, and subtract the Apache usage.

MaxClients should be set to roughly: ((total RAM) - (RAM used by processes other than Apache) - (some cushion)) / (average RAM used by Apache processes)

EDIT: Also you may want to look at the output of mod_status to see if there are a lot of processes in the KeepAlive state ("K"). If so you need to reduce your KeepAliveTimeout setting.

Solution 2

  1. Enable mod_status. Read that page for details and security considerations.
  2. View the server status at http://example.org/server-status?refresh=N (Refresh every N seconds) . You will see each httpd child, the page it is serving, the IP of the client which requested the page, etc.

Things to look for:

  • Usually when Apache has too many children, it's because one page or a set of related pages are hanging due to code issues. Look for httpd children which are all serving the same page. That page may be buggy.
  • If one client is hammering your server by requesting multiple pages in quick succession, you can see it on this page.
  • Sometimes you can also detect a DDOS on this page. Look for multiple IPs all requesting the same buggy page, or requesting multiple slow pages. DDOSes are hard to detect, due to their nature.

Solution 3

First doublecheck if you actually do have 300 apache processes with something like pgrep -c apache2.

It is possible you changed the config but haven't restarted yet (this can easily stretch into many weeks).

If your server stops responding, is it only the apache server, or the entire system? Try to monitor your memory usage (the memory-free metric is far easier to measure than the total usage by all processes).

Share:
8,424

Related videos on Youtube

dynamic
Author by

dynamic

Updated on September 18, 2022

Comments

  • dynamic
    dynamic about 1 year
    <IfModule mpm_prefork_module>
        StartServers            150
        MinSpareServers         5
        MaxSpareServers         20
        ServerLimit             200
        MaxClients              200
        MaxRequestsPerChild     0
    </IfModule>
    

    top after apache stop responding:

    After few hours of running my server stops responding due to too many apache processses in sleeping... And only thing I can do is to restart apache

    What can I do? I am on debian 6 with LAMP
    KeepAlive is off for the moment.

    • Sean Kimball
      Sean Kimball about 12 years
      What are your keepalive settings, your sleeping processes could be waiting for connections if they are set very high...
    • dynamic
      dynamic about 12 years
      Off...............
    • dynamic
      dynamic about 12 years
      @Shane: i doubt I have 100 other process
    • ravi yarlagadda
      ravi yarlagadda about 12 years
      Sure you do, or at least close to it. Stop Apache and see for yourself. Anyway, if your problem is that Apache is becoming unresponsive, you should probably be giving some details about that; what kind of response do page requests get? Anything in Apache's error logs?
    • dynamic
      dynamic about 12 years
      Error logs is clean (only hundreds of 404s pages). For your first question I honestly don't get what you mean
    • Stefan Lasiewski
      Stefan Lasiewski about 12 years
      yes123: There is no evidence above showing that you have 300 apache processes. Please do a count of the Apache httpd processes with a command like ps aux |grep --count httpd.
    • dynamic
      dynamic about 12 years
      @Stefan: top atm shows 161 total, your command echoes 37. So when there are 300 process in total, apache are about 180. Anyway now I have maxclient at 150. Let's see if apache stops again
    • Stefan Lasiewski
      Stefan Lasiewski about 12 years
      This means that you currently have 37 httpd proceesses, and about 161 processes total (not unusual for a Linux system). If you run that grep command periodically you might see the number of httpd processes grow over time.
  • xofer
    xofer about 12 years
    Any reason why KeepAlive is off? Have you tried turning it on?
  • dynamic
    dynamic about 12 years
    to simplify the debug, it has been always on, with a timeout of 2-3sec. So keepalive isn't the problem. Which values of maxclients and serverlimit do you suggest me?
  • xofer
    xofer about 12 years
    How many apache2 processes are there?
  • dynamic
    dynamic about 12 years
    How can i check ? Ah with your command wait a moment
  • dynamic
    dynamic about 12 years
    I coutned 31 apache2 process. But I have started apache few minutes ago. top shows 142 total task (i have reduced maxclients to 150 now)
  • xofer
    xofer about 12 years
    Sorry I missed that you said each process is about ~14 MB. MySQL is probably your other main memory hog. So if you figure about 1 GB (guessing) for MySQL and 1 GB for other processes, Apache could safely consume 2 GB RAM. So 150 may be a little high. I'd try 125.
  • dynamic
    dynamic about 12 years
    thank you. With 150 apache2 is up and running for ~24h without problems now. I will wait more. If it hangs out again i will take that to 125