Killing off PHP

Do you know why Apache processes get stuck and stop responding when serving pages on a WordPress site?

I’ve seen this happen here and on my previous host on a regular basis. I don’t know what happens. It can’t be a PHP script gone into an infinite loop because the normal Apache timeout should kill it. It’s not MySQL as a quick inspection of the process list usually shows it’s empty.

It could be plugins, some of them haven’t been written to the high standards that is expected in WordPress core. It could be some strange interaction between plugins and core code and memory limits and PHP extensions.

Whatever causes it, this will fix it. It’s brutal, it’s crude, but it’ll stop the load average going up on your box and it will ensure that every Apache child process is listening and responding. Add this to the crontab of your nobody or www-data user. Pick whichever user runs the webserver because you want to limit the damage in case something bad happens and the command malfunctions!
*/10 * * * * ps auxw|grep apache2| awk '$10 !~ /0:00/ {print $2":"$10}'|awk -F ':' '$2 !~ 0 {print $1}'|xargs kill -9 2> /dev/null
What this does is it uses the ps, grep, and awk tools to find processes that are using anything more than the minimum CPU time. It is very crude, but it works.

If you use Litespeed, then replace “apache2” with “lsphp”. I have found that this is very necessary as those processes get stuck quite often, especially in low memory situations.

12 thoughts on “Killing off PHP

  1. Hmmm, I had this also before. My Firefox was offering me a “Save as” box and not the page. I found out that the apache process was segfaulting. I have “fixed” it now maybe, because I’m not sure. You may want to reduce the maximum number of spawned processes and increase the limit of memory in php.ini.

    StartServers 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxClients 16
    MaxRequestsPerChild 500

    These are my settings for a 512 MB server. :-/ Kinda small but I cannot upgrade it because of low money. I gave 128M in php.ini for the mem limit.


  2. 512MB of memory? Oh I wish! I’m working on a 150MB virtual private server which Apache and MySQL have to share! It does teach you a lot about tuning a server and the importance of caching however.

  3. I have debugged it in the past but the error_log was never very interesting. Lots of seg faults and php_errors wasn’t very helpful either. This has been the most effective way of ensuring that my server is responsive!

  4. I use IIS on my server and have noticed that every couple of days I’ll get a fault in the IIS worker process. Fortunately with the way IIS uses app pools, it recycles itself before users notice. Interesting enough, it didn’t start happening until I switched from b2evolution to wordpress. I’m running a plethora of plugins which I can’t afford to disable to debug.

    Good information for the apache users out there. Nice post.

  5. And exactly that happens to my server (even with 512 MB). So it must be somewhere a missconfigured part of Apache and I bet it’s the one I have posted above. 🙂 If it was not I put a permalink to you in the (new!) category “Lost bets” 🙂

  6. Hi,

    0) what about disabling all plugins and anebling it one-by-one to track the problem?

    1) mayby it is better to limit resources for the Apache process and limit the time and resources for php scripts rather than killing Apache? Brute force kill -9 does not eliminate the cause, it only eliminate the results.

    2) I had sped up a lot my wordpress simply by installing eAccelerator, very simple and effective. Time of generating a page is about 0.5 sec, before it could last even 30secs!

    3) what can You see in (error) logs?

    4) You can also try lighttpd insted of apache 😉

  7. Thanks for the suggestions ori0n, but I’ve tried all of the above except using lighttpd. Apache processes are already quite limited due to the memory constraints. I hadn’t installed an accelerator until about a week ago, but it didn’t help. I had an outage a few days ago before I fixed my kill script – it was checking for lsphp instead of Apache, I recently changed back to Apache after using Litespeed Tech’s server.

    Nothing but seg faults in the error logs I’m afraid.

    Logging functions were the worst at increasing server load, but UTW’s SQL JOINs hurt the server a lot until I fixed that but I can’t pinpoint anything except it happened more often when the server was heavily loaded.

    I may give lighttpd a go, but I believe it’s PHP that gets bogged down so it might not help that much. I noticed the same problem with Lsws.

  8. Have you also tried to install the APD Apache module? Its the “Advanced PHP Debugger”. You can may find it with a good search engine. I know that there is also a Win32 DLL available and maybe a Windows port of the pprofp (PHP Profiler).

    After this you have to modify php.ini:

    zend_extension = /usr/lib/php5/20051025/
    apd.dumpdir = /tmp/apd/
    apd.statement_trace = 1

    By /usr/lib/php5/xxxxx/ is the full path to your|dll file. 🙂 And e.g. C:\temp\ for /tmp/apd/ under Windows.

    Add this single line in wp-config.php directly behind the

    Then checkout the path you have entered above for a pprof.xxxxx file.

    Hope this helps. 🙂

  9. Have you tried out apache2-mpm-worker ? I need to re-install VHCS here so my webserver is stopped. I’m already working on it. 🙂

  10. I have the same issue, I’ve narrowed mine down to a single echo and ob_flush_end command. It has to do with the slowness of clients on the other end, but frankly after 20 seconds, I want my Apache processes servicing someone else.

    I will try your setup there, it looks exactly like the brutality I need.

    I also was looking at trying Apache::Watchdog::RunAway. It has some level of elegance but lacks your idea’s simplicity.

    Thanks in advance.

Leave a Reply

%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.