9

I just spun up a new Rackspace cloud server and ran the following:

apt-get install php5 nginx php-fpm

When I navigate to the IP address, I see the default Nginx message. I then copied my /etc/nginx/sites-available/default file from another working setup I have:

server {

  listen 80;
  server_name localhost;

  root /srv/www;
  index index.php;

  location / {
    try_files $uri $uri/ /controllers$uri.php;
    location ~ \.php$ {
      try_files $uri /index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
      }
   }
}

I created index.php in /srv/www and started nginx and php-fpm:

/etc/init.d/nginx start
/etc/init.d/php5-fpm start

I'm getting a 502 Bad Gateway error. Am I doing something wrong?

David
  • 845
  • 3
  • 8
  • 10

4 Answers4

7

Since I can't add comment, I'll post it as an answer...

Check /etc/php5/fpm/pool.d/www.conf (or the file in there) if it's set to run on TCP or UNIX socket. Also check if the PHP is actually running. And look into the log files /var/log/php5-fpm.log or /var/log/php5-fpm/* and /var/log/nginx/* (depends on settings).

You should find the cause of the error there.

danaketh
  • 86
  • 3
  • 1
    I opened `/etc/php5/fpm/pool.d/www.conf` and found `listen = /var/run/php5-fpm.sock`. I changed it to `listen = 127.0.0.1:9000` and everything is working now. I totally forgot about that one-line configuration! Thanks! – David Dec 13 '12 at 21:04
5

In my case the solutions was:

1- To Change the lister like @David:

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

On the file search listen =, comment ...sock and add 127.0.0.1:9000

;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

2- Change on Nginx Site conf (Ex. /etc/nginx/sites-available/mysite.com)

server {
       .
       .
       . 
location ~ \.php$ {
       .
       .
       .
 #fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
 fastcgi_pass 127.0.0.1:9000;
       .
       .
       .

3- Increment the timeout and max_children:

/etc/php/7.0/fpm/php.ini  =>   default_socket_timeout = 60000
/etc/php/7.0/fpm/php.ini  =>   pm.max_children = 20
/etc/php/7.0/fpm/pool.d/www.conf  =>   request_terminate_timeout = 60000

4- Incremente timeout on /etc/nginx/nginx.conf:

keepalive_timeout 65000;

After Restart php-fpm and nginx:

sudo service php7.0-fpm restart
sudo service nginx restart

I recommend before restart nginx to test if all is OK:

sudo service nginx configtest 
1

In my case restart for fpm service worked. I only ran reload. Forgot that conf changes needs restart to apply.

mFlorin
  • 111
  • 2
1

The bad gateway error means that the php process is responding with an error. Can you check your php error log? Also try placing a file test.php with <?php phpinfo(); ?> @ /srv/www/ and then try to hit that domain directly from the command line of your server ala:

curl http://127.0.0.1:9000/test.php;

Have you checked the obvious things like you have a file @ /srv/www/index.php? Have you run ps aux and grepped for nginx and php to make sure they are running?

How about running: netstat -an | grep LISTEN to make sure the php-fpm daemon is listening?

runamok
  • 163
  • 10
  • I ran `netstat -an | grep LISTEN` and saw that nothing was listening on 127.0.0.1:9000. I opened `/etc/php5/fpm/pool.d/www.conf` and found `listen = /var/run/php5-fpm.sock`. I changed it to `listen = 127.0.0.1:9000` and everything is working now. I thought this was the default setting and I forgot to check it. Thanks! – David Dec 13 '12 at 21:02