How to Install Laravel with an Nginx Web Server on 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;

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


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;

…as 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.

Well! there is a simple solution for this. Launch your DigitalOcean server with Cloudways. The complete Nginx stack will be built in few clicks along with the Laravel application :)