Website Vidya
Linux & ServersStep-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
Rajesh RayLead 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.

Advertisement

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

Advertisement
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:

Advertisement
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
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

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

Linux & Servers•

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
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout