0

I am trying to deploy multiple Flask applications in a AWS EC2 without success :(.

I'm following this tutorial, the steps seems to be simple enough, however I haven't managed to make it work. This is my configuration:

  • Instance type: t2.micro

  • OS: Ubuntu, 22.04 LTS, 64-bit

  • Directory structure:

    Directory structure

  • Ngingx, Gunicorn and Flask have been all installed globally in the instance.

  • app_one gunicorn service configuration:

[Unit]
Description=Gunicorn instance for app_one
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/app_one
ExecStart=/usr/bin/gunicorn3 --workers 2 --bind unix:flaskapp.sock -m 007 app:app
Restart=always
[Install]
WantedBy=multi-user.target
  • app_two gunicorn service configuration:
[Unit]
Description=Gunicorn instance for app_two
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/app_two
ExecStart=/usr/bin/gunicorn3 --workers 2 --bind unix:flaskapp.sock -m 007 app:app
Restart=always
[Install]
WantedBy=multi-user.target
  • nginx default enabled file deleted and replaced with this "flaskapp" file:
server{
    listen 80;
    server_name 3.89.19.163;

    location / {
        proxy_pass http://unix:/home/ubuntu/app_one/flaskapp.sock;
        error_log  /var/log/nginx/error.3.89.19.163;
    }
}

server{
    listen 8080;
    server_name 3.89.19.163;

    location / {
        proxy_pass http://unix:/home/ubuntu/app_two/flaskapp.sock;
        error_log  /var/log/nginx/error.3.89.19.163;
    }
}
  • nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
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;
        error_log /var/log/nginx/error.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/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}
  • and last /var/log/nginx/error.log
2023/10/02 09:17:31 [notice] 2788#2788: using inherited sockets from "6;7;"

As far as I know, this error file shows an update and since is a "notice" I have nothing to worry about, however, looks like there is something odd in my configurations file because when I ran curl localhost:80 or curl localhost:8080 I get a "502 Bad Gateway"

and when I visit my publicIP address (publicIP, publicIP:80, publicIP:8080) this is what I get:

ERR_CONNECTION_TIMED_OUT

4
  • AWS incorporates a default firewall on their instances preventing public IP access. Adjust the security profiles in the AWS console for your machines. If they need to talk to each other and don't have private networking then you need to make sure those ports are open to each individual AWS instance in their AWS console firewall.
    – Thomas Ward
    Commented Oct 2, 2023 at 11:18
  • Hey @ThomasWard, I'm assuming you're talking about my instance security group? In that case, yes I have explicitly open the ports 80, 8000 and 8080 (source 0.0.0.0/0 which I understand is "everyone") and pointed the inbound traffic (port 22) directly to my IP address. If is not what you mean, can you give me another hint? Thanks
    – drn_svq
    Commented Oct 2, 2023 at 11:54
  • Nope, that's what I mean. I can toss a scanner at your IP(s) if you want but you'll have to contact me off-site if you want that (via my profile on Launchpad - linked in my profile here). 502 Bad Gateway means that whatever is behind NGINX that you're trying to reach is not alive (check the /var/log/nginx/error.log file and NOT just the last entry).
    – Thomas Ward
    Commented Oct 2, 2023 at 11:57
  • Sure, but the problem is that that's the ONLY entry on my error.log file :s
    – drn_svq
    Commented Oct 2, 2023 at 11:59

0

You must log in to answer this question.

Browse other questions tagged .