FreeBSD Configure Apache 2.2 PHP with FastCGI mod_fcgi Module

by on April 6, 2008 · 21 comments· LAST UPDATED August 8, 2009

in , ,

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?

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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 21 comments… read them below or add one }

1 Ionut April 9, 2008 at 6:18 pm

thanks, nice.

# portsanp fetch update

has to be:

# portsnap fetch update

Reply

2 nixCraft April 9, 2008 at 6:41 pm

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

Reply

3 Mel May 17, 2008 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 :)

Reply

4 nixCraft May 17, 2008 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.

Reply

5 Mel May 20, 2008 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

Reply

6 root user January 1, 2009 at 4:45 pm

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

Reply

7 nixCraft January 1, 2009 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?

Reply

8 Binit February 14, 2009 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

Reply

9 Borg June 11, 2009 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.

Reply

10 Anderson July 10, 2009 at 8:43 am

I have problem when i start apache

srv# apachectl start
Syntax error on line 187 of /usr/local/etc/apache22/httpd.conf:
Invalid command ‘FCGIWrapper’, perhaps misspelled or defined by a module not included in the server configuration

I running in FreeBSD 7.2 and Apache 2.2.11. How to fix it?

Thanks
Anderson

Reply

11 nixCraft July 10, 2009 at 9:04 am

Did you added the following to your config file?

LoadModule fcgid_module libexec/apache22/mod_fcgid.so

Reply

12 Anderson July 10, 2009 at 9:54 am

yes. I added in httpd.conf. look like this right?

………………………..
LoadModule rewrite_module libexec/apache22/mod_rewrite.so
LoadModule fastcgi_module libexec/apache22/mod_fastcgi.so
#LoadModule php5_module libexec/apache22/libphp5.so

………………………….

Reply

13 Anderson July 10, 2009 at 10:08 am

Oh! i get it…

This right?
LoadModule fcgid_module libexec/apache22/mod_fcgid.so

and This wrong?
LoadModule fastcgi_module libexec/apache22/mod_fastcgi.so

Reply

14 Fred August 8, 2009 at 6:58 pm

Great post! Thanks!

Have you ever got this setup running in a jail? I’m getting a “not implemented” error preventing apache from starting, but only in jailed environments.

Reply

15 nixCraft August 8, 2009 at 7:04 pm

@Fred,

Yes, I’m running in FreeBSD jail without any problem. Are you talking about chroot(2) based jail or FreeBSD jail(8) or mod_chroot?

Reply

16 nsamu January 3, 2011 at 6:56 pm

I receive the same error. I am running a FreeBSD jail created using ezjajil.

Reply

17 SSJ June 30, 2010 at 10:28 am

Hello,

thank you for this article. I have followed every step.

no errors at apache22 startup, but can’t get working .html files, only php is working via fcig.

when I enter http://url/index.php is ok, when I enter http://url/index.html got 500 error. File permisions is ok 644 and owner is ok ( same as index.php )

where I’m wrong?

Thank you

Reply

18 yudanta October 19, 2010 at 4:29 am

i have the same probleme that found by SSJ, when i open index.php its ok, but if i open index.html i have 500 error. this is my dir_module on httpd.conf

DirectoryIndex index.html index.shtml index.cgi index.php

thanks before

Reply

19 Aeomer November 10, 2011 at 2:20 pm

This is an oldish thread, but I had to comment on some ‘Whys?” rather than the list of “Hows?”.

It is a common myth that mod_php is slower – it is faster in real world applications, but *only just* compared to fcgid. fgcid + eAccelerator is demonstrably fractionally slower than mod_php + eAccelerator. So why not just use mod_php? The answer is two fold.

1. There are more security options using fcgid. This is only relevant when you have untrusted user scripts such as found in hosting environments.
2. You can use worker-mpm on Apache. Let’s face it, Apache is still in the Stone-Age when it comes to threading. Worker-mpm improves resource utilization on high traffic sites significantly, but mod_php in such a configuration is not guaranteed. The reason is not mod_php (which is thread-safe) but all the various extensions you may use with it (which are often not thread-safe). By using mod_fcgid you can use threaded apache and not worry about php extension thread-safety.

In the real-world I use both mod_php and fcgid depending on my requirements. mod_php for my mercurial server that is heavily fortified and has a limited user base, and mod_fcgid for load balanced high-performance sites, 80,000 or more hits per minute, where the pre-fork model is just not tenable.
YMMV

Real Story: A existing website with a following of more than 130,000 registered users and an awful lot of lurkers was re-engineered to not only look more modern but also link into a vast number of business processes. The site went live, and every one of the user base hit it in the first five minutes after launch. Despite the number of servers and virtual servers in the cluster the pre-fork model simply could not cope. Switching to worker-mpm handled the load easily, but some of the apache instances crashed randomly. Changing to use fcgid rather than mod_php sorted the crashing issue. I never did get an an answer as to which php optional extension (we used a lot of them) caused the issue. Now there’s a task for someone with time on their hands! :-)

Reply

20 alexus February 23, 2012 at 3:58 am

[Thu Feb 23 03:55:46 2012] [emerg] (78)Function not implemented: mod_fcgid: Can’t create shared memory for size 1192488 bytes

I keep get this error as soon as I do LoadModule

Reply

21 Bruce Kirkpatrick June 22, 2012 at 3:50 pm

I looked at dozens of web sites on how to setup apache 2.2 + php 5.3 + mod_fcgi in centos linux and this is the only one that works for me. Thanks so much.

Reply

Leave a Comment

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

Previous Faq:

Next Faq: