How to solve the 502 bad gateway issue with NGINX and PHP5

502 Bad Gateway issue on NGINX serverSo you installed locally or in your remote web server Nginx following this guide for the first configuration and then you added a new domain with the virtual host file. But then when you try accessing on your domain NGINX is not happy and tells you “502 Bad Gateway”.
Let’s see how to solve this problem.

Is PHP running properly?

First of all we need to make sure the problem is not in the site configuration in the virtual host file. To do so we can create a plain index.html file in the webroot.
If we can see this file correctly from the browser we can do with the next step, otherwise I suggest to check how to set up a virtual host file.

With the command:

ps auxww | grep php

we can check if there is any php process running.
The response of the command in the terminal should look like:

root 5232 0.0 0.2 371972 8368 ? Ss 12:52 0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 5233 0.0 0.2 371972 8000 ? S 12:52 0:00 php-fpm: pool www
www-data 5234 0.0 0.1 371972 7180 ? S 12:52 0:00 php-fpm: pool www
mimo 5314 0.0 0.0 13636 956 pts/0 S+ 13:03 0:00 grep --color=auto php

If the result doesn’t contains any “php-fpm” process maybe something has gone wrong in the first installation process, so we have to reinstall the packages for php5. (You can skip if in your terminal the results are like the above).

sudo aptitude install php-pear php5-cli php5-common php5-curl php5-dev php5-fpm 
php5-gd php5-imagick php5-imap php5-intl php5-mcrypt php5-memcache 
php5-ming php5-mysql php5-mysqlnd php5-ps php5-pspell php5-recode php5-snmp php5-sqlite 
php5-tidy php5-xmlrpc php5-xsl

Set up PHP-FPM to communicate correctly with NGINX

The php-fpm process has to listen the correct thing to run properly and to execute the code passed by NGINX. To edit the configuration of php-fpm we need to change a line on the file: /etc/php5/fpm/pool.d/www.conf.

sudo nano /etc/php5/fpm/pool.d/www.conf

and then look for

listen = /var/run/php5-fpm.sock

(around line 34 on my computer)
and change it to:

listen =

Just one step to go, restart php-fpm:

sudo /etc/init.d/php-fpm restart

and everything now is good to go.

Lascia un Commento!