Deploying Advanced Template to AWS Elastic Beanstalk

I’m trying to launch my Yii2-advanced project on AWS’ Elastic Beanstalk stack running an nginx server. I have been unable figure out a configuration that allows me to access the backend of the site. I have tried extending the nginx configuration via the AWS documentation:

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

Which does not work. So I modify the configuration manually via vim from the command line.

While using combinations of the following configuration settings:

To no avail. I have tried alias and root for the backend folder and each variation I get either a 404 (file not found) or 502 (bad gateway) error.

At bare minimum, this is what I’ve tried to add to my nginx configuration:

root /var/www/html/frontend/web;

index index.php index.html index.htm;

location /backend/ {
    root ../../backend/web;
}

What am I doing wrong?

1 Like

Trento insert backend root as Absolute URL

root /var/www/html/backend/web;

Just tried this… Getting a 404 (file not found) error. What else do you suggest I look at or try?

Been at this for 2 days now :frowning:

But /frontend Is working?

Is there index.php file inside backend/web folder?

Yes, and I just tried adding the following line to the configuration under the backend settings and still no avail.

index index.php index.html index.htm;

Error 404 should be simple to catch.

I’ll also try to check nginx log and eventually make nginx in debug mode to check what path nginx is processing (because index.php is not found).

You gave me some stuff to look at! Thanks! I’m hoping this information will lead to a solution. Here are the complete lines I’ve added to my configuration.

location ^~ /backend {
    rewrite ^/backend(.*)$ /backend/web$1 last;
}

location ^~ /backend/web {
    root /var/www/html/backend/web;

    try_files $uri $uri/ /backend/web/index.php$is_args$args;
    location ~ /\.(ht|svn|git) {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
    }
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

But this is what I found in the error log

*1 rewrite or internal redirection cycle while processing “/backend/web/web/web/web/web/web/web/web/web/web/web/”,

*2 rewrite or internal redirection cycle while processing “/backend/web/web/web/web/web/web/web/web/web/web/web/”,

*3 rewrite or internal redirection cycle while processing “/backend/web/web/web/web/web/web/web/web/web/web/web/web/index.php”

So it seems to be that server is a rewrite loop. clearly something is wrong with my configuration. Any advice?

Thanks for getting me this far!

I don’t understand why backend config should be different from frontend.

What are you referring to exactly? What should I change?

Made some changes and now getting bad gateway error.

I am not having any luck here. I have even opened a question on stack overflow

Has no one successfully launched the advanced template on Nginx recently?

I’m currently using PHP 8 and Nginx 1.20.00.

You can see in the stack overflow questions the different configurations I’ve tried. Someone has to know the fix to this.

@samdark Would you be able to help at all?

I have no experience with AWS Beanstalk.

@samdark

I understanding but do you have any guidance on how to configure nginx for the advanced template or can tag someone who does? (The yii2 docs recommendation does not work and the links up above do not work)

Recommendation in docs is for multiple domains. That’s the whole goal of advanced application. But if you want to serve from a single domain:

Thanks but I actually posted this exact same link in my original post up above. As stated, the configuration does not work. I have tried it and many variations of it to no avail.

I have tried other configurations that people have claimed work for the advanced template to no avail.

I have literally copied and pasted these configurations and not changed a single character… Still, they do not work.

I’m beginning to suspect there is a quirk somewhere, perhaps in the latest yii2 release or Nginx that is preventing these configurations from working.

I have been unable to find a recent configuration from the past 12 months.

I am hoping that someone more advanced and capable than I is willing to try these configurations with the latest release of yii2 and see if they can get it to work themselves on Nginx.

I am truly at a loss here.

If I stumble across a solution or figure it out, I will make a new guide, but right now, there doesn’t seem to be a way to do this.

Hi @QuantumPsi,

The wiki shows 2 options:

  • The Easy Way
  • The Hard Way

Which one did you try? It looks to me that the easy way is quite straightforward and very easy to implement. Did you try the hard way?

Yes, I’ve tried both and even combined both methods.

I’ve copied and pasted the code and still no luck.

If you give me some time, I’ll post every configuration I’ve tried and their results.

Yeah, I want to see it.

First off, how did you configure the nginx and what error did you get when you tried “the easy way”?

Alright, I’ll post the other configurations I’ve tried later (traveling at the moment), but here’s what happened when I used the methods outlined here…

Method 1:
Created a link to the backend and restarted the server. The URL is replaced as expected to domain.com/backend/web/site/login, however, what is displayed is a 404 error, page not found. There are no errors in the error log.

And here is the COMPLETE nginx configuration, including the amazon specific files. For the record, I removed the amazon specific files thinking they may have had something to do with the issue, but removing them made no difference.

# configuration file /etc/nginx/nginx.conf:
#Elastic Beanstalk Nginx Configuration File

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

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;
        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;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}
