Apache Module mod_fcgid
Summary
Any program assigned to the handler fcgid-script is processed
using the FastCGI protocol; mod_fcgid starts a sufficient
number instances of the program to handle concurrent requests, and these
programs remain running to handle further incoming requests. This is
significantly faster than using the default mod_cgi or
mod_cgid modules to launch the program upon each request.
However, the programs invoked by mod_fcgid continue to
consume resources, so the administrator must weigh the impact of invoking
a particular program once per request against the resources required to
leave a sufficient number of instances running continuously.
The pool of fcgid-invoked programs is shared between all httpd workers.
Configuration directives below let the administrator tune the number of
instances of the program that will run concurrently.
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).
Some changes have been made in the ASF release of mod_fcgid which
can affect existing configurations. All documentation refers to new
names for the directives. (The old names still work but are now
deprecated.) Please read the Upgrade Notes for
details.
For an introduction to using CGI scripts with Apache, see
the generic tutorial on Dynamic Content
With CGI.
Directives
Topics
See also

The following changes have been made in the ASF release of mod_fcgid
and should be considered when upgrading from the original version by
Ryan Pan (Pan Qingfeng).
- All directives have been renamed in order to use a common prefix "Fcgid".
Underscores in directive names have been eliminated in favor of
CamelCase. The old directive names will still work but are deprecated.
To fix your configuration you can use the sed script build/fixconf.sed.
The following table contains old and new directive names:

Note
The examples assume that mod_fcgid and other necessary
modules are loaded into the server already, either built-in or
via the LoadModule
directive.
Additionally, the example configurations provide full access
to the applications using access control directives which work
with Apache 2.0 and 2.2. These directives are not appropriate
for all environments, and they do not work for development
levels of Apache HTTP Server (Subversion trunk).
The first example is a very simple Perl FastCGI application,
and its configuration directives. This is typical for FastCGI
applications which require no special configuration.
Perl FastCGI application - /usr/local/apache/fcgi-bin/foo.pl
#!/usr/bin/perl
use CGI::Fast;
while (my $q = CGI::Fast->new) {
print("Content-Type: text/plain\n\n");
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
}
}
Configuration directives
<Directory /usr/local/apache/fcgi-bin/>
SetHandler fcgid-script
Options +ExecCGI
# Customize the next two directives for your requirements.
Order allow,deny
Allow from all
</Directory>
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.)
Here is an example that uses a wrapper script to invoke PHP:
PHP application - /usr/local/phpapp/phpinfo.php
<?php
phpinfo();
?>
Configuration directives
# FcgidMaxRequestsPerProcess should be <= PHP_FCGI_MAX_REQUESTS
# The example PHP wrapper script overrides the default PHP setting.
FcgidMaxRequestsPerProcess 10000
# Uncomment the following line if cgi.fix_pathinfo is set to 1 in
# php.ini:
# 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>
PHP wrapper script - /usr/local/bin/php-wrapper
#!/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
Special PHP considerations
By default, PHP FastCGI processes exit after handling 500
requests, and they may exit after this module has already
connected to the application and sent the next request. When that
occurs, an error will be logged and 500 Internal Server
Error will be returned to the client. This PHP behavior
can be disabled by setting PHP_FCGI_MAX_REQUESTS to
0, but that can be a problem if the PHP application leaks
resources. Alternatively, PHP_FCGI_MAX_REQUESTS can
be set to a much higher value than the default to reduce the
frequency of this problem.
FcgidMaxRequestsPerProcess
can be set to a value less than or equal to
PHP_FCGI_MAX_REQUESTS to resolve the problem.
PHP child process management (PHP_FCGI_CHILDREN)
should always be disabled with mod_fcgid, which will only route
one request at a time to application processes it has spawned;
thus, any child processes created by PHP will not be used
effectively. (Additionally, the PHP child processes may not be
terminated properly.) By default, and with the environment
variable setting PHP_FCGI_CHILDREN=0, PHP child
process management is disabled.
The popular APC opcode cache for PHP cannot share a cache
between PHP FastCGI processes unless PHP manages the child
processes. Thus, the effectiveness of the cache is limited with
mod_fcgid; concurrent PHP requests will use different opcode
caches.

mod_fcgid has several types of controls which affect the creation
of additional application processes:
mod_fcgid has several types of controls which affect the termination
of existing application processes:
Several of the directives control processing for a process
class. A process class is the set of processes which were started
with the same executable file and share certain other characteristics such
as virtual host and identity. Two commands which are links to or otherwise
refer to the same executable file share the same process class.
Note
Certain settings or other concepts that depend on the virtual host,
such as FcgidInitialEnv
or process classes, distinguish between virtual hosts only if they
have distinct server names. (See the ServerName
documentation for more information.) In the case of
FcgidInitialEnv, if two
virtual hosts have the same server name but different environments as
defined by
FcgidInitialEnv, the
environment used for a particular request will be that defined for the
virtual host of the request that caused the FastCGI process to be
started.
Information about each process will be displayed in the
mod_status server-status page.

Access checking or, more formally, access control, is a procedure
which verifies that the client is allowed to access a resource, using
some mechanism other than authentication and authorization.
Key environment variables passed to the application for access
checking are:
FCGI_APACHE_ROLE
- set to
ACCESS_CHECKER; by checking the current role,
the same FastCGI application can handle multiple stages of request
processing
The application must output a Status line to indicate
the result of the check.
Warning
Before 2.3.6, only one FastCGI application of any type (AAA or handler)
can be used for a particular request URI. Otherwise, the wrong FastCGI
application may be invoked for one or more phases of request processing.

This directive controls whether or not other access checkers
are allowed to run when this module has an access checker configured
and it fails a request. If this directive is On (default)
and a FastCGI access checker returns a failure status, a failure is
returned to the client without giving other access checkers a chance to
allow access. If this directive is Off, other access
checkers will be called.

Authentication is the procedure which verifies that the user is
who they claim they are. This directive specifies the full path to
a FastCGI application which will handle authentication for a particular
context, such as a directory.
Key environment variables passed to the application on authentication
are:
REMOTE_USER
- set to the user id of the client
REMOTE_PASSWD
- set to the plain text password provided by the client
FCGI_APACHE_ROLE
- set to
AUTHENTICATOR; by checking the current role,
the same FastCGI application can handle multiple stages of request
processing
The application must output a Status line to indicate
the result of authentication.
Warning
Before 2.3.6, only one FastCGI application of any type (AAA or handler)
can be used for a particular request URI. Otherwise, the wrong FastCGI
application may be invoked for one or more phases of request processing.

This directive controls whether or not other authenticators
are allowed to run when this module has an authenticator configured
and it fails a request. If this directive is On (default)
and a FastCGI authenticator returns a failure status, a failure is
returned to the client without giving other authenticators a chance to
validate the client identity. If this directive is Off,
other authenticators will be called.

Authorization is the procedure which verifies that the user is
allowed to access a particular resource. This directive specifies
the full path to a FastCGI application which will handle authorization
for a particular context, such as a directory.
Key environment variables passed to the application on authorization
are:
REMOTE_USER
- set to the user id of the client, which has already been
authenticated
FCGI_APACHE_ROLE
- set to
AUTHORIZER; by checking the current role, the
same FastCGI application can handle multiple stages of request
processing
The application must output a Status line to indicate
the result of authorization.
Warning
Before 2.3.6, only one FastCGI application of any type (AAA or handler)
can be used for a particular request URI. Otherwise, the wrong FastCGI
application may be invoked for one or more phases of request processing.

This directive controls whether or not other authorizers
are allowed to run when this module has an authorizer configured
and it fails a request. If this directive is On (default)
and a FastCGI authorizer returns a failure status, a failure is
returned to the client without giving other authorizer a chance to
access the resource. If this directive is Off, other
authorizers will be called.

