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

After upgrading php-fpm, my PHP-based sites were returning “502 Bad Gateway” errors. This can happen when the php5-fpm package reconfigures itself to listen on a different socket. Here’s how you can solve it.

Check to make sure that php-fpm is running with ps aux | 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 listen value to match the fastcgi_pass location in your Nginx configuration. For example, I changed mine from:

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


listen =

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions. While I wouldn’t recommend it, you can simply give read-write permissions to all with sudo chmod go+rw /var/run/php5-fpm.sock.

Restart php-fpm with sudo service php5-fpm restart and everything should work normally again.

Did you find this post useful?

  • Gomathinayagam S

    I migrated my ubuntu from 13.04 to all the way down to 14.04 and the fastcgi sock broken. I had to change nginx config and php conf to use localhost:9000 as per author instruction. Worked. Thanks.

  • Alexander

    Thanks! It’s works!

  • ?????? ?????

    Sometimes it is simply need to check the owner of /var/run/php5-fpm.sock. It may cause the 502 if its owner is root

    • Antonieta Zacarias

      Thanks so much!!! You saved me today :)

  • Rafael A’ig

    Thanks so much for sharing your solution. Worked great!

  • Lukman abdul faqih

    i following your article after i changed the config on /etc/php5/fpm/pool.d/www.conf on debian weeezy after that i restarted my fpm service and nginx wow my script phpinfo is running rite now before that i got 502 Bad Gateway

    502 Bad Gateway

    502 Bad Gateway

  • vsync

    this line –

    listen =

    had caused me 502 by itself!

  • rajkumar

    Thanks for the information.This helped me

  • Ray

    Unfortunately, this is not working for me?

    Here is my information. I hope someone can spot something wrong!

    I just installed a fresh copy of Ubuntu 12.04.3 and updated it (on a local VM).

    I installed nginx sucessfully.
    ray@Web-Server:/etc/nginx/sites-available$ nginx -v
    nginx version: nginx/1.4.5

    I installed php5-cli and php5-fpm.
    ray@Web-Server:/etc/nginx/sites-available$ php -v
    PHP 5.3.10-1ubuntu3.10 with Suhosin-Patch (cli) (built: Feb 28 2014 23:14:25)
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

    I added the info.php file to /usr/share/nginx/html/info.php

    I did the following:

    cd /etc/nginx
    sudo rm sites-enabled/default
    sudo cp sites-available/default sites-available/my-default
    sudo ln -s /etc/nginx/sites-available/my-default sites-enabled/default

    my-default looks like this:

    server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules

    location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    # fastcgi_pass;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    Everything else is commented out in the file.

    So I was successful in browsing to the default nginx but info.php is taking the 502 Bad Gateway error?

    Some of my “ps -A” output is:

    906 tty4 00:00:00 getty
    913 tty5 00:00:00 getty
    920 tty2 00:00:00 getty
    921 tty3 00:00:00 getty
    923 tty6 00:00:00 getty
    936 ? 00:00:00 acpid
    937 ? 00:00:00 cron
    938 ? 00:00:00 atd
    948 ? 00:00:00 whoopsie
    959 ? 00:00:00 php5-fpm
    960 ? 00:00:00 php5-fpm
    961 ? 00:00:00 php5-fpm
    962 ? 00:00:00 php5-fpm
    963 ? 00:00:00 php5-fpm
    993 tty1 00:00:00 getty
    998 ? 00:00:00 sshd
    1139 ? 00:00:00 sshd
    1140 pts/0 00:00:00 bash
    1664 ? 00:00:00 nginx
    1665 ? 00:00:00 nginx
    1666 ? 00:00:00 nginx
    1667 ? 00:00:00 nginx
    1668 ? 00:00:00 nginx

    /var/log/nginx/error.log =

    2014/03/08 14:07:02 [crit] 1668#0: *29
    connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or
    directory) while connecting to upstream, client:, server:
    localhost, request: “GET /info.php HTTP/1.1″, upstream:
    “fastcgi://unix:/var/run/php5-fpm.sock:”, host: “″

    From the Web Server, I can ping just fine.

    I also tried uncommenting the # fastcgi_pass; but then nginx would not even start up!!




    • wildlyinaccurate

      You can find out exactly where php-fpm is listening by running `grep ‘listen =’ /etc/php5/fpm/pool.d/www.conf`. Once you figure out the listen address, just change the value of `fastcgi_pass` in your nginx configuration. Keep in mind that you can only have one `fastcgi_pass` directive, so when you uncomment one, make sure the other is commented out or removed.

      And remember to restart nginx when you’ve made any configuration changes!

  • Thomas

    Saved my day! :)

  • rajkumar

    solved, Thanks a lot

  • Rolando Quintero F.


  • Héder

    good!! thanks!

  • Hedgehog

    Thanks a lot. This helped me as well.

  • tuannguyen261

    You are save my life! Thanks you. Keep up your work!

  • Nasimuddin Ansari

    It helped me too !

  • aufdenschlips

    Great advice! Thank you!
    Have a good one!

  • Jakson Rochelly

    That’s was really a saving read! Thanks for sharing.

  • Cameron Taylor

    Your a God! – I would never have found this!

  • Hristo Hristov

    Thanks! Saved me a bunch of time. I had to run this in the final step:

    sudo /etc/init.d/php5-fpm restart

  • Paulo Souza

    Thanks a lot!

  • Simon

    Thanx, that’s work !

  • Drew Noakes

    Thank you. In my case, I actually had to do the opposite of what you described. My new install was configured to listen via TCP where nginx was attempting to connect via sockets.

    • Sander Versluys

      Same here! This article and comments helped though! Thanks!

  • Sander

    Wow, thank you! I’m new to Linux and I’ve been looking for a solution to this problem for 2 days now and this works perfect! One thing I’d like to know, what’s the difference between this “.sock” path and the ip:port value?

    • Joseph Wynn

      Hi Sander,

      Unix sockets use the local filesystem to create a connection and perform slightly better because they don’t have the overhead of the TCP/IP stack. Internet sockets (IP:port) are more flexible and portable.

      I don’t think the performance difference is noticeable unless the server is handling very high traffic.