Q. I'd like to switch from mod_php5 to mod_fastcgi. I'm using FreeBSD 7 release along with following software:
+ Apache 2.2
+ PHP as mod_php5
+ MySQL DB 5.1.23 server
How do I configure php as FastCGI server?
A. mod_fcgid has a new process management strategy, which concentrates on reducing the number of fastcgi server, and kick out the corrupt fastcgi server as soon as possible. It is a binary compatibility alternative to Apache module mod_fastcgi; so your existing fastcgi programs do not need to be recompiled. mod_fcgid supports suEXEC.
Why run PHP5 as mod_fcgi / mod_fastcgi?
FastCGI as has some serious advantages over mod_php5:
- You can do user level separations. You can enable quotas per user. Limit users by processes and CPU consumption.
- chroot security call per user possible
- According to several reports fastcgi works much faster than mod_php and cgi mode.
Step # 1: Install mod_fcgid
Make sure your ports are upto date:
# portsnap fetch update
Install mod_fcgid:
# cd /usr/ports/www/mod_fcgid
# make install clean
Make sure php supports FastCGI
Make sure php-cgi binary exists and it is compiled with fastcgi support:
# cd /usr/ports/lang/php5
# make showconfig | grep -i FASTCGI
Output:
FASTCGI=on "Enable fastcgi support (CGI only)"
Another way to test fastcgi support, enter:
# /usr/local/bin/php-cgi -v
Output:
PHP 5.2.5 with Suhosin-Patch 0.9.6.2 (cgi-fcgi) (built: Mar 6 2008 09:15:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
If you don't see word cgi-fcgi, recompile php with fastcgi support by visiting /usr/ports/lang/php5
# cd /usr/ports/lang/php5
# make config
# make install clean
Step # 3: Load mod_fcgi module
Open your httpd.conf file located at /usr/local/etc/apache22/ directory:
# vi /usr/local/etc/apache22/httpd.conf
Load mod_fcgi module:
LoadModule fcgid_module libexec/apache22/mod_fcgid.so
Configure mod_fcgi
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
FCGIWrapper /usr/local/bin/php-cgi .php
</IfModule>
Find your DocumentRoot directory configuration option that read as follows:
<Directory "/usr/local/www/apache22/data">
Append following two lines:
SetHandler fcgid-script
FCGIWrapper /usr/local/bin/php-cgi .php
Options ExecCGI
At the end configuration should read as follows:
# This should be changed to whatever you set DocumentRoot to. <Directory "/usr/local/www/apache22/data"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny SetHandler fcgid-script FCGIWrapper /usr/local/bin/php-cgi .php Options ExecCGI Allow from all </Directory>
Step # 4: Disable mod_php5
Find line that read as follows:
LoadModule php5_module libexec/apache22/libphp5.so
Comment out line:
#LoadModule php5_module libexec/apache22/libphp5.so
Also make sure following two line (mime type) exists:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Save and close the file.
Step # 5: Restart Apache22
Finally, restart apache web server:
# /usr/local/etc/rc.d/apache22 restart
Step # 5: Test mod_fcgi
Use following small program to verify mod_fcgi is working properly:
<?php phpinfo(); ?>
You must see Server API as CGI/FastCGI as well as following screen:

(Fig. 01: PHP5 Configured as FastCGI using mod_fcgi)
Further readings:
Updated for accuracy.
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 5-17-08


{ 9 comments… read them below or add one }
thanks, nice.
# portsanp fetch update
has to be:
# portsnap fetch update
Thanks for the heads up. The faq has been updated.
What I’m missing in this article is why not to use fcgi.
The most important reason to stick to the module, is when your site requires settings through php_value/php_flag apache configuration directives.
For example, I have several aliases and virtual hosts use different include_paths.
I now have to combine them in the system’s php.ini or maybe use php-cgi -c if the FCGIWrapper statement allows this.
Also, you’re missing the:
# cd /usr/ports/www/mod_fcgidGreat article though, thanks for writing it up :)
Mel,
Thanks for pointing out /usr/ports/www/mod_fcgid. Yes -c is the best option for virtual hosting. You can always write your own FCGIWrapper if you want.
Ok, I just got done troubleshooting why all documents were passed to php-cgi and as a result causing images and css to not be displayed, since php-cgi has no idea how to process png images :).
So, simplified:
. AddHandler binds a handler to a file extension
. SetHandler binds the context location to a handler.
Telltale:
[notice] mod_fcgid: process /usr/local/www/phpPgAdmin/images/themes/default/title.png(2905) exit(communication error), terminated by calling exit(), return code: 255
Sucks. Doesnt work. Why you missed /usr/ports/www/mod_fcgid part?
@root user ,
No I did not missed /usr/ports/www/mod_fcgid. Please read it carefully. Do you get any sort of error?
Thanks for posting this Vivek …. While I was able to get the fastCGI to work with PHP by following your instructions, it broke something else and was wondering if you had any ideas.
I now longer can access my regular webpages under the DocumentRoot as I am receiving the following error :
Forbidden
You don’t have permission to access / on this server.
Please note that the command: apachectl status also gives me the same error.
I found that when I comment out the following three lines nested within the Directory directive, my apache doesn’t error out:
…
SetHandler fcgid-script
FCGIWrapper /usr/local/bin/php-cgi .php
Options ExecCGI
…
The /var/log/httpd-error.log reads:
[warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[notice] Digest: generating secret for digest authentication …
[notice] Digest: done
[notice] Apache/2.2.6 (FreeBSD) mod_ssl/2.2.6 OpenSSL/0.9.8e DAV/2 configured — resuming normal operations
I’m running Apache 2.2.6 against FreeBSD 7.0
Could it be file or directory permissions ? If you have any suggestion, I’d be happy to hear them.
Thanks,
Binit
I followed the directions exactly and this does not work properly. The only thing close I can get working properly is fcgid and when I disable the php5 module, virtual hosting goes for an absolute crapper. I also could not get the CGI/FastCGI to show up in phpinfo.php. I could however get fcgid to work.