CentOS / Red Hat PHP FastCGI script For Ngnix / Lighttpd / Apache Webserver

by on April 22, 2009 · 5 comments

A shell script to start / stop / restart php-cgi as an app server. By default it will bind to 127.0.0.1 IP and 9000 port using spawn-fcgi. This script is tested on CentOS / RHEL / Fedora Linux only. FreeBSD specific version available here.

How do I use this script?

Install spawn-fcgi package using yum command.

Download this script and place in /etc/init.d/ directory as /etc/init.d/php_cgi
# wget http://bash.cyberciti.biz/dl/419.sh.zip -O /etc/init.d/php_cgi
# chmod +x /etc/init.d/php_cgi

php_cgi init.d sysv style script

  1. #!/bin/sh
  2. #
  3. # php-cgi - php-fastcgi swaping via spawn-fcgi
  4. #
  5. # chkconfig: - 85 15
  6. # description: Run php-cgi as app server
  7. # processname: php-cgi
  8. # config: /etc/sysconfig/phpfastcgi (defaults RH style)
  9. # pidfile: /var/run/php_cgi.pid
  10. # Note: See how to use this script :
  11. # http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
  12. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14.  
  15. # Source networking configuration.
  16. . /etc/sysconfig/network
  17.  
  18. # Check that networking is up.
  19. [ "$NETWORKING" = "no" ] && exit 0
  20.  
  21. spawnfcgi="/usr/bin/spawn-fcgi"
  22. php_cgi="/usr/bin/php-cgi"
  23. prog=$(basename $php_cgi)
  24. server_ip=127.0.0.1
  25. server_port=9000
  26. server_user=nginx
  27. server_group=nginx
  28. server_childs=5
  29. pidfile="/var/run/php_cgi.pid"
  30.  
  31. # do not edit, put changes in /etc/sysconfig/phpfastcgi
  32. [ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
  33.  
  34. start() {
  35. [ -x $php_cgi ] || exit 1
  36. [ -x $spawnfcgi ] || exit 2
  37. echo -n $"Starting $prog: "
  38. daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
  39. retval=$?
  40. echo
  41. return $retval
  42. }
  43.  
  44. stop() {
  45. echo -n $"Stopping $prog: "
  46. killproc -p ${pidfile} $prog -QUIT
  47. retval=$?
  48. echo
  49. [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
  50. return $retval
  51. }
  52.  
  53. restart(){
  54. stop
  55. sleep 2
  56. start
  57. }
  58.  
  59. rh_status(){
  60. status -p ${pidfile} $prog
  61. }
  62.  
  63. case "$1" in
  64. start)
  65. start;;
  66. stop)
  67. stop;;
  68. restart)
  69. restart;;
  70. status)
  71. rh_status;;
  72. *)
  73. echo $"Usage: $0 {start|stop|restart|status}"
  74. exit 3
  75. esac


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

Click here to subscribe via email.

  • Kylix.Tan

    I am not sure if there is daemon command in redhat/centos distribution or somewhere else

  • Vivek Gite

    Run the following and try again
    yum clean all

  • Diesel

    Thx! Script is very useful!

  • Antony

    I have used this script with CentOS 5.5. I have nginx, php5 installed following your tutorial. I installed spawn-fcgi with yum from epel. I have in chkconfig a service called spawn-fcgi and a service called php_cgi.
    if i run service spawn-fcgi it doesn’t start whereas service php_cgi start works great!
    When the server reboot it does not start php_cgi auomatically..why?
    Thanks.

  • Atesin

    if you run the web server in the same machine, better you use unix sockets .. modify this couple lines

    ~24
    #server_ip=127.0.0.1
    #server_port=9000
    server_socket=/var/lib/php/php-cgi.sock

    ~40
    #daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
    daemon $spawnfcgi -s ${server_socket} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}

Previous Script:

Next Script: