Deployment of Flask App|Gunicorn| Nginx|AWS-EC2

The blog will cover the whole process of deployment of a flask app on AWS-EC2 using Gunicorn & Nginx in just 7 Steps


Steps to deploy the Flask App on EC2:

sudo apt-get update
sudo apt install nginx

sudo apt install python3
sudo apt install python3-pip
pip3 install virtualenv
pip3 install Flask
pip3 install Flask-RESTful
pip3 install gunicorn

python3 -m venv base
source base/bin/activate

cd /etc/systemd/system
vim gunicorn.service

Paste the following code:

Description=Gunicorn Service

ExecStart=/home/ubuntu/base/bin/gunicorn — workers 3 — error-logfile /home/ubuntu/gunicorn_logs/error_log — bind unix:my_flask_app.sock -m 007 app:app

a. Replace the WorkingDirectory path with your path
b. Use command “pwd” at your project to the exact path of the project
c. Use command “which gunicorn” after activating your venv to know the path of the guinicorn used
d. Create a “gunicorn_logs” directory for error logs of gunicorn

cd /etc/nginx/sites-available
vim default

Paste the following code:
listen 80;
server_name PrivateIP_of_your _EC2 instance;

location / {
proxy_pass http://unix:/home/ubuntu/my-flask-app/my_flask_app.sock;

Add Private IP of your EC2 instance here.

use: nginx -t
Output should be:
Configuration test OK

sudo systemctl daemon-reload
sudo service gunicorn stop
sudo service gunicorn start
sudo service gunicorn status #output should be running here
sudo service nginx stop
sudo service nginx start
sudo service nginx status #output should be running here

Hurray !! You just deployed your Flask App
Visit http://your_ec2_private_ip
Your app should be up and running.

If still can’t access your project:
1. vim /var/log/nginx/access.log
2. vim /var/log/nginx/error.log
3. vim /home/ubuntu/gunicorn_logs/error_log

Engineer trying have "T" shaped skill set