skip to Main Content

I am trying to get my laravel app to work. It works locally, I deployed the app with BitBucket pipeline, I can php artisan serve (shows it starts the server), but when reaching the server, I get Bad Gateway 502, what am I doing wrong?

I run on Debian 12

nginx error.log is showing the following message:

2024/03/22 15:40:43 [error] 249350#249350: *3 directory index of "/var/www/html/laravel/current/" is forbidden, client: 80..., server: <server_name>, request: "GET / HTTP/1.1", host: "85...:8000"

This is my nginx.conf:

pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        include /etc/nginx/sites-available/*;
}

This is my conf file:
/etc/nginx/sites-available/filename.conf

server {
    listen 80;
    server_name server_domain_or_IP;
    root /var/www/html/laravel/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

running the server does not do anything either:

enter image description here

but also trying to add the port does not do anything

2

Answers


  1. Chosen as BEST ANSWER

    tried all, but unfortunately to no avail. I think my initial nginx set-up was faulty, although i could not figure out why this was. I eventually ended up freshly installing nginx which went well. Now other issues this helped me: https://askubuntu.com/questions/361902/how-to-install-nginx-after-removed-it-manually

    then followed the previous steps and it started working


  2. You are doing it very bad. php artisan serve is a server for local development, not for production.

    I will asume that you have nginx installed, and that you already configured .env file, databases and all other things for production. If not, please follow this tutorial

    First of all, you need to copy (or link) your project directory to /var/www

    • # cp /path/toyour/project-directory /var/www/yourproject -r. This command will copy your project. Also you can use mv for move or ln -s for link.

    • # vim /etc/nginx/sites-available/yourproject

      listen 80;
      listen  [::]:80;
      server_name server_domain_or_IP;
      root /var/www/yourproject/public;
      
      add_header X-Frame-Options "SAMEORIGIN";
      add_header X-XSS-Protection "1; mode=block";
      add_header X-Content-Type-Options "nosniff";
      
      index index.html index.htm index.php;
      
      charset utf-8;
      
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
      
      location = /favicon.ico { access_log off; log_not_found off; }
      location = /robots.txt  { access_log off; log_not_found off; }
      
      error_page 404 /index.php;
      
      location ~ .php$ {
          # Please change your PHP version based on your setup.
          fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          include fastcgi_params;
      }
      
      location ~ /.(?!well-known).* {
          deny all;
      }
      } # Dont forget save it.```
      
      
      
    • # ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled/yourproject

    With # nginx -t you can test your configuration. If is ok, you can reload service with # nginx -s reload

    Notes

    • Set your own PHP version
    • Set your own domain name
    • # in the commands, indicates that you need do it as root.

    After this configuration, you need set up the permissions:

    • # chown -R www-data:www-data /var/www/yourproject/storage
    • # chown -R www-data:www-data /var/www/yourproject/bootstrap/cache

    I tested the configuration and i having a error:
    I can see the error with tail -f /var/log/nginx/error.log

    [crit] 46660#46660: *3 connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: example.localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "example.localhost"
    

    The solution was: # apt install php8.1-fpm -y && service nginx restart. Tested on Ubuntu 22.04 with PHP 8.1.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search