Debian: Converting Apache + PHP to FastCGI – The Simple Way

I have a server running on Debian 6 64bit installed with Apache and PHP5 using apt-get package manager. By default, it will configure DSO  as the PHP handler (more details here). I need to convert it to serve PHP through FastCGI (mod_fcgid) to save memory usage. It turns to be that converting them is easy and require simple steps.


Here is my pre-configured Apache + PHP settings (installed using apt-get install apache2 php5 command):

$ dpkg --get-selections | grep apache
apache2             install
apache2-mpm-prefork install
apache2-utils       install
apache2.2-bin       install
apache2.2-common    install
libapache2-mod-php5 install
$ dpkg --get-selections | grep php
libapache2-mod-php5 install
php5                install
php5-cli            install
php5-common         install
php5-suhosin        install


1. Update repository:

$ apt-get update

2. Install required packages for fcgid:

$ apt-get install apache2-mpm-worker libapache2-mod-fcgid php5-cgi

3. Enable cgi.fix_pathinfo in /etc/php5/apache2/php.ini:


4. Stop Apache:

$ /etc/init.d/apache2 stop

5. Disable php5, since we will be using php-cgi:

$ a2dismod php5

6. Setup the virtual host for the website under /etc/apache2/sites-available/default:

 <VirtualHost *:80>
        DocumentRoot /var/www
        <Directory /var/www>
                Options +ExecCGI
                AllowOverride AuthConfig FileInfo Limit
                AddHandler fcgid-script .php
                FCGIWrapper /usr/lib/cgi-bin/php .php
                Order Deny,Allow
                Allow from All
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined

7. Start Apache:

$ /etc/init.d/apache2 start


Done! You are now running on FastCGI as the PHP handler. You can verify this with phpinfo: