4

I am trying to install fast-cgi for nginx on an EC2 instance. I followed the steps explained here, but that is meant for Debian and does not work out of the box for a red-hat based system. I modified the script a bit to look like -

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

. /etc/rc.d/init.d/functions

(( EUID )) && echo .You need to have root priviliges.. && exit 1
BIND=/tmp/php.socket
USER=nginx
PHP_FCGI_CHILDREN=15
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 $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      #ORIGINAL LINE
      #daemon $PHP_CGI --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      #MODIFIED LINE
      daemon --user=$USER $PHP_CGI -b $BIND&
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/php-fcgi
      #echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
      rm /var/lock/subsys/php-fcgi
}

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

The problem I have now is -

  1. service php-fcgi start keeps the shell blocked. If I run service php-fcgi start & and then ps aux, I see the php-cgi process running bound to the socket. I see the start command stop only when I execute service php-fcgi stop. How do I solve this blocking issue? I have tried adding an & at the end of the line spawning the daemon. But other scripts do not seem to be doing this. This is the most complicated script I am attempting to modify yet :-(
  2. How do I get the script to display the green [ OK ]? I checked scripts like httpd and saw that all they were doing was something as shown below. But I never see a green [ OK ] when I execute php-fcgi. I also discovered that putting echo_success with functions sourced displays the green [ OK ] but I do not see any other scripts in the /etc/rc.d/init.d/ executing echo_success or echo_failure. What have I got wrong?
  3. Also, How do i specify PHP_FCGI_CHILDREN with daemon?
echo
      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/
Pang
  • 273
  • 3
  • 8
Lord Loh.
  • 1,089
  • 3
  • 16
  • 25

2 Answers2

3
  1. Change the daemon line to:

    daemon --user $USER --pidfile=$PIDFILE "$PHP_CGI -b $BIND &> /dev/null &"
    

    &> /dev/null is equipvalent to >/dev/null 2>&1, means that redirect both stdout and stderr to /dev/null.

  2. If that doesn't show the [ OK ] flag, try this:

    if [ -n "$pid" ]; then
        echo $pid > $PIDFILE
        success "Starting php-cgi service"
    else
        failure "Starting php-cgi service"
    fi
    

    Take a look at the success function in the /etc/init.d/functions for more details.

    The stop function should change to:

    killproc -p $PIDFILE $PHP_CGI_NAME
    
  3. Insert the PHP_CGI_ARGS as an environment variable to the daemon function:

    daemon --user $USER --pidfile=$PIDFILE "env - $PHP_CGI_ARGS $PHP_CGI -b $BIND &> /dev/null &"

Moreover, to make it start automatically at boot, you should change the init info to the Red Hat based style:

# chkconfig: 345 85 15
# description: Running php-cgi
# processname: php-cgi
# config: /etc/sysconfig/php-cgi
quanta
  • 51,413
  • 19
  • 159
  • 217
  • There should be an echo after the `success`, `failure` or `warning` functions, as in `success; echo "Good job!"` – mFeinstein May 03 '17 at 00:55
  • As a side note, I can only print the `[ OK ]` message from `success` if I use `echo`, and I can't see how it should work otherwise after reading the code for `success`... But, there are pieces of code showing `success` messages without `echo` I have no idea how it could work, so if someone understands it, please to tell me – mFeinstein May 09 '17 at 07:17
2

So, you're on a redhat based system, and you want a version of PHP that comes with FPM (the best way to do cgi on PHP >= 5.3)? Why don't you just install the packages from somewhere like IUS: http://iuscommunity.org/ , rather then trying to get a custom-built version working? The repository will allow you to stay up to date a lot easier, and will generally make your life easy.

Once the repo is installed, this is all you'd need to do: chkconfig php-fpm start; service php-fpm start, and you'll be all set.

devicenull
  • 5,622
  • 1
  • 26
  • 31
  • Thank you for the suggestion. While this will help me with the fast CGI, I would still like to know how to write init scripts. I tried reading a few, but am not able to replicate their behavior. – Lord Loh. Apr 16 '12 at 17:26