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)

  • I am a step further, I installed php53 from the IUS repo. Now it says Error connecting to mysql – Qurben Apr 16 '12 at 10:27
  • Install everything from EPEL repo – GioMac Aug 16 '13 at 23:50
  • looks like mysql id down. – GioMac Aug 16 '13 at 23:50
  • ps auxwf|grep mysql;netstat -anp|grep mysql – GioMac Aug 16 '13 at 23:51

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!


Is there anything running on port 9000? Check the output of netstat -anp | grep :9000. It looks like PHP is either not running (or firewalled).

Is PHP itself actually running? Check the output of ps aux | grep php. If not, you should check over what you did to enable it as a service again.

  • Yes, php-cgi is listening on port 9000 tcp 0 0* LISTEN 2015/php-cgi and php-cgi is running. Serving php files is not a problem, phpinfo() is working and I have uploaded code igniter it works too. But when I connect to the database I get an error – Qurben Apr 16 '12 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.