Apache fcgid

Any program assigned to the handler fcgid-script is processed using the FastCGI protocoll; mod_fcgid starts a sufficient number instances of the program to handle concurrent requests, and these programs remain running to handle further incoming requests.

Specific executables are assigned this handler either by having a name containing an extension defined by the AddHandler directive, or with an override using the SetHandler directive (e.g., for all files in a specific directory such as cgi-bin).

PHP

PHP applications are usually configured using the FcgidWrapper directive and a corresponding wrapper script. The wrapper script can be an appropriate place to define any environment variables required by the application, such as PHP_FCGI_MAX_REQUESTS or anything else. (Environment variables can also be set with FcgidInitialEnv, but they then apply to all applications.)

Apache Installation

The only variable that needs to be set during installation is APXS. By default it will look at /usr/sbin/apxs.

APXS=/usr/local/steve/apache2/bin/apxs ./configure.apxs
make
make install

Apache Configuration

First, make sure that mod_fcgid is loaded

<IfModule !fcgid_module>
        LoadModule fcgid_module modules/mod_fcgid.so
</IfModule>

After creating the PHP wrapper script to the php-cgi binary, assign it to handle files with the .php extension:

<IfModule fcgid_module>
        AddHandler fcgid-script .php
        FcgidWrapper /var/www/fcgi-bin/php-wrapper .php
        <Directory /var/www/fcgi-bin/>
                SetHandler fcgid-script
                Options +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>
</IfModule>

For a VirtualHost entry, it must also have the ExecCGI option enabled:

<VirtualHost *:80>
        ServerName qa.beandog.org
        DocumentRoot "/var/www/html/qa"
        <Directory "/var/www/html/qa">
                Options +ExecCGI
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
# FcgidMaxRequestsPerProcess should be <= PHP_FCGI_MAX_REQUESTS
# The example PHP wrapper script overrides the default PHP setting.
# Default PHP_FCGI_MAX_REQUESTS is 500
# FcgidMaxRequestsPerProcess 10000

# Uncomment the following line if cgi.fix_pathinfo is set to 1 in
# php.ini:
# Default cgi.fix_pathinfo is 1
FcgidFixPathinfo 1

Alias /phpapp/ /usr/local/phpapp/
<Location /phpapp/>
AddHandler fcgid-script .php
Options +ExecCGI
FcgidWrapper /usr/local/bin/php-wrapper .php

# Customize the next two directives for your requirements.
Order allow,deny
Allow from all
</Location>
Sample PHP Wrapper Script
#!/bin/sh
# Set desired PHP_FCGI_* environment variables.
# Example:
# PHP FastCGI processes exit after 500 requests by default.
PHP_FCGI_MAX_REQUESTS=10000
export PHP_FCGI_MAX_REQUESTS

# Replace with the path to your FastCGI-enabled PHP executable
exec /usr/local/bin/php-cgi

Setup CGI User Environment

  • Create /var/www/client.com/htdocs as well as /var/www/client.com/cgi-bin
  • Create a new user client-www
    • Home directory: /var/www/client.com
    • Shell: /bin/sh
    • No login allowed
    • New group with same name as user
    • User ID >= 1000
    • Group ID >= 1000
    • Don't copy skeleton files

Timeout

By default, CGI requests time out at 40 seconds. The amount can be modified using FcgidIOTimeout globally in httpd.conf or in a virtualhost.

FcgidIOTimeout 90