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:

  1. <VirtualHost *:80>
  2. ServerAdmin webmaster@example.com
  3. DocumentRoot "/websites/home/example.com/http"
  4. ServerName example.com
  5. ServerAlias www.example.com
  6. ErrorLog "/websites/home/example.com/logs/error.log"
  7. CustomLog "/websites/home/example.com/logs/access.log" common
  8. ScriptAlias /cgi-bin/ "/websites/home/example.com/cgi-bin/"
  9.  
  10. <Directory "/websites/home/example.com/http">
  11. Options -Indexes FollowSymLinks +ExecCGI
  12. AllowOverride AuthConfig FileInfo
  13. AddHandler php5-fastcgi .php
  14. Action php5-fastcgi /cgi-bin/php.cgi
  15. Order allow,deny
  16. Allow from all
  17. </Directory>
  18.  
  19. <Directory "/websites/home/example.com/cgi-bin">
  20. AllowOverride None
  21. Options None
  22. Order allow,deny
  23. Allow from all
  24. </Directory>
  25.  
  26. #SuExecUserGroup exampleUser exampleGroup
  27. </VirtualHost>

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

Sample mod_fastcgi PHP script

  1. #!/bin/sh
  2. #
  3. # Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
  4. #
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # Comment/suggestion: <vivek@nixCraft.com>
  9. # http://bash.cyberciti.biz/misc-shell/linux-start-stop-restar-toracle-server/
  10. # -------------------------------------------------------------------------
  11.  
  12. ### Set PATH ###
  13. PHP_CGI=/usr/local/bin/php-cgi
  14. PHP_FCGI_CHILDREN=4
  15. PHP_FCGI_MAX_REQUESTS=1000
  16. ### no editing below ###
  17. export PHP_FCGI_CHILDREN
  18. export PHP_FCGI_MAX_REQUESTS
  19. exec $PHP_CGI


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Riadh

    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.

  • Quoc

    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

Previous Script:

Next Script: