- π€ Introduction
- π Features
- βοΈ Tech Stack
- π Folder Structure
- π§° Prerequisites
- β Give A Star
- π Follow Me
- π Learn More
π€ Introduction
In this tutorial, we'll guide you through the process of deploying a Flask web application on an AWS EC2 instance. Flask is a lightweight web application framework written in Python, and AWS EC2 (Elastic Compute Cloud) provides scalable computing capacity in the cloud.
π Features
- Deploy a Flask web application on AWS EC2.
- Use Gunicorn as the WSGI server.
- Configure Nginx as a reverse proxy.
βοΈ Tech Stacks
- Python
- Flask
- AWS EC2
- Gunicorn
- Nginx
π Folder Structure
- helloworld/
- venv/
- app.py
π§° Prerequisites
- An AWS account
- Basic knowledge of Python and Flask
- Familiarity with the Linux command line
First, SSH into your EC2 instance and update the package list:
sudo apt-get update && sudo apt-get upgrade
Then, install the Python virtual environment package:
sudo apt-get install python3-venv
Create a new directory for your Flask app and navigate into it:
mkdir helloworld
cd helloworld
Create a virtual environment named 'venv':
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Within the virtual environment, install Flask using pip:
pip install Flask
Create a file named app.py
sudo vi app.py
And add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == "__main__":
app.run()
Run the Flask app to verify it works:
python app.py
Run Gunicorn WSGI server to serve the Flask Application When you βrunβ flask, you are actually running Werkzeugβs development WSGI server, which forward requests from a web server. Since Werkzeug is only for development, we have to use Gunicorn, which is a production-ready WSGI server, to serve our application.
Install Gunicorn using pip:
pip install gunicorn
Run Gunicorn to serve the Flask application:
gunicorn -b 0.0.0.0:8000 app:app
Gunicorn is running (Ctrl + C to exit gunicorn)!
Use systemd to manage Gunicorn Systemd is a boot manager for Linux. We are using it to restart gunicorn if the EC2 restarts or reboots for some reason. We create a .service file in the /etc/systemd/system folder, and specify what would happen to gunicorn when the system reboots. We will be adding 3 parts to systemd Unit file β Unit, Service, Install
Unit β This section is for description about the project and some dependencies Service β To specify user/group we want to run this service after. Also some information about the executables and the commands. Install β tells systemd at which moment during boot process this service should start. With that said, create an unit file in the /etc/systemd/system directory
Create a systemd unit file:
sudo nano /etc/systemd/system/helloworld.service
Add the following configuration:
[Unit]
Description=Gunicorn instance for a simple hello world app
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/helloworld
ExecStart=/home/ubuntu/helloworld/venv/bin/gunicorn -b localhost:8000 app:app
Restart=always
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl start helloworld
sudo systemctl enable helloworld
Check if the app is running with
curl localhost:8000
Run Nginx Webserver to accept and route request to Gunicorn Finally, we set up Nginx as a reverse-proxy to accept the requests from the user and route it to gunicorn.
Install Nginx:
sudo apt-get install nginx
Start and enable the Nginx service:
sudo systemctl start nginx
sudo systemctl enable nginx
Edit the Nginx default configuration file:
sudo nano /etc/nginx/sites-available/default
Add the following code at the top of the file (below the default comments)
upstream flaskhelloworld {
server 127.0.0.1:8000;
}
Add a proxy_pass to flaskhelloworld atlocation /
location / {
proxy_pass http://flaskhelloworld;
}
Restart Nginx for changes to take effect:
sudo systemctl restart nginx
Now, your Flask application should be accessible via your EC2 instance's public IP address.
Congratulations! You have successfully deployed a Flask web application on an AWS EC2 instance using Gunicorn and Nginx as a reverse proxy.
β Give A Star
You can also give this repository a star to show more people and they can use this repository.
π Follow Me
π Learn More
Of course! Here are the resources with descriptions and direct links:
Python:
-
Official Python Documentation: The Python Software Foundation maintains comprehensive documentation covering all aspects of Python, from beginner to advanced topics.
-
Python Crash Course by Eric Matthes: This book is great for beginners who want to get started with Python programming. It covers the basics of Python syntax and also delves into more advanced topics.
-
Automate the Boring Stuff with Python by Al Sweigart: Perfect for those interested in automating tasks using Python, this book covers practical Python programming for total beginners.
Flask:
-
Flask Documentation: The official Flask documentation is an excellent resource for learning Flask. It covers everything from installation to advanced topics like Blueprints and Extensions.
-
Flask Web Development by Miguel Grinberg: This book provides a comprehensive guide to building web applications with Flask. It covers everything from basic concepts to more advanced topics like deployment and testing.
-
Flask Mega-Tutorial by Miguel Grinberg: This online tutorial series covers building a full-featured web application with Flask, step by step. It's a great resource for hands-on learning.
AWS EC2:
-
AWS Documentation - EC2: The official AWS documentation provides comprehensive guides and tutorials for using EC2 instances, covering everything from launching instances to managing security and scalability.
-
AWS Certified Solutions Architect - Associate Certification Guide: This guide covers all the topics you need to know to become certified in AWS, including EC2 instances.
-
AWS in Action by Andreas Wittig and Michael Wittig: This book provides practical examples and insights into using various AWS services, including EC2.
Gunicorn:
-
Gunicorn Documentation: The official Gunicorn documentation provides information on installing, configuring, and using Gunicorn as a WSGI server for Python web applications.
-
Deploying Django by Kenneth Love: While focusing on Django, this book includes a chapter on deploying Django applications with Gunicorn, useful for understanding its usage in real-world scenarios.
-
Real Python Tutorials: Real Python offers tutorials on various Python-related topics, including deploying web applications with Gunicorn.
Nginx:
-
Nginx Documentation: The official Nginx documentation provides detailed information on installing, configuring, and using Nginx as a web server and reverse proxy.
-
Nginx Essentials by Valery Kholodkov: This book covers the essentials of Nginx configuration and usage, making it a good resource for beginners.
-
DigitalOcean Tutorials: DigitalOcean offers numerous tutorials on web development and server administration topics, including configuring Nginx.