502 Bad gateway when following LEMP install on new droplet

August 8, 2016 1.7k views
PHP LEMP Ubuntu 16.04

On a new droplet I have been working through the tutorial "How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04" and am stuck with the php test failing with a 502 bad gateway. I have gone thru several times and can find no errors.
Can anyone point me in the right direction?

3 comments
  • Hi!

    In order to be able to better help you, we'll need a bit more info first. Are there any errors in Nginx's error logs (php errors should be forwarded to there too)?

    sudo tail -30 /var/log/nginx/error.log
    

    Though, to be honest, there's not much that could go wrong with the basic info php file. Make sure that the function is spelled correctly without any underscores or hyphens: phpinfo();.

  • http://bell-computing.com/info.php generates 501.
    Here is the content of the sudo nano /usr/share/nginx/html/info.php
    <?php
    phpinfo();
    ?>

    and here is the output from sudo tail...
    2016/08/08 17:58:47 [emerg] 16367#16367: directive "servername" is not terminated by ";" in /etc/nginx/sites-enabled/default:43
    2016/08/08 17:59:41 [emerg] 16371#16371: "fastcgi
    pass" directive is duplicate in /etc/nginx/sites-enabled/default:60
    2016/08/08 18:00:53 [emerg] 16375#16375: unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:90
    2016/08/08 18:01:42 [emerg] 16379#16379: unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:90
    2016/08/08 18:03:11 [notice] 16407#16407: signal process started
    2016/08/08 18:07:36 [crit] 16410#16410: *9 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167.$
    2016/08/08 18:12:29 [crit] 16410#16410: *11 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$
    2016/08/08 18:19:52 [crit] 16410#16410: *16 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$
    2016/08/08 18:58:28 [crit] 16410#16410: *30 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$

  • The file contents are correct but bell-computing.com/info.php generates the 501. Its in my document root /usr/share/nginx/html/info.php.

    2016/08/08 18:03:11 [notice] 16407#16407: signal process started
    2016/08/08 18:07:36 [crit] 16410#16410: *9 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167.$
    2016/08/08 18:12:29 [crit] 16410#16410: *11 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$
    2016/08/08 18:19:52 [crit] 16410#16410: *16 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$
    2016/08/08 18:58:28 [crit] 16410#16410: *30 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$
    2016/08/08 20:54:21 [crit] 16410#16410: *44 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 80.14.167$

1 Answer

Edit the file /etc/php/7.0/fpm/pool.d/www.conf

And uncomment or add the following lines.

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

If you are running Nginx as a different user, change www-data to that user. By default it will run as www-data.

You may have to delete the file /var/run/php/php7.0-fpm.sock if it exists.

Now run the commands below.

sudo service nginx restart
sudo service php7.0-fpm restart
  • I did try that havig read it in another blog but still no luck. eleted the droplet and started again and this time it worked (at least to get phpinfo). Os I musst have c@cked up.

Have another answer? Share your knowledge.