Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have nginx on my server (Ubuntu 11.10). i have created the script below in /etc/init.d/php5cgi to start/stop/restart php5-cgi. the problem is that php5-cgi dies from time to another which makes my website (nginx) giving 502 Bad Gateway (and i dont know why!! it would be highly appreciated if somebody tells us why php5-cgi dies like this).

now, how can i tell my server to keep this process alive forever, in other words to restart it automatically in case it has been stopped (i though in making a shell script and cron job to keep checking, but this means that the website might go down for a minute [from cron to another] and this is not an option for us :( ).

any idea about the reason behind stopping php5cgi very frequently or how to keep it alive forever is highly appreciated.

My /etc/init.d/php5cgi is


case "$1" in
        start-stop-daemon --start \
                --exec $COMMAND -- -a $ADDRESS -p $PORT \
                -u $USER -g $GROUP -f $PHPCGI -P /var/run/$|| true
        echo -n "$NAME started with pid "
        cat /var/run/$
        if [ -e /var/run/$ ]
                $KILL `cat /var/run/$`
                rm /var/run/$
                echo "$NAME stopped"
                echo "$NAME is not running, no pid file in /var/run/"
        $0 stop
        $0 start
      echo "Usage: $0 {start|stop|restart}"
      exit 1
exit $RETVAL
share|improve this question
Are there any log messages from PHP in /var/log/syslog, or some other file in /var/log? – mgorven May 1 '12 at 21:49
If you don't have a specific reason for using php5-cgi, perhaps consider using php5-fpm - it is usually regarded as a superior process manager, and in addition to its other advantages, you might get better stability from it. (It is in the Oneiric repository, so it should be a fairly easy switch actually). – cyberx86 May 2 '12 at 0:28
nothing in the error log :( and my php version is 5.2.10.dfsg.1-2ubuntu6 . there is no fpm package for it, i have tried php5-fpm_5.2.10.dfsg.1-2ubuntu6.4+ppa1_amd64.deb but it didn't work (dpkg: error processing php5-fpm (--install): dependency problems - leaving unconfigured) – Alaa Alomari May 2 '12 at 7:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.