Introduction
Deploying a Flask web application on an AWS EC2 instance allows you to make your app accessible. In this tutorial, we'll complete the complete setup, from launching an EC2 instance to running your Flask app with Gunicorn and Nginx.
1. Launch an EC2 Instance
Step 1: Log in to AWS and Create an EC2 Instance
Go to the AWS EC2 Dashboard.
Click Launch Instance.
Choose an Amazon Machine Image (AMI): Select Ubuntu 22.04 LTS.
Choose an instance type: Select t2.micro (Free Tier eligible).
Configure instance details (default settings are fine).
Add storage (default 8GB EBS is sufficient).
Configure security group:
Allow HTTP (80), HTTPS (443), and Custom TCP Rule (5000, for Flask).
Allow SSH (22) for remote access.
Click Launch and download the private key (
.pem
file).
Step 2: Connect to Your EC2 Instance
Use SSH to connect to your instance:
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
2. Install Dependencies:
Step 3: Update System Packages
sudo apt update && sudo apt upgrade -y
Step 4: Install Required Software
sudo apt install python3 python3-pip python3-venv nginx git -y
3. Set Up Flask Application
Step 5: Clone or Upload Your Flask App
If your app is on GitHub:
git clone https://github.com/your-repo.git flask-app
cd flask-app
Or use SCP to upload files from your local machine:
scp -i "your-key.pem" -r /path/to/your/flask-app ubuntu@your-ec2-public-ip:/home/ubuntu/
Step 6: Create a Virtual Environment & Install Dependencies
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
4. Run Flask Application with Gunicorn
Step 7: Test Flask Application
Run the Flask app:
python app.py
If it's running on http://127.0.0.1:5000
, it's working! Press Ctrl+C
to stop it.
Step 8: Run Flask with Gunicorn
gunicorn --bind 0.0.0.0:5000 wsgi:app
If your app file is named app.py
, use wsgi:app
, otherwise adjust accordingly.
5. Configure Nginx as a Reverse Proxy
Step 9: Configure Nginx
sudo nano /etc/nginx/sites-available/flask
Add the following configuration:
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;
}
}
Save and close (CTRL+X
, then Y
, then Enter
).
Step 10: Enable the Nginx Configuration
sudo ln -s /etc/nginx/sites-available/flask /etc/nginx/sites-enabled
sudo systemctl restart nginx
6. Keep the Flask App Running with Supervisor
Step 11: Install and Configure Supervisor
sudo apt install supervisor -y
Create a Supervisor config file:
sudo nano /etc/supervisor/conf.d/flask.conf
Add the following:
[program:flask]
command=/home/ubuntu/flask-app/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 wsgi:app
directory=/home/ubuntu/flask-app
autostart=true
autorestart=true
stderr_logfile=/var/log/flask.err.log
stdout_logfile=/var/log/flask.out.log
Save and close.
Step 12: Start Supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start flask
7. Access Your Flask Application
Now, visit http://your-ec2-public-ip/
in your browser. ๐ Your Flask app is live!
your ec2-public-IP and the port on which you have deployed your application should be written.
Hence I have successfully deployed my Flask-based web application on AWS EC2
Conclusion
You have successfully deployed a Flask web application on an AWS EC2 instance using Gunicorn and Nginx! Your app is now running in production. ๐
Next Steps:
Set up a custom domain with AWS Route 53.
Optimize Flask performance with caching and database tuning.
Monitor your application with AWS CloudWatch or Prometheus.
Let me know if you need any clarifications! Happy coding! ๐