Run Flask app on ec2 – tutorial

I was following this tutorial on Youtube for running a Flask app on Youtube.

We will need for this purpose – Python3 & PIP Package manager, Nginx, Gunicorn3 – WSGI web app, Flask

Connect to ec2 using SSH instructions on your instance. This tutorial for is an ubuntu instnace.

Now once you are connected, below are the commands to put in terminal to make your instance do things.

  1. Connect using SSH
  2. sudo apt-get update
  3. sudo apt-get install python3
  4. whereis python
  5. sudo apt-get install python3-pip
  6. whereis pip3
  7. sudo pip install flask
  8. sudo apt-get install nginx
  9. sudo apt-get install gunicorn3
  10. mkdir flaskapplication
  11. vi app.py
  12. sudo service start nginx
  13. Run following code in standalone mode by adding it to app.py

from flask import Flask

app = Flask(name)
@app.route(“/”)
def homepage():
return “Hello World”
if name==”main“:
app.run(host=’0.0.0.0′,port=8080)

So when you go to your instance IP address with port attached (8080) you should see it running. Apparently, this is the standalone mode.

So to run it on prroduction server, we need to install Guinicorn3

So run the following commands now

  1. cd /etc/nginx/
  2. cd sites-enabled/
  3. sudo vi flaskapp
  4. Paste deployment code from https://gunicorn.org/#deployment. The edited script is added at the bottom of the page
  5. :wq to save and quit
  6. sudo service ngninx restart
  7. cd ~
  8. cd flaskapplication
  9. gunicorn3 app:app
  10. Run it!
  11. Voila!

Guinicord deplyment script is as below –

  server {
    listen 80;
    server_name [your ip addresss];

    location / {
        proxy_pass http://127.0.0.1:8000;
        
    }
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

Only humans shall pass *