[php-maint] Bug#426780: Init-script for php-cgi in external FASTCGI Mode (Daemon mode)

Roger Pettersson noname at tele2.se
Thu Dec 2 06:09:56 UTC 2010


I use env -i to start php-cgi else php environment gets polluted by bash env.
One example is if you run pam-tmpdir things that uses TMPDIR will fail.

/etc/default/php-fastcgi

#
# Settings for php-cgi in external FASTCGI Mode
#

# Should php-fastcgi run automatically on startup? (default: no)
START=yes

# Init script verbose output.
# INIT_VERBOSE=yes

# Listen type (default: tcp)
PHP_FCGI_TYPE=tcp

# Which user runs PHP? (default: www-data)
PHP_FCGI_USER=www-data

# Host and TCP port for FASTCGI-Listener (default: localhost:9000)
PHP_FCGI_HOST=127.0.0.1
PHP_FCGI_PORT=9000

# Unix socket.
PHP_FCGI_SOCKET=/var/run/php-fastcgi/php-fastcgi.sock

# Environment variables, which are processed by PHP
PHP_FCGI_CHILDREN=3
PHP_FCGI_MAX_REQUESTS=1024

/etc/init.d/php-fastcgi

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fastcgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description:       Start and stop php-cgi in external FASTCGI mode
### END INIT INFO

# Author: Kurt Zankl <kz at xon.uni.cc> modified by bac0n <noname at tele2.se>.

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi in external FASTCGI mode"                                                                                                                                                                                                                                        
NAME=php-fastcgi                                                                                                                                                                                                                                                               
DAEMON=/usr/bin/php-cgi                                                                                                                                                                                                                                                        
PIDDIR=/var/run/php-fastcgi                                                                                                                                                                                                                                                    
PIDFILE=$PIDDIR/$NAME.pid                                                                                                                                                                                                                                                      
SOCKETDIR=/var/run/php-fastcgi                                                                                                                                                                                                                                                 
SCRIPTNAME=/etc/init.d/$NAME                                                                                                                                                                                                                                                   
ENV=/usr/bin/env                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
# Exit if the package is not installed.                                                                                                                                                                                                                                        
[ -x "$DAEMON" ] || exit 0                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
# Read configuration variable file if it is present.                                                                                                                                                                                                                           
[ -r /etc/default/$NAME ] && . /etc/default/$NAME                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                               
# Load the VERBOSE setting and other rcS variables                                                                                                                                                                                                                             
. /lib/init/vars.sh                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                               
# Define LSB log_* functions.                                                                                                                                                                                                                                                  
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.                                                                                                                                                                                                           
. /lib/lsb/init-functions                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                               
# If the daemon is not enabled, give the user a warning and then exit,                                                                                                                                                                                                         
# unless we are stopping the daemon.                                                                                                                                                                                                                                           
if [ "$START" != "yes" -a "$1" != "stop" ]; then                                                                                                                                                                                                                               
        log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"                                                                                                                                                                                           
        exit 0                                                                                                                                                                                                                                                                 
fi                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                               
# Set up pid directory if it doesnt exists.                                                                                                                                                                                                                                    
if [ ! -d "$PIDDIR" ]; then                                                                                                                                                                                                                                                    
        [ "$VERBOSE" != "no" ] && log_begin_msg "Setting up pid directory"                                                                                                                                                                                                     
        mkdir -p $PIDDIR                                                                                                                                                                                                                                                       
        chown ${PHP_FCGI_USER}.root $PIDDIR                                                                                                                                                                                                                                    
        [ "$VERBOSE" != "no" ] && log_end_msg 0                                                                                                                                                                                                                                
fi                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                               
# Daemon arguments.
if [ $PHP_FCGI_TYPE == "socket" ]; then

        # Set up socket directory if it doesnt exists.
        if [ ! -d "$SOCKETDIR" ]; then
                [ "$VERBOSE" != "no" ] && log_begin_msg "Setting up socket directory"
                mkdir -p $SOCKETDIR
                chown ${PHP_FCGI_USER}.root $SOCKETDIR
                [ "$VERBOSE" != "no" ] && log_end_msg 0
        fi
        DAEMON_ARGS="-q -b $PHP_FCGI_SOCKET"
else
        DAEMON_ARGS="-q -b $PHP_FCGI_HOST:$PHP_FCGI_PORT"
fi

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1

        # Start with clean environment.
        if [ $PHP_FCGI_TYPE == "socket" ]; then
        start-stop-daemon --start --quiet --background --make-pidfile --chuid $PHP_FCGI_USER --pidfile $PIDFILE --exec $ENV -- -i \
                PHP_FCGI_SOCKET=$PHP_FCGI_SOCKET \
                PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN \
                PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS \
                $DAEMON $DAEMON_ARGS || return 2
        else
        start-stop-daemon --start --quiet --background --make-pidfile --chuid $PHP_FCGI_USER --pidfile $PIDFILE --exec $ENV -- -i \
                PHP_FCGI_HOST=$PHP_FCGI_HOST \
                PHP_FCGI_PORT=$PHP_FCGI_PORT \
                PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN \
                PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS \
                $DAEMON $DAEMON_ARGS || return 2
        fi
}

do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null
        [ $? -eq 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ $? -eq 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        # Return last error.
        return $?
}

case "$1" in
        start)
                [ "$VERBOSE" != "no" ] && log_daemon_msg "Starting $DESC" "$NAME"
                do_start
                if [ "$VERBOSE" != "no" ]; then
                        case $? in
                                0|1) log_end_msg 0 ;;
                                2)   log_end_msg 1 ;;
                        esac
                fi
        ;;
        stop)
                [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
                do_stop
                if [ "$VERBOSE" != "no" ]; then
                        case "$?" in
                                0|1) log_end_msg 0 ;;
                                2)   log_end_msg 1 ;;
                        esac
                fi
        ;;
        restart|force-reload)
                log_daemon_msg "Restarting $DESC" "$NAME"
                do_stop
                case "$?" in
                        0|1)
                                do_start
                                case "$?" in
                                        0) log_end_msg 0 ;;
                                        1) log_end_msg 1 ;; # Old process is still running
                                        *) log_end_msg 1 ;; # Failed to start
                                esac
                        ;;
                        *)
                                # Failed to stop
                                log_end_msg 1
                        ;;
                esac
        ;;
        *)
                echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
                exit 3
        ;;
esac
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.alioth.debian.org/pipermail/pkg-php-maint/attachments/20101202/77b4811f/attachment.pgp>


More information about the pkg-php-maint mailing list