How to set up Nginx and Gunicorn to make the Python Flask app on AWS EC2 accessible web pages

Henry Coder
Dec 10, 2023

--

Step 1: Install Nginx

sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

Step 2: Configure Nginx as a Reverse Proxy for Flask

sudo nano /etc/nginx/conf.d/winjob.conf
server {
listen 80;
server_name your_server_ip_or_domain;

location / {
proxy_pass http://localhost:5000; # Forward requests to Gunicorn
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;
}
}

(NOTE: Replace your_server_ip_or_domain with your EC2 instance's public IP or your domain name. )

sudo nginx -t
sudo systemctl reload nginx

Step 3: Configure Security Group

Step 4: Deploy Flask App with Gunicorn

Activate the Virtual Environment and install gunicorn:

source ~/winjob/venv_winjob/bin/activate
pip install gunicorn
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app

It works!!!

--

--