fixing absurdly high wordpress CPU loads...

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:

1
2
3
4
5
6
7
8
#!/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.