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