≡ Menu

Shell Script To Start FastCGI PHP Server For Nginx Web Server

This script is tested under both FreeBSD and Linux. You need to modify paths and server port numbers.

How do I use this script

Download script
# fetch http://bash.cyberciti.biz/dl/251.sh.zip
# unzip 251.sh.zip
# mv 251.sh /usr/local/etc/rc.d/fastcgi.php
# chmod +x /usr/local/etc/rc.d/fastcgi.php

Sample nginx config - nginx.conf

  1.  
  2. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  3. location ~ \.php$ {
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. fastcgi_param SCRIPT_FILENAME /webroot/theos.in/http$fastcgi_script_name;
  7. include fastcgi_params;
  8. }
  9.  

Shell Script

  1. #!/bin/sh
  2. # Shell Script to start / stop PHP FastCGI using lighttpd - spawn-fcgi binary file.
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2006 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. PROVIDES=php-cgi
  11. LIGHTTPD_FCGI=/usr/local/bin/spawn-fcgi
  12. SERVER_IP=127.0.0.1
  13. SERVER_PORT=9000
  14. SERVER_USER=www
  15. SERVER_GROUP=www
  16. PHP_CGI=/usr/local/bin/php-cgi
  17. PGREP=/bin/pgrep
  18. KILLALL=/usr/bin/killall
  19. ### No editing below ####
  20. cmd=$1
  21.  
  22. pcgi_start(){
  23. echo "Starting $PROVIDES..."
  24. $LIGHTTPD_FCGI -a $SERVER_IP -p $SERVER_PORT -u $SERVER_USER -g $SERVER_GROUP -f $PHP_CGI
  25. }
  26.  
  27. pcgi_stop(){
  28. echo "Killing $PROVIDES..."
  29. $KILLALL $PROVIDES
  30. }
  31.  
  32. pcgi_restart(){
  33. pcgi_stop
  34. pcgi_start
  35. }
  36.  
  37. pcgi_status(){
  38. $PGREP $PROVIDES > /dev/null
  39. [ $? -eq 0 ] && echo "$PROVIDES running" || echo "$PROVIDES NOT running"
  40.  
  41. }
  42.  
  43. pcgi_help(){
  44. echo "Usage: $0 {start|stop|restart|status}"
  45. }
  46.  
  47.  
  48. case ${cmd} in
  49. [Ss][Tt][Aa][Rr][Tt]) pcgi_start;;
  50. [Ss][Tt][Oo][Pp]) pcgi_stop;;
  51. [Rr][Ee][Ss][Tt][Aa][Rr][Tt]) pcgi_restart;;
  52. [Ss][Tt][Aa][Tt][Uu][Ss]) pcgi_status 0;;
  53. *) pcgi_help ;;
  54. esac

Comments on this entry are closed.

  • 2ker

    Very helpful – thanks!