This is the maximum time limit for request handling. If a FastCGI
request does not complete within FcgidBusyTimeout seconds, it will be
subject to termination. Because the check is performed at the
interval defined by FcgidBusyScanInterval,
request handling may be allowed to proceed for a longer period of time.
The purpose of this directive is to terminate hung applications.
The default timeout may need to be increased for applications that
can take longer to process the request.

This directive allows processing options to be specified for
a specific command spawned by mod_fcgid. Each option for the
command corresponds to another directive that normally applies to
all commands started within a particular context. If a
particular option is not specified on this directive, the
default will be used.
The following table provides a list of options and
corresponding directives:
Multiple environment variables are defined by repeating
the InitialEnv option.
Example
FcgidCmdOptions /usr/local/bin/wrapper \
InitialEnv MAX_REQUESTS=2000 \
MaxRequestsPerProcess 2000 \
IOTimeout 90
When /usr/local/bin/wrapper is spawned, its initial
environment contains the MAX_REQUESTS=2000
environment variable setting; additionally, mod_fcgid will
terminate it after it has handled 2000 requests, and I/O
operations will time out after 90 seconds. Directives
corresponding to other options, such as
FcgidIdleTimeout or
FcgidProcessLifeTime,
will be ignored for this command; defaults will be used for options
not specified on FcgidCmdOptions.

This is the maximum period of time the module will wait
while trying to connect to a FastCGI application on Windows.
(This directive is not respected on Unix, where AF_UNIX defaults
will apply.)
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This is the interval at which the module will handle
pending process termination. Termination is pending for
any processes which have exceeded
FcgidIdleTimeout or
FcgidProcessLifeTime.
Unix: mod_fcgid will terminate such processes with SIGTERM;
if the process is still active during the next scan, the process
will be terminated with SIGKILL. Thus, this directive controls the
amount of time for orderly process terminate before being forcibly
killed.
This directive enables special SCRIPT_NAME
processing which allows PHP to provide additional path information.
The setting of FcgidFixPathinfo
should mirror the cgi.fix_pathinfo setting in
php.ini.
Application processes which have not handled a request for this
period of time will be terminated, if the number of processses for the
class exceeds
FcgidMinProcessesPerClass.
A value of 0 disables the check.
This idle timeout check is performed at the frequency of the configured
FcgidIdleScanInterval.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
Use FcgidInitialEnv to define environment
variables to pass to the FastCGI application. This directive can
be used multiple times.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This is the maximum period of time the module will wait
while trying to read from or write to a FastCGI application.
Note
The FastCGI application must begin generating the response within
this period of time. Increase this directive as necessary to handle
applications which take a relatively long period of time to respond.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This module uses AF_UNIX sockets or named pipes, depending on the
platform, to communicate with FastCGI applications. This directive
specifies the directory where those sockets or named pipes will be
created.
This directive sets the maximum number of FastCGI application
processes which can be active at one time.
This directive sets the maximum number of processes that can be
started for each process class.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This module reads the entire request body from the client
before sending it to the application. Normally the request body
will be stored in memory. Once the amount of request body read
from the client exceeds FcgidMaxRequestInMem
bytes, the remainder of the request body will be stored in a
temporary file.
If the size of the request body exceeds this amount, the
request will fail with 500 Server Error.
Administrators should change this to an appropriate value for their site based
on application requirements.
Warning
Before 2.3.6, this defaulted to 1GB. Most users of earlier versions should
use this directive to set a more reasonable limit.
See also

FastCGI application processes will be terminated after handling
the specified number of requests. A value of 0
disables the check.
Note
A value of -1 is currently accepted for ease of
migration for existing configurations. It is treated the same as
0.
Certain applications, notably PHP as FastCGI, have their own
facility for terminating after handling a certain number of
requests. This directive can be used to avoid sending
additional requests to the application after it has handled its
limit.
Note
If this is set such that frequent process creation will be
required, you will likely need to adjust
FcgidSpawnScoreUpLimit
or other score-related directives to allow more frequent process
creation.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.

