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...

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...
- Steps Required To Get Rid of The "502 Bad Gateway Error"
- Other Error Fixes
Interactive Builder for this Guide
Build your custom nginx syntax (PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory))
server {
listen 80;
listen [::]:80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
gzip on;
gzip_types text/plain text/css application/json application/javascript application/xml;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}server_nameBinds this virtual host to app.example.comreturn 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPSproxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application serverX-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjackingI 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
- Error Establishing a Database Connection in WordPress Fix
- 5 Simple Steps to Fix 504 Gateway Timeout Nginx Error
- Your PHP installation appears to be missing the MySQL extension which is required by WordPress
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.

UNIX and Linux System Administration Handbook, 5th Edition
Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
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.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Zip a Folder in Ubuntu Linux / Debian Linux
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides











Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!