Project

General

Profile

Actions

I hope somebody might also find this useful as a init script for several fcgi php daemons with individual user permissions as described here


#! /bin/sh
### BEGIN INIT INFO
# Provides:          FastCGI servers for PHP
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Start PHP FastCGI servers.
# Description:       PHP can be started with lighttpd's spawn-fcgi binary to
#                    get individual user permisions. This must be done
#                    for each normal user which wants to have PHP support.
### END INIT INFO
#
# Author:  Jannis Leidel
#          <jannis AT leidel.info>.
#
# Version: @(#)php-fcgi 0.1 12-May-2007 jannis AT leidel.info
#

# List of user which need own PHP-Fcgi processes
# e.g. FCGI_USER_LIST="user1 user2 user3" 
FCGI_USER_LIST="" 

# Set the socket and pid directory and make sure it exists 
RUNFILES_PATH=/var/run/php-fcgi
mkdir -p $RUNFILES_PATH

FCGI_PROGRAM="/usr/bin/php-cgi" 
FCGI_WEB_SERVER_ADDRS="127.0.0.1" 
PHP_FCGI_CHILDREN=2
PHP_FCGI_MAX_REQUESTS=1000

ALLOWED_ENV="PATH USER" 
#### DO NOT CHANGE ANYTHING AFTER THIS LINE!

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="PHP FastCGI servers" 

# Function that starts all php daemons.
d_start()
{
    export PHP_FCGI_MAX_REQUESTS
    export FCGI_WEB_SERVER_ADDRS
    ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS" 
    E=
    for i in $ALLOWED_ENV; do
        E="$E $i=${!i}" 
    done

    for FCGI_USER in $FCGI_USER_LIST
    do
        echo -n " $FCGI_USER" 
        if [ -f $RUNFILES_PATH/$FCGI_USER.pid ]; then
            echo -n " already running" 
        else
            start-stop-daemon --start --quiet --pidfile $RUNFILES_PATH/$FCGI_USER.pid \
                --chuid $FCGI_USER --exec /usr/bin/env -- spawn-fcgi \
                -s $RUNFILES_PATH/$FCGI_USER.sock -f $FCGI_PROGRAM \
                -u $FCGI_USER -g $FCGI_USER -C $PHP_FCGI_CHILDREN \
                -P $RUNFILES_PATH/$FCGI_USER.pid 2>/dev/null

            chmod 400 $RUNFILES_PATH/$FCGI_USER.pid
            chmod 777 $RUNFILES_PATH/$FCGI_USER.sock
        fi
    done
}

# Function that stops all php daemons.
d_stop() {
    for FCGI_USER in $FCGI_USER_LIST
    do
        echo -n " $FCGI_USER" 
        start-stop-daemon --stop --quiet --pidfile $RUNFILES_PATH/$FCGI_USER.pid \
                          || echo -n " not running" 
        if [ -f $RUNFILES_PATH/$FCGI_USER.pid ]; then
           rm $RUNFILES_PATH/$FCGI_USER.pid
        fi
    done
}

ACTION="$1" 
case "$ACTION" in
    start)
        echo -n "Starting $DESC: " 
        d_start
        echo "." 
        ;;

    stop)
        echo -n "Stopping $DESC: " 
        d_stop
        echo "." 
        ;;

    restart|force-reload)
        echo -n "Restarting $DESC: " 
        d_stop
        sleep 2
        d_start
        echo "." 
        ;;

    *)
        echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

exit 0

Updated by Anonymous over 12 years ago · 3 revisions