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 -
service php-fcgi start
keeps the shell blocked. If I runservice php-fcgi start &
and thenps aux
, I see the php-cgi process running bound to the socket. I see the start command stop only when I executeservice 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 :-(- 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 executephp-fcgi
. I also discovered that puttingecho_success
withfunctions
sourced displays the green[ OK ]
but I do not see any other scripts in the/etc/rc.d/init.d/
executingecho_success
orecho_failure
. What have I got wrong? - Also, How do i specify
PHP_FCGI_CHILDREN
withdaemon
?
echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/
Change the
daemon
line to:&> /dev/null
is equipvalent to>/dev/null 2>&1
, means that redirect both stdout and stderr to/dev/null
.If that doesn't show the
[ OK ]
flag, try this:Take a look at the
success
function in the/etc/init.d/functions
for more details.The
stop
function should change to:Insert the
PHP_CGI_ARGS
as an environment variable to thedaemon
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:
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.