Deploying Advanced Template to AWS Elastic Beanstalk

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.

My mistake, I just redid method 1 and it indeed redirects to domain.com/backend/site/login without the web.

I miswrote in my previous post.

I left the config changes from method 2 in place, but they make no difference.

Please let me know what to check/try next.

It is the expected behavior that the first backend access is redirected to “backend/site/login” because the backend index page requires a valid user login. Did you register some user in the frontend? Try to register some user in the frontend and login with it, then you will be able to access the backend index page.

When you take the 1st method, there should be no need to do extra nginx configurations for the backend. You should be able to think that there’s only frontend, no backend at all.

Hmm, but should it not at least take me to the login page for the backend? Instead it gives me a 404 error.

I’ll try this in a bit (traveling) and need to finish setting up the site or disabling some things to log in (Capthas and stuff).

What do you mean by “it”? Is it a valid URL like "http://domain.com/backend" or an invalid URL like "http://domain.com/backend/web"? It’s very natural that an invalid URL ends in a 404 error.

And just as I suggested in another thread, take a look at kartik-v’s Yii2 Practical-A-App and probably you will like it.

My apologies for not being clear. The url shows domain.com/backend/site/login however, what is actually displayed is a blank 404 Not Found page (nginx/1.20.0)

This is for apache. I actually had no problem getting this setup in apache on my local machine, but I’d like to run nginx for full deployment.

I see. So what do you see in nginx’s error log about it? Or, application logs in “runtime” folder may have some information.

Oh is it? I don’t believe it and will check it.

[EDIT]
Yii2 Parctical-A-App works with nginx.

[EDIT 2]
Oh, I’m sorry. The document above doesn’t tell what to do for a single domain. Hmm…

Alright, just pulled the nginx error.log. This is what it shows:

2021/09/07 04:01:02 [error] 5643#5643: *98 open() "/var/www/html/frontend/web/backend/site/login" failed (2: No such file or directory), client: 45.17.74.222, server: , request: "GET /backend/site/login HTTP/1.1", host: "rof-env.eba-w4zn6ugp.us-east-2.elasticbeanstalk.com"

As you can see, it’s simply appending backend/site/login. This is using method 1 with a simple symbolic link. I had noticed this before and is why I went with method 2 (and others like it).

Still, no luck.

Did you enable the pretty url? Try with it disabled.

And btw, I even tried an absolute symbolic link with the command below

ln -s /var/www/html/backend/web backend

Got the exact same behavior and the exact same error response.

Seems to be that nginx will not go outside the root directory… and I don’t know why

Give me a sec…

Voila! Disabling PrettyUrl and I am able to access the backend now.

Sigh… I thought about disabling pretty url a while ago, but I didn’t think it would be the source of my problem. Figured it had to be something I was doing wrong.

Now the question is, is there something that needs to happen with prettyUrl? I’d really like to use it if possible.

OK, I think I got a solution for enabling PrettyUrl.

Try adding these lines in the nginx configuration.

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

instead of the existing line:

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

Url manager configurations can be simple enough:

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],

This should work for both frontend and backend.
I didn’t have to set baseUrl or scriptUrl for the backend.

THIS WORKED! THANK YOU SO MUCH!

I have spent more than a week on this problem. Where should I create a resource for this in case others have the same issue?

I think you can revise the wiki.

1 Like

Wiki has been updated! Thanks again!

1 Like

hey last question (I hope),

How would I direct a subdomain to the backend?

Nevermind, I figured it out. Needed to create another “server” configuration for the subdomain. Ironically, this is actually easier than what I was trying to do up above.

1 Like