Monday, September 23, 2013

Solving “502 Bad Gateway” with nginx & php-fpm

After upgrading php-fpm, my PHP-based sites were returning “502 Bad Gateway” errors. Here’s how I managed to solve it.

Check to make sure that php-fpm is running with
ps auxww | grep php
– if you can’t see any php-fpm processes in the output, then you may need to re-install php-fpm. If php-fpm is running okay, then skip this first step.

sudo apt-get remove php5 php5-cgi php5-fpm

sudo apt-get install php5 php5-cgi php5-fpm


The thing to notice here is that the order in which you install the packages is important. In the past I have found that installing them in the wrong order causes the packages to be configured incorrectly.

Next, get php-fpm to listen on the correct host/port. In /etc/php5/fpm/pool.d/www.conf change the following line from:

listen = /var/run/php5-fpm.sock


To:

listen = 127.0.0.1:9000


Restart php-fpm with
sudo /etc/init.d/php5-fpm restart
and everything should work normally again.

server {
        listen 80;
        server_name your_domain.com;
        root /your_document_root;
        index index.php;
        access_log  /your_document_root/logs/access_log;
        error_log  /your_document_root/logs/error_log;



        location / {
                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php last;
                        break;
                 }
        }

    location ~ \.php$ {
        include fastcgi_params;
        try_files $uri =404;
        fastcgi_pass  127.0.0.1:9000;
        # fastcgi_pass  unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /your_document_root$fastcgi_script_name;
        fastcgi_index  index.php;
    }
}



Ref: http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm

No comments :

Post a Comment