Apache FastCGI / mod_fastcgi PHP Script

by on October 7, 2008 · 2 comments

Use this script to run Apache 2.x with mod_fastcgi. Install Apache 22 from FreeBSD port or using source code. The script is tested under FreeBSD and Debian / RHEL / CentOS Linux.

How do I use script?

Download the script

Put in cgi-bin directory as php.cgi

Set permission

Configure httpd.conf as follows for mod_fastcgi:

<VirtualHost *:80>
        ServerAdmin webmaster@example.com
        DocumentRoot "/websites/home/example.com/http"
        ServerName example.com
        ServerAlias www.example.com
        ErrorLog "/websites/home/example.com/logs/error.log"
        CustomLog "/websites/home/example.com/logs/access.log" common
        ScriptAlias /cgi-bin/ "/websites/home/example.com/cgi-bin/"
 
<Directory "/websites/home/example.com/http">
    Options -Indexes FollowSymLinks +ExecCGI
    AllowOverride AuthConfig FileInfo
    AddHandler php5-fastcgi .php
    Action php5-fastcgi /cgi-bin/php.cgi
    Order allow,deny
    Allow from all
</Directory>
 
<Directory "/websites/home/example.com/cgi-bin">
  AllowOverride None
  Options None
  Order allow,deny
  Allow from all
</Directory>
 
#SuExecUserGroup exampleUser exampleGroup
</VirtualHost>

See Red hat / CentOS Linux mod_fastcgi and FreeBSD mod_fastcgi tutorial for detailed information.;

Sample mod_fastcgi PHP script

#!/bin/sh
#
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
#
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/>
# This script is licensed under GNU GPL version 2.0 or above
# Comment/suggestion: <vivek@nixCraft.com>
# http://bash.cyberciti.biz/misc-shell/linux-start-stop-restar-toracle-server/
# -------------------------------------------------------------------------
 
### Set PATH ###
PHP_CGI=/usr/local/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 2 comments… read them below or add one }

1 Riadh September 27, 2010

I’ve read a dozen articles explaining how to set-up php5 with fast cgi without being able to make it work.
Now that I’ve read yours, everything works perfectly.
thanks.

2 Quoc September 25, 2011

try to turn off mod_cgi if it still work. This is the setting for mod_cgi, It’s does not use mod_fastcgi at all

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 10 + 10 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: apache 2, apache php fastcgi script, apache web server, fcgi, mod_fastcgi, php script, php5, PHP_FCGI_CHILDREN, PHP_FCGI_MAX_REQUESTS

Previous Script:

Next Script: