How to Install Laravel with an Nginx Web Server on Ubuntu 16.04

June 10, 2016 4.1k views
Nginx PHP Frameworks Ubuntu 16.04

I am new user on nginx. I am trying to create a server block for using Laravel 5.2 but I am getting 502 Bad Gateway. Maybe I am doing something wrong. I am using Ubuntu 16.04, PHP 7 and nginx 1.10.0. My config file as bellow:

server {
    listen 80;
    listen [::]:80;

    root /var/www/;
    index index.php index.html index.htm;


    location / {
        try_files $uri $uri/ /index.php?$query_string;

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
1 Answer


To simplify the php-fpm configuration, I'd use TCP connections instead of sockets as permissions on the sock file will result in connections errors (and the majority of guides never mention this - so it's nothing you did).

From the command line:

  • Open /etc/php/7.0/fpm/pool.d/www.conf
  • Search for listen = /etc/php/7.0/fpm/pool.d/www.conf
  • Change it to listen =
  • Restart php-fpm by running service php70-fpm restart

You may need to adjust these paths a bit depending on how you installed PHP, though if you cd /etc/php and run ls -al, you can check whether it's 7 or 7.0 for your installation.

Now, in the same server block you posted:

  • Change fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  • To fastcgi_pass;

Unless you're explicitly using IPv6 to connect and know for a fact that IPv6 is enabled, I'd also remove:

  • listen [::]:80; it will cause NGINX to fail if not enabled, not supported, etc.

Now run:

  • service nginx restart

...and see if you're able to connect. If not, then we need to take a look at the error logs to see if there's something more explicit being logged to detail what's happening.

By default, the error log is located at /var/log/nginx/error.log so we'd run:

  • tail -50 /var/log/nginx/error.log

Which will output the last 50 lines of the log (or all lines if there's less than 50). If you would copy and paste that here, I'll be more than happy to take a look at it for you. If you'd like, feel free to change instances of your domain or IP to filler data.

Have another answer? Share your knowledge.