🚀 Want to **deploy your Flask app on AWS EC2**? In this step-by-step guide, I'll show you how to **set up an AWS EC2 instance, install Flask, configure Gunicorn & Nginx, and run your Flask app in production**!
By the end of this tutorial, you’ll learn:
✅ How to **launch an AWS EC2 instance**
✅ How to **set up Python, Flask, and dependencies**
✅ How to **configure Gunicorn for production**
✅ How to **set up Nginx as a reverse proxy**
✅ How to **secure your Flask app with a firewall**
---
## **🔹 What You’ll Learn in This Video:**
✔️ How to **create an AWS EC2 instance** (Ubuntu/Amazon Linux)
✔️ How to **connect to EC2 via SSH**
✔️ How to **install Flask, Python, and Pip**
✔️ How to **run Flask with Gunicorn & Nginx**
✔️ How to **set up a firewall for security**
---
## **🔹 Prerequisites**
✔️ An **AWS Account** ([Sign Up Here](https://aws.amazon.com/free/))
✔️ Basic knowledge of **Flask & Python**
✔️ A **Flask app** ready for deployment
✔️ A system with **SSH (Mac/Linux) or PuTTY (Windows)**
---
## **🔹 Step 1: Launch an AWS EC2 Instance**
1️⃣ Go to **AWS Management Console** → Navigate to **EC2**
2️⃣ Click **Launch Instance**
3️⃣ Choose an **Amazon Machine Image (AMI)**:
- **Ubuntu 22.04 LTS** (Recommended)
- **Amazon Linux 2**
4️⃣ Select **Instance Type** → Choose **t2.micro** (Free Tier)
5️⃣ Configure **Security Groups**:
- Allow **SSH (port 22)**
- Allow **HTTP (port 80)**
- Allow **HTTPS (port 443)**
6️⃣ Click **Launch** and download the **key pair (`.pem` file)**
---
## **🔹 Step 2: Connect to EC2 via SSH**
🔹 **For Windows (Using PuTTY):**
1️⃣ Convert your `.pem` file to `.ppk` using **PuTTYgen**
2️⃣ Open **PuTTY**, enter **EC2 Public IP**, and load the `.ppk` key
3️⃣ Click **Open** and log in as `ubuntu` (for Ubuntu) or `ec2-user` (for Amazon Linux)
🔹 **For Mac & Linux (Using Terminal):**
```bash
chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@your-ec2-public-ip
```
💡 Replace `your-key.pem` with your actual key file name and `your-ec2-public-ip` with your EC2 instance's public IP.
---
## **🔹 Step 3: Install Flask, Python, and Dependencies**
🔹 **Update the system and install Python:**
```bash
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip -y
```
🔹 **Install Flask and Virtual Environment:**
```bash
sudo apt install python3-venv -y
python3 -m venv venv
source venv/bin/activate
pip install flask gunicorn
```
🔹 **Create a Simple Flask App (`app.py`):**
```python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask on AWS EC2!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
---
## **🔹 Step 4: Run Flask with Gunicorn**
🔹 **Test the app locally:**
```bash
python app.py
```
🔹 **Run Flask with Gunicorn (Production Mode):**
```bash
gunicorn --bind 0.0.0.0:5000 app:app
```
---
## **🔹 Step 5: Set Up Nginx as a Reverse Proxy**
🔹 **Install Nginx:**
```bash
sudo apt install nginx -y
```
🔹 **Configure Nginx for Flask:**
```bash
sudo nano /etc/nginx/sites-available/flaskapp
```
🔹 **Add the following configuration:**
```nginx
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;
}
}
```
🔹 **Activate the Nginx configuration:**
```bash
sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled
sudo systemctl restart nginx
```
---
## **🔹 Step 6: Configure Firewall & Test the App**
🔹 **Allow necessary ports:**
```bash
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
```
🔹 **Access your Flask app in a browser:**
```plaintext
http://your-ec2-public-ip
```
---
## **🔹 Common Issues & Fixes**
🔹 **Nginx not working?**
✅ **Fix:** Check Nginx status → `sudo systemctl status nginx`
🔹 **Permission denied (publickey) error?**
✅ **Fix:** Ensure your **key file permissions** are correct:
```bash
chmod 400 your-key.pem
```
🔹 **Firewall blocking requests?**
✅ **Fix:** Open port 80 for HTTP traffic:
```bash
sudo ufw allow 80/tcp
```
---
## **🔹 Who Is This Tutorial For?**
✅ **Developers** deploying Flask apps on AWS
✅ **Cloud engineers** setting up production servers
✅ **Beginners** learning AWS EC2 & cloud deployment
---
## **👍 Like, Share & Subscribe!**
If this tutorial helped you, **LIKE, SHARE, and SUBSCRIBE** for more **AWS, Flask, and Cloud Computing tutorials**! 🚀
💬 Have questions? Drop them in the **comments** below!
---
### **🔹 Hashtags:**
#AWS #Flask #EC2 #DeployFlask #CloudComputing #Python #Gunicorn #Nginx #DevOps #Ubuntu #AWSDeployment