May 2, 2008
filed in the early evening by DrScofield in: hacking
technorati tags:
QR code for this entry · average time to read 1:14 minutes

ever since i switched to wordpress 2.5 (and 2.5.1) i was experiencing absurdly high CPU loads whenever i posted a new blog entry or changed a page — culminating in loads of 124+ yesterday, effectively bringing our server to a standstill! :-( not good. not good at all.

a ps ax | grep apache2 | wc -l revealed a rocking 104 apache2 processes running! yikes! all with about 21MB of memory usage (at least), no wonder the poor box was thrashing for dear life…

looking at what ubuntu had installed i found that (a) we had apache-prefork running and (b) switching to apache-worker1 would remove mod_php5 — which we need to have all the PHP scripts running (including this blog). hmmm.

some googling later it transpired that switching to thread-based apache was the right thing to do and also, that the fast CGI module mod_fcgid would be the solution to the disappearing php5 apache module.

here’s what i did: first install threaded apache

% apt-get install apache2-mpm-worker

then, install the fcgid module

% apt-get install libapache2-mod-fcgid

then, modify /etc/apache2/mods-available/fcgid.conf to read as follows:

<IfModule mod_fcgid.c>
    AddHandler    fcgid-script .php
    FCGIWrapper   /usr/local/sbin/php-fcgi .php

    SocketPath    /var/lib/apache2/fcgid/sock
    SharememPath  /var/lib/apache2/fcgid/shm
    IPCConnectTimeout 60
</IfModule>

create /usr/local/sbin/php-fcgi .php with the following content:

#!/bin/sh
PHPRC="/etc/php5/apache2"
export PHPRC
PHP_FCGI_CHILDREN=1
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php5-cgi

now, all that’s left to do, is ensure that for each virtual host you have ExecCGI enabled:

<Directory /var/www/xyzzyxyzzy.net/>
        Options ExecCGI Indexes FollowSymLinks MultiViews
        AllowOverride All

        Order allow,deny
        Allow from all
</Directory>

restart your apache and we are back in business — on my box the load is now back in sane regions: instead of 124+ we are back at 0.52 for posting a blog entry (at most)!


  1. the thread based apache. 

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.

4 comments »

  1. Hi, I followed your instructions – thanks! – and all seems to be working fine as before, but how do I check whether this threaded thing is actually working? Thanks in advance

    comment by Vito Botta — May 7, 2009 @ 12:19

  2. while it does run smoothly most of the time, i’ve since encountered this situation again a couple of times. currently i’m contemplating switching to lighttpd…

    sorry about getting your hopes up…

    comment by DrScofield — May 7, 2009 @ 22:47

  3. So how did you keep apt/dpkg from removing php5 and all the stuff it’s a dependency for?

    apt-get install apache2-mpm-worker
    
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following packages were automatically installed and are no longer required:
      aspell-en liblcms1 libcupsimage2 aspell libwmf0.2-7 libilmbase6 libmagickcore1
      libmagickwand1 libxslt1.1 libsqlite0 libmcrypt4 libcups2 libt1-5 libgraphviz4 libopenexr6
      libdjvulibre21 libxpm4 libgd2-xpm libgs8 libtiff4 psfontmgr libjasper1 ghostscript
      libmhash2 dictionaries-common libdjvulibre-text libpaper-utils libxt6 libaspell15 gsfonts
      libpaper1
    Use 'apt-get autoremove' to remove them.
    The following packages will be REMOVED:
      apache2-mpm-prefork libapache2-mod-php5 php5 php5-curl php5-gd php5-imagick php5-mcrypt
      php5-memcache php5-mhash php5-mysql php5-pspell php5-snmp php5-sqlite php5-xmlrpc php5-xsl
    The following NEW packages will be installed:
      apache2-mpm-worker
    0 upgraded, 1 newly installed, 15 to remove and 0 not upgraded.
    Need to get 258kB of archives.
    After this operation, 8249kB disk space will be freed.
    Do you want to continue [Y/n]? N
    Abort.

    comment by Dr. Kenneth Noisewater — June 10, 2009 @ 17:54

  4. hmm… i think i installed those php5 packages that i needed afterwards — though, with a more recent version of ubuntu the dependencies might have changed.

    in any case, see my earlier comment about this possibly not helping much :-(

    comment by DrScofield — June 11, 2009 @ 08:14

RSS feed for comments on this post. TrackBack URI

Leave a comment