#!/bin/bash
## refer to:
##
## /manual/en/
## /2006/05/30/nginx-php-fastcgi-howto
##
## php-cgi file path
PHPFCGI=`which php-cgi`
## PID file path
PHP_PID="/tmp/"
## Bind TCP address
FCGI_BIND_ADDRESS="127.0.0.1:9000"
## Bind to Unix domain socket
#FCGI_BIND_ADDRESS="/tmp/"
## How many PHP child processes are derived
## This does not include the main process
PHP_FCGI_CHILDREN=16
## Maximum number of requests processed by each PHP process
PHP_FCGI_MAX_REQUESTS=4096
## User
USERID=verdana
################## no config below this line
# Switch the startup command according to the user
if test x$UID = x0; then
CMD="/bin/su -m -c \"$PHPFCGI -q -b $FCGI_BIND_ADDRESS\" $USERID"
else
CMD="$PHPFCGI -b $FCGI_BIND_ADDRESS"
fi
echo $CMD
# Related environment variables
E="PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS"
# Ignore other environment variables and start with a clean environment
nohup env - $E sh -c "$CMD" &> /dev/null &
# Record the PID of the PHP main process
# $! Returns the PID of sh
# Find the smallest PID in all php-cgi processes, which is the PID of the main process
MASTER_PID=`ps -e | grep 'php-cgi' | sed -n '1p' | awk '{print $1}'`
echo $MASTER_PID > "$PHP_PID"