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/kneelam.dev/public;
index index.php index.html index.htm;
server_name kneelam.dev www.kneelam.dev;
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;
}
}
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.
@sayful
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:
/etc/php/7.0/fpm/pool.d/www.conf
listen = /etc/php/7.0/fpm/pool.d/www.conf
listen = 127.0.0.1:9000
php-fpm
by runningservice 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 runls -al
, you can check whether it’s 7 or 7.0 for your installation.Now, in the same server block you posted:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
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.
@sayful
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:
/etc/php/7.0/fpm/pool.d/www.conf
listen = /etc/php/7.0/fpm/pool.d/www.conf
listen = 127.0.0.1:9000
php-fpm
by runningservice 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 runls -al
, you can check whether it’s 7 or 7.0 for your installation.Now, in the same server block you posted:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
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 :)