Fixing php-fcgi (php5-fastcgi) Init Script

A long time ago when I setup my self-hosted WordPress blog (what you’re reading on now), I had to install PHP onto my system.  I believe I followed some guide for configuring a php-fcgi Init script (used to run and manage the program as a service).  I can not remember the exact details of what happened during that time, but I realized recently that even though the service starts, if I run the command ‘service php-fcgi status’, it will show up as having failed to start.

After some tweaking and editing, I now have the Init script running properly.  Below are the details as to what I had to change to get this to work.  If you installed php-fcgi and need an Init script for it, then you can use the final version below (this is verified to work on openSUSE 12.3).

Originally the PHP_CGI_ARGS variable was:

PHP_CGI_ARGS=”- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND”

…and the actual line to start the process in the start() function was

/sbin/startproc -u “$USER”  /usr/bin/env — “$PHP_CGI_ARGS”

What I believe the issue was, is that /sbin/startproc was essentially trying to daemonize the /usr/bin/env process instead of /usr/bin/php-cgi.  As you can see by the final version of the Init file below, I changed the PHP_CGI_ARGS variable to:

PHP_CGI_ARGS=”- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS”

…and the line in the start() function to:

/usr/bin/env — $PHP_CGI_ARGS /sbin/startproc -u $USER $PHP_CGI -b $BIND

This way /usr/bin/env, whose purpose in this script is to run a command with a modified environment, will run /sbin/startproc who in turn will daemonize the /usr/bin/php-cgi process (what we wanted to run as a service, a.k.a. daemon, in the first place).

Final version of ‘/etc/init.d/php-fcgi’:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          php-fcgi
# Required-Start:    $network
# Should-Start:      nginx lighttpd httpd
# Required-Stop:     $network
# Should-Stop:       nginx lighttpd httpd
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: starts php over fcgi
# Description:       starts php over fcgi
### END INIT INFO

(( EUID )) && echo .You need to have root priviliges.. && exit 1
BIND=127.0.0.1:9000
USER=wwwrun
PHP_FCGI_CHILDREN=2
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      /usr/bin/env -- $PHP_CGI_ARGS /sbin/startproc -u $USER $PHP_CGI -b $BIND
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

After changing your script, if on a current openSUSE system, you will have to run the following command before using the normal service command to start, stop, or restart the php-fcgi service.:

# systemctl daemon-reload

Leave a Reply

Your email address will not be published. Required fields are marked *