Once your web application is ready, deploying it to a cloud platform allows users to access it globally. In this tutorial, we will cover how to deploy Flask and Django applications on AWS (EC2), Google Cloud (App Engine), and Heroku.
port 22) and HTTP (port 80).ssh -i your-key.pem ubuntu@your-ec2-public-ipsudo apt update
sudo apt install python3-pip python3-venv nginxmkdir flask-app && cd flask-app
python3 -m venv venv
source venv/bin/activate
pip install flask gunicornCreate a simple Flask app (app.py):
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Flask App Deployed on AWS"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)gunicorn --bind 0.0.0.0:5000 app:appsudo nano /etc/nginx/sites-available/flaskAdd the following content:
server {
listen 80;
server_name your-ec2-public-ip;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Enable and restart Nginx:
sudo ln -s /etc/nginx/sites-available/flask /etc/nginx/sites-enabled
sudo systemctl restart nginxYour Flask app is now deployed on AWS!
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
gcloud initEnsure your project structure is:
flask-app/
│── app.py
│── requirements.txt
│── app.yamlapp.yaml)runtime: python39
entrypoint: gunicorn -b :$PORT app:app
handlers:
- url: /.*
script: autogcloud app deployYour Flask app is live on Google App Engine.
curl https://cli-assets.heroku.com/install.sh | sh
heroku loginCreate Procfile:
web: gunicorn app:appgit init
heroku create flask-app-name
git add .
git commit -m "Deploy Flask app"
git push heroku masterYour Flask app is live on Heroku!
Follow similar steps as Flask, but:
pip install django gunicorngunicorn --bind 0.0.0.0:8000 myproject.wsgi:applicationFollow the same steps as Flask, but update app.yaml:
runtime: python39
entrypoint: gunicorn -b :$PORT myproject.wsgiThen, deploy using:
gcloud app deployFollow the same steps as Flask, but:
Procfile:web: gunicorn myproject.wsgigit push heroku masterWe explored how to deploy Flask and Django applications on AWS EC2, Google Cloud App Engine, and Heroku.
Sign in to join the discussion and post comments.
Sign inObject-Oriented Programming (OOP) in Python
Learn the fundamentals of Object-Oriented Programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and more. Understand how OOP enhances code reusability, scalability, and organization.
Python Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.