How to deploy your Flask App on AWS EC2 instance with Nginx, Gunicorn

  1. Create an instance on AWS account
  2. How to connect to the AWS instance
  3. Clone the Flask App from Github to AWS EC2 instance
  4. Understand why we need WSGI
  5. Set up the Gunicorn for the application
  6. Create a systemd Unit file
  7. Install and Configure Nginx

1.Create an instance on AWS account

2. How to connect to the EC2 instance

$ chmod 400 “nameofkeypair.pem”
$ ssh -i “nameofkeypair.pem” ubuntu@”public-ip-of-instance or public-dns-name-of-instance”

3. Clone the Flask App from Github to AWS EC2 instance

$ sudo apt-get update$ sudo apt-get install python3-pip$ sudo pip3 install virtualenv$ git clone https://github.com/busraerkoc/BookLibrary.git$ cd BookLibrary$ virtualenv venv$ source venv/bin/activate$ pip install -r requirements.txt
* Serving Flask app "library_site" (lazy loading) 
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!

4. Understand why we need WSGI

5. Set up the Gunicorn for the application

$ pip3 install gunicorn
$ gunicorn --bind 0.0.0.0:8000 library_site:app
[2021–01–07 14:29:30 +0000] [1288] [INFO] Starting gunicorn 20.0.4 
[2021–01–07 14:29:30 +0000] [1288] [INFO] Listening at: http://127.0.0.1:8000 (1288)
[2021–01–07 14:29:30 +0000] [1288] [INFO] Using worker: sync
[2021–01–07 14:29:30 +0000] [1291] [INFO] Booting worker with pid: 1291

6. Create a systemd Unit file

$ sudo nano /etc/systemd/system/flaskapp.service
[Unit]
Description=Gunicorn daemon to serve my flaskapp
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/BookLibrary
ExecStart=/home/ubuntu/BookLibrary/venv/bin/gunicorn --bind unix:flaskapp.sock library_site:app
[Install]
WantedBy=multi-user.target
$ sudo systemctl start flaskapp$ sudo systemctl enable flaskapp
Created symlink /etc/systemd/system/multi-user.target.wants/flaskapp.service → /etc/systemd/system/flaskapp.service.

7. Install and Configure Nginx

$ sudo apt-get install nginx
  • First way is create a configuration file in the /etc/nginx/conf.d/*.conf .
  • Second way is create a configuration file in the /etc/nginx/sites-available directory and then link it to /etc/nginx/sites-enable/* .
  • sites-available : stores all of your virtual host configurations.
  • sites-enabled : contains symlinks to file in sites-available directory. It allows you disable virtual host configuration by removing this symlink.
$ sudo nano /etc/nginx/sites-available/app
server { 
listen 80;
server_name private_ip_of_your_ec2_instance;

location / {
proxy_pass http://unix:/home/ubuntu/BookLibrary/flaskapp.sock;
}
}
$ sudo rm -rf default
$ sudo systemctl start nginx
$ sudo systemctl enable nginx 
security group inbound rule of the EC2 instance
flask book library application main page

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Importance of On-Demand Food Delivery Clone App in 2021–22

Python — Introduction

Code faster with RxSwift

Fun with SQL using Postgres and Azure Data Studio

What Does a Salesforce Admin Do?

AWS Amplify: How to add @key to an existing App-sync Schema

Where to write which code in Springboot Application

IDOR with Autorize!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Büşra Erkoç

Büşra Erkoç

More from Medium

Installing Nginx Using AWS EC2 User Data

Deploy A Static Website with Docker

Configure Web Server on AWS Cloud

Introduction to Docker