Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have experience with nginx but it's always been pre-installed for me (via VPS.net pre-configured image). I really like what it does for me, and now I'm trying to install it on my own server with apt-get. This is a fairly fresh Debian 5 install. I have few extra packages installed but they're all .deb's, no manual compiling or anything crazy going on.

Apache is already installed but I disabled it. I did apt-get install nginx and that worked fine. Changed the config around a bit for my needs, although the same problem I'm about to describe happens even with the default config.

It took me a while to figure out that the default debian package for nginx doesn't spawn fastcgi processes automatically. That's pretty lame, but I figured out how to do that with this script, which I found posted on many different web sites:


## ABSOLUTE path to the PHP binary

## tcp-port to bind on

## IP to bind on

## number of PHP children to spawn

## number of request before php-process will be restarted

# allowed environment variables sperated by spaces

## if this script is run as root switch to the following user

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then


if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"

echo $EX

# copy the allowed environment variables

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"

# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &

When I do a "ps -A | grep php5-cgi", I see the 10 processes running, that should be ready to listen.

But when I try to view a web page via nginx, I just get a 502 bad gateway error.

After futzing around a bit, I tried telneting to 9000 (fastcgi is listening on port 9000, and nginx is configured to talk to that port), but it just immediately closes the connection.

This makes me think the problem is with fastcgi, but I'm not sure what I can do to test it. It may just be closing the connection because it's not getting fed any data to process, but it closes immediately so that makes me think otherwise.

So... any advice? I can't figure it out. It doesn't help that it's 1AM, but I'm going crazy here!

share|improve this question
does netcat -nap | grep LISTEN | grep 9000 show the listening socket? w/ php-fcgi php handles the error handling, but when my home-grown fcgi scripts fail to interact w/ fcgi correctly, you can get a 502 error as well. –  Jason Mar 12 '10 at 13:25
When I do that it says: all-A-records NIY –  Sean Mar 12 '10 at 18:19
You might be interested in using php-fpm sapi. I just had the very symptoms and it turned out that my memcached extension was incompatible with libmemcached 0.39. Only place this showed was in the php-fpm logs! –  Martin Fjordvald Apr 27 '10 at 11:44
add comment

2 Answers

On my server I use nginx+fcgi as well.

My solution isn't foolproof but at least works. I have this script which uses spawn-fcgi and php5-cgi under /etc/init.d/

PHP_SCRIPT='/usr/bin/spawn-fcgi -a -p 9000 -u www-data -f /usr/bin/php5-cgi'
case "$1" in
      killall -9 php5-cgi
      killall -9 php5-cgi
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
exit $RETVAL

and the related nginx conf is this:

server {
        location ~ .php$ {
                        fastcgi_index index.php;
                        include /etc/nginx/fastcgi.conf;
                        fastcgi_param SCRIPT_FILENAME /var/www/hyperblasted/$fastcgi_script_name;

and the fastcgi.conf contains the following

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

Hope this helps :)

PS: With this setup I had an issue where the cgi daemon would die every now and then. I worked around this issue by executing this in a cronjob every 5 minutes:

if ps aux | grep 'php5-cgi' | grep -v grep  > /dev/null ; then
        echo "PHP-cgi is runnning !"    
        echo "PHP-cgi is down. Starting over..."
        /etc/init.d/php-fcgi start
share|improve this answer
That's the thing though, when I installed nginx with apt-get, there is no spawn-fcgi binary file. That's why I had to figure out on my own how to spawn the PHP CGI listeners, And they are running... they're just not processing anything. –  Sean Mar 12 '10 at 11:19
apt-get install spawn-fcgi. It works on sid. –  Eduardo Ivanec May 16 '11 at 17:41
Do you know why it dies after few requests? I can't seem to figure it out. :( –  Richards Nov 2 '11 at 20:20
add comment

I'm not an expert on this at all. In fact I saw your post because I had the same problem. HOwever, I seem to have been able to get it to work and what I did was change the USERID (www-data) from the /etc/init.d/php-fastcgi file to the user I was currently running. Sorry if this doesn't help or that I can't explain any further, but I thought I'd post it in case it is relevant.

share|improve this answer
Do you mean the actual owner of the file or the value of the variable USERID as set in the file? –  Adrian Lang Nov 22 '12 at 12:18
add comment

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.