About Linux FAQ

Browse More FAQs:

FreeBSD Configure Apache 2.2 PHP with FastCGI mod_fcgi Module

Posted by Vivek on Sunday April 6, 08 @4:10 pm

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:
FreeBSD PHP5 mod_fcgi
(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:

Discussion on This FAQ

  1. Ionut Says:

    thanks, nice.

    # portsanp fetch update

    has to be:

    # portsnap fetch update

  2. vivek Says:

    Thanks for the heads up. The faq has been updated.

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , , ~ Last updated on: April 9, 2008

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.