FreeBSD Configure Apache 2.2 PHP with FastCGI mod_fcgi Module
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:
# 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:
/usr/local/bin/php-cgi -v
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.
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- FreeBSD Lighttpd fastcgi php configuration and installation
- Reconfiguring FreeBSD Ports - Remove OPTIONS config For Any Port
- FreeBSD Set a Default Route / Gateway
- FreeBSD admin book / handbook
- FreeBSD Static Routing Configuration
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
Tags: apache 2, binary compatibility, fastcgi, fcgi, freebsd php apache, freebsd php fastcgi, freebsd php5 fastcgi, httpd, mod_fcgi, php5, ports, process management, suhosin, zend engine, zend technologies ~ Last updated on: April 9, 2008




April 9th, 2008 at 6:18 pm
thanks, nice.
# portsanp fetch update
has to be:
# portsnap fetch update
April 9th, 2008 at 6:41 pm
Thanks for the heads up. The faq has been updated.