In my last post I wrote about some problems that I had with my virtual server that freezes under heavy load. The improvements I suggested worked really nice but the server still sometimes freezes – not often as before but it still happened. So I “googled” around and also asked at ServerFault.com.

It seems that keeping up the the apache mpm_prefork and mod_php will not be the solution anymore. Most sites suggested to use mpm_worker instead, because it uses less memory (and my problem was, that my Apache consumed all my memory). On the other hand there could be some problems with thread-safety, but I liked to give it a try. PHP will not me used as an Apache module any more. Instead we will run PHP as a FastCGI script.

A short step by step tutorial for switching from mpm_prefork to mpm_worker with using PHP.

  • Open a terminal session as a root user.
    apt-get install apache2-mpm-worker libapache2-mod-fcgid php5-cgi

    apt will complain about a few things and will remove some of the previous installed packages. After installation make sure that mod_fcgid is enabled.

  • Edit you /etc/apache2/apache2.conf or /etc/apache2/httpd.conf or any config file that will be parsed by apache and add this:
    1. AddHandler fcgid-script .php
    2. FCGIWrapper /usr/lib/cgi-bin/php5 .php
    3. Options ExecCGI FollowSymlinks Indexes
  • Now enter you /etc/apache2/sites-available directory and edit all the the files there. Every file should contain at least one “VirtualHost” node. Within these “VirtualHost” nodes there should be one or two “Directory” nodes. If not already there add a new line starting with “Options” and add “Indexes” and “ExecCGI” as parameters on these lines. It should be look like this one:
    1. Options Indexes FollowSymLinks MultiViews ExecCGI
    2. AllowOverride AuthConfig
    3. Order allow,deny
    4. allow from all
  • Restart apache:
    /etc/init.d/apache2 restart

Hopefully now everything should work as before. If you had enabled eAccelerator or any other byte code cache system or if you had mode some modifications on you php.ini you now have to copy them to your “new” php.ini located at /etc/php5/cgi/php.ini

A few things wont work as before, e.g. when use user authentication by PHP and not by htaccess. There are also a few issues I read about, but nothing which affect my sites.

As a result I would say spending this hours messing around with Apache and mod_fcgid was a good investment. My server now need only 90 MB Ram when idle, it was 250 MB Ram before. Even under load memory consumption stays lower as before. I hope this will be the last tweak for I longer time.

Using PHP as a FastCGI script gives you a lot of new options. So if you have a lot of time to spent or very worried about the security of you web server you could use the Apache module suexec which then could execute the script as a special user and not as the default web server user www-data. It could even use multiple user for each virtual host / directory.

Interesting links: