Nginx + PHP-fastcgi + MySQL on CentOS: 502 Bad Gateway in all files with MySQL connection


I am trying to install an Nginx server with PHP and MySQL on my VPS running CentOS.

I followed this tutorial to install all the sofware.

he server is running, the virtual hosts are working, but when I try to connect to MySQL in a PHP file the server returns 502 Bad Gateway.

I am a newbie in Linus, so I don't exactly know where to find all the logs etc.

I have found this error in the virtual host log, but I don't know what it means.

2012/04/15 23:07:33 [error] 22360#0: *3 connect() failed (111:
Connection refused) while connecting to upstream, client:, server: @host, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://", host: "@host"

(where @host is my server)

Best Answer

Looks like PHP won't able to connect to MySQL dispute which http server you use and the way you ling php to http server.

First, check if your MySQL is running and listen at all (you may forget to run it). Simply run mysql from CLI, and see if it connects. You may need to use -h host key to point mysql client to right host.

Then, try to run the simplest script (like

$link = mysql_connect('hostname', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

so you'll see what's the error. If your MySQL listen on localhost (, then use 'localhost' as hostname ('hostname' in the example), if it listen in socket, then use socket path (something like ':/tmp/mysql').

Good luck!