This directive sets the minimum number of processes that will be
retained in a process class after finishing requests.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This is the maximum amount of response data the module will read
from the FastCGI application before flushing the data to the client.

This directive specifies the name of a request header which
will be passed to the FastCGI application as an environment
variable. The name of the environment variable is derived from
the value specified on this directive, as discussed below:
The legacy behavior is to use the value specified on this directive
as the environment variable name, converting hyphens to underscores.
No case conversion is performed.
Beginning with release 2.3.6, an additional environment variable
is created. The value specified on this directive is converted to
upper case, prefixed with HTTP_, and hyphens are
converted to underscores.
Note
Most request headers are already available to the application
as environment variables, and generally are prefixed with
HTTP_. (Notable exceptions are Content-type
and Content-length, which do not have the
HTTP_ prefix.) Thus, this directive is only required
for request headers that are purposefully omitted, such as
Authorization and Proxy-Authorization.
Only pass these request headers if absolutely required.

Idle application processes which have existed for greater
than this time will be terminated, if the number of processses for the
class exceeds
FcgidMinProcessesPerClass.
A value of 0 disables the check.
This process lifetime check is performed at the frequency of the configured
FcgidIdleScanInterval.
This setting will apply to all applications spawned for this
server or virtual host. Use
FcgidCmdOptions to apply
this setting to a single application.
This module uses shared memory on Unix to maintain state which
is shared between httpd processes. This directive specifies the
name of the shared memory file.
Lower values of this directive increase the allowed spawn rate.
Refer to the FcgidSpawnScoreUpLimit
directive for more information.

A process activity score is maintained for each FastCGI application;
the score is used to control the rate of spawning in order to avoid
placing too much load on the system, particularly for applications that
are repeatedly exiting abnormally.
The value of FcgidSpawnScore
is added to the score for every spawned application process. The value of
FcgidTerminationScore is added
to the score for every terminated application process. The value of
FcgidTimeScore is subtracted
from the score every second.
When the current score is higher than the value of
FcgidSpawnScoreUpLimit, no additional application
processes will be spawned; subsequent requests must wait until an existing
process is free or until the score decreases below the limit.
If the limit is reached under normal load, it may not be sufficient to
simply increase the limit, as that would only delay the amount of time
before the limit is reached again. Decrease the value of
FcgidSpawnScore and/or
FcgidTerminationScore, or
increase the value of FcgidTimeScore,
to allow a higher rate of spawning.

Lower values of this directive increase the allowed spawn rate. Negative
values can be useful in some circumstances, such as allowing process
replacement without increasing the score.
Refer to the FcgidSpawnScoreUpLimit
directive for more information.
Higher values of this directive increase the allowed spawn rate.
Refer to the FcgidSpawnScoreUpLimit
directive for more information.
Uses Job Control Objects on Windows, only, to enforce shutdown of all
fcgi processes created by the httpd worker when the httpd worker has been
terminated. Processes terminated in this way do not have the opportunity
to clean up gracefully, complete pending disk writes, or similar closure
transactions, therefore this behavior is experimental and disabled, by
default.

The given command is used to spawn FCGI server processes. If this directive
is not used, the file pointed to by the request URL will be used instead.
Options for the command can be included using quotation marks surrounding
the command and options.
The optional suffix argument restricts the use of this FCGI
server to all URLs with the given exact path suffix. A suffix needs to start
with '.'.
The virtual flag signals that there will be no check
whether the request URL actually points to an existing file. The only
file which needs to exist is the wrapper itself.
The directive can be used multiple times. A wrapper defined without a suffix
is used as a default in case no suffix matches.

The module checks for exited FastCGI applications at this interval.
During this period of time, the application may exist in the process
table as a zombie (on Unix).