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

#!/bin/sh
#
# php-cgi - php-fastcgi swaping via  spawn-fcgi
#
# chkconfig:   - 85 15
# description:  Run php-cgi as app server
# processname: php-cgi
# config:      /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile:     /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
server_childs=5
pidfile="/var/run/php_cgi.pid"
 
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
 
start() {
    [ -x $php_cgi ] || exit 1
    [ -x $spawnfcgi ] || exit 2
    echo -n $"Starting $prog: "
    daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
    retval=$?
    echo
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} $prog -QUIT
    retval=$?
    echo
    [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
    return $retval
}
 
restart(){
	stop
	sleep 2
	start
}
 
rh_status(){
	status -p ${pidfile} $prog
}
 
case "$1" in
    start)
        start;;
    stop)
        stop;;
    restart)
        restart;;
    status)
        rh_status;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 3
esac
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 5 comments… read them below or add one }

1 Kylix.Tan December 31, 2009

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

2 Vivek Gite January 1, 2010

Run the following and try again
yum clean all

3 Diesel February 22, 2010

Thx! Script is very useful!

4 Antony March 1, 2011

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.

5 Atesin March 8, 2012

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}

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 + 14 ?
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: /etc/init.d/php_cgi, /usr/bin/php-cgi, centos nginx php fastcgi, Fedora nginx php fastcgi, function library, networking configuration, php-cgi, redhat nginx php fastcgi, retval, server ip, server port, spawn-fcgi

Previous Script:

Next Script: