How to upload a Flask server in 0.0.0.0:80 on a Linux Ubuntu 16.04 with python3 on an AWS EC2?

0

I need to run a Flask server on an EC2 instance.

For this we need to solve these two steps:

  • Open this port on EC2
  • Make Flask run in 0.0.0.0:80
  • The first step is ok. The door is open to interet:

    ForthesecondstepI'mfollowingthis video .

    The Flask server code (according to the video) is as follows:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    
    app.run(host="0.0.0.0", port 80)  
    

    I open a terminal, connect to the EC2 and run the Flask server with the following command (tm according to the video):

    FLASK_APP=flaskserver.py flask run
    

    This is the output:

    * Serving Flask app "flaskserver.py"
     * Environment: production
       WARNING: Do not use the development server in a production environment.
       Use a production WSGI server instead.
     * Debug mode: off
    /home/ubuntu/flaskserver.py:8: Warning: Silently ignoring app.run() because the application is run from the flask command line executable.  Consider putting app.run() behind an if __name__ == "__main__" guard to silence this warning.
      app.run(host = "0.0.0.0", port = 80)
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    

    In the code I have an error message that ignores app.run() and Flask is finally created in http://127.0.0.1:5000/ .

    The video shows that Flask runs at http://0.0.0.0:80/ .

    If I open another terminal and request the url $ curl http://127.0.0.1:5000/ I have the answer Hello World! , but if I request curl http://0.0.0.0:80/ I have the curl: (7) Failed to connect to 0.0.0.0 port 80: Connection refused response.

    If I request the url provided by amazon ( http://ec2-xx-xxx-xxx-xx.us-east-2.compute.amazonaws.com ) through the browser I get the following error:

    This site can’t be reached
    

    Conclusion The route is not found either by the browser or the curl of the termial because Flask is not responding by 0.0.0.0:80.

    Question:

    Does anyone know how to upload this Flask server to 0.0.0.0:80?

        
    asked by anonymous 28.11.2018 / 04:57

    1 answer

    -1

    I found the solution:

    Open port 80 as already done and use:

    //flaskserver.py
    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    

    and upload the server

    sudo FLASK_APP=flaskserver.py flask run --host=0.0.0.0 --port=80
    
        
    28.11.2018 / 05:31