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:
# 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:
FreeBSD PHP5 mod_fcgi
(Fig. 01: PHP5 Configured as FastCGI using mod_fcgi)

Further readings:

Updated for accuracy.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 9 comments… read them below or add one }

1 Ionut 04.09.08 at 6:18 pm

thanks, nice.

# portsanp fetch update

has to be:

# portsnap fetch update

2 vivek 04.09.08 at 6:41 pm

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

3 Mel 05.17.08 at 12:28 pm

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_fcgid

Great article though, thanks for writing it up :)

4 vivek 05.17.08 at 2:45 pm

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.

5 Mel 05.20.08 at 3:43 pm

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

6 root user 01.01.09 at 4:45 pm

Sucks. Doesnt work. Why you missed /usr/ports/www/mod_fcgid part?

7 Vivek Gite 01.01.09 at 5:07 pm

@root user ,

No I did not missed /usr/ports/www/mod_fcgid. Please read it carefully. Do you get any sort of error?

8 Binit 02.14.09 at 10:54 pm

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

9 Borg 06.11.09 at 8:27 am

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.

Leave a Comment

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

Tagged as: , , , , , , , , , , , , , ,

Previous post: How To Open .TBZ ( tar.bz2 ) File Under Linux / UNIX

Next post: How To Start and Stop MySQL Database Server From A Shell Prompt