Search code examples
flasknginxamazon-ec2

Why is my Flask app not running at all on any port and any IP?


I have just deployed Flask on my free tier AWS EC2 instance. My main.py basic app runs just fine in the terminal, in the virtual environment that I created.

main.py

 from flask import Flask
 app = Flask(__name__)

 @app.route("/")
 def testprint():
     print('''
       text test
     ''')

 testprint()

 if __name__ == "__main__":
      app.run(host='0.0.0.0', port='5000')

However, I cannot access it via port 5000, neither by using localhost nor my external IP address in the browser.

curl 127.0.0.1:5000
curl: (7) Failed to connect to 127.0.0.1 port 5000 after 0 ms: Couldn't connect to server

My Security Groups inbound rules look like this:

EC2 Inbound Rules

Python and Flask versions:

flask --version
Python 3.9.16
Flask 3.0.3
Werkzeug 3.0.3

Also running nginx version: nginx/1.24.0. There is no ufw on the instance.

Tried opening up port 5000 on the inbound rules.


Solution

  • based on the information provided these are the list of things that i'd make sure of,

    1. ✅ inbound rule to allow port 5000
    2. ⚠️ there's no return statement on the function replace with return
    3. try to hit the api on /, ie, 127.0.0.1:5000/ on your server localhost.
    4. and if that works, find the public ip of the instance to hit from internet
    5. more importantly afterall, make sure the application is running

    btw, how & where have you configured nginx? anyways, you still would be able to hit directly if exposed, make sure nginx doesn't interfere with the port 5000, (ps: it doesn't by default)

    happy to help further :)