Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk + Laravel, Nginx Configuration

Recently AWS started distributing the Elastic Beanstalk PHP environment with Amazon Linux 2, which has dropped apache in favor of Nginx, I've been trying to correctly configure my Laravel project to work, I used to just have to add some .htaccess configuration and that was it, on Nginx I can't seem to figure out how to make my app to work, my first issue was the reverse proxy port, which I fixed by setting PORT environment variable to 80, but when I try to access any route from the URL aside from /, it gives me an 404 Not Found error.

so I tried to add a .ebextension/Nginx/nginx.conf to my project containing the following:

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33282;

events {
    worker_connections  1024;
}

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

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  include       conf.d/*.conf;

  map $http_upgrade $connection_upgrade {
      default     "upgrade";
  }

  server {
      listen 80 default_server;
      root /var/app/current/public;

      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-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
      }

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

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

      client_header_timeout 60;
      client_body_timeout   60;
      keepalive_timeout     60;
      gzip                  off;
      gzip_comp_level       4;

      # Include the Elastic Beanstalk generated locations
      include conf.d/elasticbeanstalk/01_static.conf;
      include conf.d/elasticbeanstalk/healthd.conf;
  }
}

but it didn't work, I tried to check if the configs ware applied on the instance, but /etc/Nginx/Nginx.conf did not change.

How can I configure an Elastic Beanstalk PHP Amazon Linux 2 through .ebextensions to make Nginx work with a stateless Laravel application?

Thank you!

like image 988
Ollegn Avatar asked May 11 '20 16:05

Ollegn


2 Answers

I manually added a file to /etc/nginx/conf.d/elasticbeanstalk/ and named it laravel.conf. The laravel.conf file contained:

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

Full solution:

https://forums.aws.amazon.com/thread.jspa?threadID=321787&tstart=0

like image 153
Matt Byrnes Avatar answered Oct 19 '22 20:10

Matt Byrnes


The correct way would be to add this file inside

.platform/nginx/conf.d/elasticbeanstalk/laravel.conf

Here is a link to docs:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

like image 12
lex064 Avatar answered Oct 19 '22 19:10

lex064