# 20081106 - Script to spawn php5-cgi enabling Nginx to serve PHP code/pages

**URL:** <https://www.ruby-forum.com/t/20081106-script-to-spawn-php5-cgi-enabling-nginx-to-serve-php-code-pages/152688>\
**Category:** NGINX\
**Created:** [November 6, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/20081106-script-to-spawn-php5-cgi-enabling-nginx-to-serve-php-code-pages/152688 "2008-11-06T13:47:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [November 6, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/20081106-script-to-spawn-php5-cgi-enabling-nginx-to-serve-php-code-pages/152688/1 "2008-11-06T13:47:20Z")

</div>

The php5-cgi (until 5.2.4) package does not include a script to  
automatically start it in external FASTCGI Mode (Daemon mode).

As there is some confusion on the Internet on how to properly set PHP  
script execution in the Nginx Web server, I am attaching the script that  
we reliably use at our servers.

This is how we install it in Ubuntu/Debian(sudo enabled):  
sudo nano /etc/init.d/php-fastcgi # copy and paste the text between  
the “— cut here —” to this file and save it (+x, Y, ).  
sudo chmod u+x /etc/init.d/php-fastcgi  
sudo chown 0.0 /etc/init.d/php-fastcgi  
sudo update-rc.d php-fastcgi defaults 21 23

I hope it may also help you at your setups.  
M.

_Note: This version 20081106 includes changes to use Unix domain sockets  
instead of the more common IP:port arrangement_

— cut here —  
#! /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

# 

# Do NOT “set -e”

# Default values are enclosed in []

PATH=/usr/sbin:/usr/bin:/sbin:/bin

# Start php-fastcgi? [no]

START=yes

# Read configuration data

NAME=php-fastcgi  
DESC=“php-cgi runing in external FASTCGI mode”  
PIDFILE=/var/run/$NAME.pid  
SCRIPTNAME=/etc/init.d/$NAME

# Daemon pathname and args: IP=[127.0.0.1], port=[9000]

# /usr/bin/php-cgi links to -\> /etc/alternatives/php-cgi -\>

/usr/bin/php5-cgi  
DAEMON=/usr/bin/php-cgi  
DAEMON\_ARGS="-q -b 127.0.0.1:9000"

# 20081106: To use Unix domain sockets substitute the former line for:

# DAEMON\_ARGS="-q -b /tmp/php-fastcgi.socket"

# Take care to also change the relevant line(s) in nginx config file(s):

# from: fastcgi\_pass 127.0.0.1:9000;

# to: fastcgi\_pass unix:/tmp/php-fastcgi.socket;

# Then restart both php-cgi and nginx

# Which user runs PHP? [www-data]

EXEC\_AS\_USER=www-data

# php-cgi env. variables: spawned children [5] , concurrent requests

[1000]  
PHP\_FCGI\_CHILDREN=5  
PHP\_FCGI\_MAX\_REQUESTS=125  
export PHP\_FCGI\_CHILDREN PHP\_FCGI\_MAX\_REQUESTS

# Exit if php-cgi is not installed

[-x “$DAEMON”] || exit 0

# Load rcS variable settings and set (re)start/stop verbosity

[-f /etc/default/rcS] && . /etc/default/rcS  
VERBOSE=yes

# Define LSB log\_\* functions.

# Depend on lsb-base (\>= 3.0-6) to ensure that this file is present.

. /lib/lsb/init-functions

# If $START not ‘yes’ AND we are not stopping the $DAEMON

if [“$START” != “yes” -a “$1” != “stop”]; then  
log\_warning\_msg “To enable $NAME, edit /etc/init.d/$NAME and set  
START=yes”  
exit 0  
fi

do\_start()  
{

# Return values: 0=started ok,1=already running,2=unable to start

start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON   
–test \> /dev/null || return 1  
start-stop-daemon --start --quiet --background   
–chuid $EXEC\_AS\_USER --pidfile $PIDFILE --make-pidfile   
–exec $DAEMON – $DAEMON\_ARGS   
|| return 2  
}

do\_stop()  
{

# Return values: 0=stopped ok,1=already stopped,2=unable to stop,

# other if a failure occurred

```
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
  --pidfile $PIDFILE > /dev/null # --name $DAEMON

```

RETVAL="$?"  
[“$RETVAL” = 2] && return 2

# Wait for children to finish too.

start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5   
–exec $DAEMON  
[“$?” = 2] && return 2

# Many daemons don’t delete their pidfiles when they exit.

rm -f $PIDFILE  
return “$RETVAL”  
}

case “$1” in  
start)  
[“$VERBOSE” != no] && log\_daemon\_msg “Starting $DESC”  
do\_start  
case “$?” in  
0|1) [“$VERBOSE” != no] && log\_end\_msg 0 ;;  
2) [“$VERBOSE” != no] && log\_end\_msg 1 ;;  
esac  
;;  
stop)  
[“$VERBOSE” != no] && log\_daemon\_msg “Stopping $DESC”  
do\_stop  
case “$?” in  
0|1) [“$VERBOSE” != no] && log\_end\_msg 0 ;;  
2) [“$VERBOSE” != no] && log\_end\_msg 1 ;;  
esac  
;;  
restart|force-reload)  
log\_daemon\_msg “Restarting $DESC”  
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

# 

— cut here —

---

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [November 6, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/20081106-script-to-spawn-php5-cgi-enabling-nginx-to-serve-php-code-pages/152688/2 "2008-11-06T13:58:30Z")

</div>

Mark A. wrote:

> The php5-cgi (until 5.2.4) package does not include a script to  
> automatically start it in external FASTCGI Mode (Daemon mode).

> — cut here —  
> #! /bin/sh

Note that /bin/sh in all Linux’es is symlinked to bash while /bin/ash is  
what you really want to use here. It’s stripped down to include only  
‘sh’ support layer.

It’s also a good practice when she-banging sh-scripts to add a dash  
after handler so no one can pass any arguments to your script:

#!/bin/ash -

---

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [November 6, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/20081106-script-to-spawn-php5-cgi-enabling-nginx-to-serve-php-code-pages/152688/3 "2008-11-06T15:27:14Z")

</div>

Volodymyr K. wrote:

> Note that /bin/sh in all Linux’es is symlinked to bash

Not always. In Ubuntu (and all our servers work over Ubuntu) it is a  
link to some form of ash:  
ls -lia /bin/sh # shows: /bin/sh -\> /bin/dash

> while /bin/ash is what you really want to use here. It’s stripped down to include only  
> ‘sh’ support layer.

I agree.

> It’s also a good practice when she-banging sh-scripts to add a dash  
> after handler so no one can pass any arguments to your script:  
> #!/bin/ash -

That is nice.

Regards,

M.