# configuration file /etc/nginx/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

# configuration file /etc/nginx/conf.d/healthd_logformat.conf:
log_format healthd  '$msec"$uri"'
                    '$status"$request_time"$upstream_response_time"'
                    '$http_x_forwarded_for';
# configuration file /etc/nginx/conf.d/php-fpm.conf:
# This file is managed by Elastic Beanstalk
#
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}
# configuration file /etc/nginx/conf.d/elasticbeanstalk/healthd.conf:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
}

access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
# configuration file /etc/nginx/conf.d/elasticbeanstalk/php.conf:
# This file is managed by Elastic Beanstalk
#
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
root /var/www/html/frontend/web;

index index.php index.html index.htm;

try_files $uri $uri/ /index.php$is_args$args;

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}

Now, using method 2. I’ve tried this both with and without the link to the backend in place as described in method 1. And here is the configuration I tried.

#Elastic Beanstalk Nginx Configuration File

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

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;
        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;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;

        set $project_root /var/www/html;
        set $fcgi_server 127.0.0.1:9000;

        charset utf-8;
        client_max_body_size 128M;

        root $project_root/frontend/web;
        index index.php;

        location ^~ /backend {
                rewrite ^/backend(.*)$ /backend/web$1 last;
        }

        location ^~ /backend/web {
                root $project_root;

                try_files $uri $uri/ /backend/web/index.php$is_args$args;

                location ~ /\.(ht|svn|git) {
                        deny all;
                }

                location ~ \.php$ {
                        try_files $uri =404;
                        include fastcgi_params;
                        fastcgi_pass $fcgi_server;
                }
        }

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ /\.(ht|svn|git) {
                deny all;
        }

        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass $fcgi_server;
        }

    }
}

# configuration file /etc/nginx/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

# configuration file /etc/nginx/conf.d/healthd_logformat.conf:
log_format healthd  '$msec"$uri"'
                    '$status"$request_time"$upstream_response_time"'
                    '$http_x_forwarded_for';
# configuration file /etc/nginx/conf.d/php-fpm.conf:
# This file is managed by Elastic Beanstalk
#
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}
# configuration file /etc/nginx/conf.d/elasticbeanstalk/healthd.conf:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
}

access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
# configuration file /etc/nginx/conf.d/elasticbeanstalk/php.conf:
# This file is managed by Elastic Beanstalk
#
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}

# configuration file /etc/nginx/fastcgi_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

These are the config lines in the frontend
frontend\config\main.php

        'request' => [
            'baseUrl' => '',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'scriptUrl' => '/index.php',
            'rules' => [
            ],
        ],

And here is the backend
backend\config\main.php

        'request' => [
            'baseUrl' => '/backend',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'scriptUrl' => '/backend/index.php',
            'rules' => [
            ],
        ],

Using these configuration settings, when going to the backend, I get a 502 error.

I’m going to submit this, my battery is low, so I want to get it submitted. I’ll add other comments if I think of anything later.

And this is what’s in the error log when trying to access the backend:

2021/09/05 15:54:10 [error] 19892#19892: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.58.168.21, server: , request: "GET /backend/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "(servername).elasticbeanstalk.com"

domain.com/backend/web/site/login” ? It’s not the expected URL of the backend login page. It should be “domain.com/backend/site/login”.

This command creates a symbolic link to “backend/web” directory as the name of “backend” in “frontend/web” directory. So, “/var/www/html/frontend/web/backend” is actually “/var/www/html/backend/web”. And because “/var/www/html/frontend/web” is the web root directory, “domain.com/backend” is located in “/var/www/html/frontend/web/backend”, i.e., “/var/www/html/backend/web”.

I guess the chances are that 1st option worked for you in fact but you misunderstood the expected URL of the backend.