Running a webserver on a Xen guest with a limited amount of memory, and serve dynamic content, makes one wanting to optimize it, or switch to another webserver, like lighttpd. I was close to doing that, but I like my current apache setup, so I tried something else instead, which seems to have worked nicely for me. I switched to apache2-mpm-worker instead of apache-mpm-prefork, which is the default on Debian.
Lowering the number of preforked apache processes and changing other parameters in the apache2 configuration only gets one so far. It is robust, when there’s enough memory available to cope with the overhead.. but here is how to change to the mpm-worker instead, when having used mpm-prefork before, and php5, on Debian (Lenny).
- Stop apache2, ‘/etc/init.d/apache2 stop’
- Run aptitude, find “apache2-mpm-prefork”, hit – (for Remove, we want to save the configuration)
- That will break some packages, hit “g” and aptitude will (did for me) solve it by removing apache2-mpm-prefork and libapache-mod-php5, and install apache2-mpm-worker, and php5-cgi instead.
- After aptitude is done, read /usr/share/doc/php5-cgi/README.Debian.gz, which says the following:
To use php5-cgi with apache2 1) activate CGI (it's on by default in default debian setups) a) If using the prefork MPM, use 'a2enmod cgi' b) If using a threaded MPM, use 'a2enmod cgid' 2) activate mod_actions (a2enmod actions) 3) Add the following to a config snippet in /etc/apache2/conf.d <IfModule mod_actions.c> Action application/x-httpd-php /cgi-bin/php5 </IfModule>
- Do the above, I put the config in step 3 in /etc/apache2/conf.d/php5-cgi.conf
- Start apache2, ‘/etc/init.d/apache2 start’
The amount of memory used is a lot less on my machine. PHP now runs as its own process, for the requests that require it. I didn’t change the default configuration for apache2-mpm-worker, it seems to run nicely as is. That is, from /etc/apache2/apache2.conf:
<IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule>
Do note that if you have other packages and dependencies installed, aptitude will possibly not solve the dependencies the same way. Feel free to comment if you have other experiences using apache2-mpm-worker and php5-cgi to conserve memory on a small Xen guest with a small amount of available RAM, and serve content as good as possible.