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

Asked

Viewed 504 times

0

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

For this we need to solve these two steps:

  1. Open that door in EC2
  2. Make Flask rotate in 0.0.0.0:80

The first step is ok. The door is open to interet: inserir a descrição da imagem aqui

For the second step I’m following this 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)  

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

FLASK_APP=flaskserver.py flask run

That 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 the app.run() and Flask is finally created in http://127.0.0.1:5000/.

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

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

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

This site can’t be reached

Completion the route is found neither by the browser nor by the curl thermial because Flask is not responding by 0.0.0.0:80.

Question:

someone knows how to climb this Flask server at 0.0.0.0:80 ?

1 answer

-1

I found the solution:

Open door 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
  • 1

    Read Deploy to Production, please; this link says that the server created by flask run is not recommended for production and says a better way to do.

  • And this also applies to HTTP servers provided by Flask, Bottle, Django, Rails etc...

  • Even says flask is not the best option for a server in production.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.