mod_fcgid

mod_fcgid was introduced to be binary compatible with FastCGI, but with better control over spawning processes. It is a high performance alternative to mod_cgi or mod_cgid, which starts a sufficient number instances of the CGI program to handle concurrent requests, and these programs remain running to handle further incoming requests.

Installing fcgid

For Red Hat or Centos,

1. First we install the required Apache components, with Apache threaded server (MPM Worker), to save memory.

# yum install apache2-mpm-worker libapache2-mod-fcgid

2. Then we enable the Apache fcgid module.

#a2enmod fcgid

3. And install PHP CGI, and the a few other PHP components, if they are not already on your system.

# yum install php5-cgi php5-curl php5-gd php5-mysql

Configuring fcgid

To configure fcgid, you have to do two things:

1. Create a new file in /etc/apache2/conf.d/php-fcgid.conf and add the following in it:

AddHandler fcgid-script .fcgi .php
# Where to look for the php.ini file?
DefaultInitEnv PHPRC “/etc/php5/cgi”
# Maximum requests a process handles before it is terminated
MaxRequestsPerProcess 1000
# Maximum number of PHP processes
MaxProcessCount 10
# Number of seconds of idle time before a process is terminated
IPCCommTimeout 240
IdleTimeout 240
#Or use this if you use the file above
FCGIWrapper /usr/bin/php-cgi .php

ServerLimit 500
StartServers 3
MinSpareThreads 3
MaxSpareThreads 10
ThreadsPerChild 10
MaxClients 300
MaxRequestsPerChild 1000

For a large site with a server with more memory and CPUs we can use this:

AddHandler fcgid-script .fcgi .php
# Where to look for the php.ini file?
DefaultInitEnv PHPRC “/etc/php5/cgi”
# Where is the PHP executable
FCGIWrapper /usr/bin/php-cgi .php
# Maximum requests a process handles before it is terminated
MaxRequestsPerProcess 1500
# Maximum number of PHP processes.
MaxProcessCount 45
# Number of seconds of idle time before a process is terminated
IPCCommTimeout 240
IdleTimeout 240

# Large site

ServerLimit 2048
ThreadLimit 100
StartServers 10
MinSpareThreads 30
MaxSpareThreads 100
ThreadsPerChild 64
MaxClients 2048
MaxRequestsPerChild 5000

2. Add ExecCGI to the Options line in the vhosts you want to use PHP and or fcgid on. for example:

Order Allow,Deny
Allow From All
Allow Override All
Options MultiViews Indexes Includes FollowSymLinks ExecCGI

Now, restart Apache, and fcgid should be active.

Leave a Reply