Website Vidya
DeploymentStep-by-Step Guide

PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)

I personally had this 502 Bad Gateway Error issue on my website. This is the most frustrating error I have ever experienced while using NGINX with PHP5-FPM. When I went through the log I found a very simple error: 12:40:32 [crit] 4257#0: *5...

RR
Website Vidya TeamLead Systems Educator
Published:
3 min read
PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)
AI Executive SummaryCORE CIS AI

3 min read

I personally had this 502 Bad Gateway Error issue on my website. This is the most frustrating error I have ever experienced while using NGINX with PHP5-FPM. When I went through the log I found a very simple error: 12:40:32 [crit] 4257#0: *5...

Key Highlights
  • Steps Required To Get Rid of The "502 Bad Gateway Error"
  • Other Error Fixes
Grounded in full engineering runbookCORE CIS Ai Engine

I personally had this 502 Bad Gateway Error issue on my website. This is the most frustrating error I have ever experienced while using NGINX with PHP5-FPM. When I went through the log I found a very simple error:

12:40:32 [crit] 4257#0: *538 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 185.130.5.165, server: websitevidya.com, request: "POST /xmlrpc.php HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "x.x.x.x"

The error can easily be presumed through this line:

connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)

Setting up Error Logs

Note: Setting up error logs plays a significant role in identifying server errors. You can check the official documentation of NGINX to configure error logging.

Steps Required To Get Rid of The "502 Bad Gateway Error"

Let’s check out the 2 steps that I needed to get rid of this error.

1. Make sure PHP5-FPM listening According to NGINX Configuration

Open the www.conf file inside the /etc/php5/fpm/pool.d directory and check for any one of the following two options and ensure that your PHP-FPM is listening according to what you have configured in NGINX.

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

or

listen = 127.0.0.1:9000

Also, make sure that below lines are uncommented:

user = www-data
group = www-data

and

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

If you are using TCP/IP on PHP-FPM i.e.,listen = 127.0.0.1:9000 you don’t require Step 2. Check Step 2 if you set listen variable as you see below:

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

2. Make sure file php5-fpm.sock is present inside /var/run/ directory

Check whether the file php5-fpm.sock inside /var/run/ is present or not. The absence of file php5-fpm.sock inside /var/run/ directory can even cause this error.

If we take a close look at the error, we get the idea, either a directory or a file inside the directory is not present.

connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)

This was the actual error that I faced while using listen variable as listen = /var/run/php5-fpm.sock and it took my whole day.

To fix this error message, we just need to reinstall PHP-FPM using the following command:

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

Now, open the www.conf file inside /etc/php5/fpm/pool.d directory and check step 1 for the required configuration.

Now, restart the PHP-FPM & NGINX web server:

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

This will fix the 502 Bad Gateway Error issue. If the error appears again, I would advise you to switch back to use TCP/IP on PHP-FPM until you find a right solution as per the configuration your NGINX and PHP-FPM files have.

To switch back to TCP/IP follow these steps:

Edit the pool file /etc/php5/fpm/pool.d/www.conf and change:

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

To this:

listen = 127.0.0.1:9000

Edit the Nginx virtual host file and change:

        location ~ \.php$ {
            ...
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            ...
        }

To this:

        location ~ \.php$ {
            ...
            fastcgi_pass 127.0.0.1:9000;
            ...
        }

If you wrote the fastcgi_pass on

/etc/nginx/fastcgi_params

file, edit it too.

Finally, restart the PHP-FPM & NGINX web server:

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

If you want to use PHP-FPM with NGINX or it does not solve your error. You can share your file configuration in the comment section below. I will dig into the real cause of the issue to help your website up and be running.

Other Error Fixes

I hope you will find this guide will give you some idea and help you identify the real issue to fix PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)

The above information and some R&D can surely help you to get rid of the issue.

Topics:#php
Written by

Website Vidya Team

Technology educator and researcher at Website Vidya.

Discussion

0
Share your thoughts, ask a question, or suggest a tip...
Write →

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Deployment•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Deployment•

How to Install PHPMyAdmin on Ubuntu: 4 Simple Steps

If you’re working with MySQL or MariaDB on Ubuntu, managing databases purely from the command line can quickly feel overwhelming. That’s where phpMyAdmin comes in. PHPMyAdmin is a web-based application that provides a Graphical User Interfa...

RR
Rajesh Rai
Read More
Deployment•

25 Best Linux Commands Every Sysadmin Should Know

If you’ve spent any time managing Linux servers, you already know this truth: the command line is not optional. It’s where real troubleshooting happens, where performance issues are understood, and where outages are prevented before users e...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Snapd Consuming High CPU / RAM on Linux

If you’ve ever opened your system monitor and noticed snapd quietly eating up your CPU or RAM, you’re not alone. Many Linux users—especially on Ubuntu and Ubuntu-based distributions—eventually run into this issue and wonder what’s going on ...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Broken Packages in Ubuntu: 7 Simple Ways

If you’ve been using Ubuntu for a while, chances are you’ve run into package-related issues at least once. A failed update, an interrupted installation, or conflicting dependencies can easily leave your system in a messy state. When that ha...

RR
Rajesh Rai
Read More