Publishing your Flask website on AWS EC2 can seem daunting, but you can transition smoothly from being a local host to living with the proper steps. This comprehensive guide will walk you through the entire process, from preparing your project to enabling your website for public access.

Project Preparation: Uploading Your Flask Project to AWS EC2

Step 1: Launch an EC2 Instance

  • Sign in to AWS Management Console: Navigate to the EC2 dashboard.
  • Launch an Instance: Choose an Amazon Machine Image (AMI), such as Ubuntu Server.
  • Configure Instance Details: Set up your instance with the necessary configurations.
  • Add Storage and Tags: Customize your storage and tag your instance appropriately.
  • Configure Security Group: Open ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).

Step 2: Transfer Your Flask Project

Generate a Key Pair: Securely save the private key file (.pem).

SSH into Your EC2 Instance:

ssh -i “your-key.pem” ubuntu@your-ec2-public-ip

Install Git:

sudo apt update

sudo apt install git

Clone Your Flask Project:

git clone https://github.com/your-repo/flask-project.git

Environment Setup: Creating a Virtual Environment and Installing Dependencies

Step 3: Set Up the Environment

Navigate to Your Project Directory:

cd flask-project

Install Python3 and Pip:

sudo apt install python3-pip python3-dev

Create a Virtual Environment:

python3 -m venv venv

source venv/bin/activate

Install Project Dependencies:

pip install -r requirements.txt

Web Server Configuration: Installing and Setting Up Nginx as a Reverse Proxy

Step 4: Install Nginx

Update Packages and Install Nginx:

sudo apt update

sudo apt install nginx

Step 5: Configure Nginx as a Reverse Proxy

Create Nginx Configuration File:

sudo nano /etc/nginx/sites-available/flask_project

Add the Following Configuration:

server {

    listen 80;

    server_name your-domain.com;

    location / {

        proxy_pass http://127.0.0.1:8000;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header X-Forwarded-Proto $scheme;

    }

}

Enable the Configuration and Restart Nginx:

sudo ln -s /etc/nginx/sites-available/flask_project /etc/nginx/sites-enabled

sudo nginx -t

sudo systemctl restart nginx

WSGI Server Deployment: Running Your Flask App with Gunicorn in the Background

Step 6: Install and Configure Gunicorn

Install Gunicorn:

pip install gunicorn

Test Gunicorn:

gunicorn –bind 127.0.0.1:8000 wsgi:app

Step 7: Create a Systemd Service for Gunicorn

Create Gunicorn Service File:

sudo nano /etc/systemd/system/gunicorn.service

Add the Following Configuration:

[Unit]

Description=gunicorn daemon

After=network.target

[Service]

User=ubuntu

Group=www-data

WorkingDirectory=/home/ubuntu/flask-project

ExecStart=/home/ubuntu/flask-project/venv/bin/gunicorn –workers 3 –bind 127.0.0.1:8000 wsgi:app

[Install]

WantedBy=multi-user.target

Start and Enable Gunicorn:

sudo systemctl start gunicorn

sudo systemctl enable gunicorn

Enabling Your Website: Starting Nginx and Accessing Your Deployed Flask Site

Step 8: Start Nginx

Restart Nginx:

sudo systemctl restart nginx

Step 9: Access Your Flask Site

Visit Your Domain or Public IP: Open your browser and navigate to http://your-domain.com or http://your-ec2-public-ip.

Congratulations! Your Flask website is now live on AWS EC2. You’ve transitioned your project from localhost to a publicly accessible web server.

References

Deploying a Flask application to Elastic Beanstalk

Deploy Python Application using AWS App Runner