Welcome to part 9 of tutorial series on Flask application with EC2 instance. This part is based on the request from one of the subscriber on my YouTube channel. As the title says, I’m going to take you through on how to deploy multiple flask application on an EC2 instance.

Ideally, we will deploy the applications on a different port. Here, I have launched a fresh instance. You can use the previous instance as well. If you have not referred the previous tutorial then you can refer this tutorial on how to create an EC2 instance. Once, the instance is launched, login into an EC2 instance via SSH.

Since it is a fresh instance we need to run certain commands to configure it and make it suitable for running the flask application. I have covered on how to configure EC2 instance for deploying a flask application in one of the earlier tutorials.

Post configuring the instance, we will start with creating the flask application. In fact 2 flask application. Execute the below commands to initiate.

mkdir flaskapp
mkdir flaskapp1

Our two flask application is flaskapp and flaskapp1 respectively. Now, navigate to each of the directories one after another and execute below commands.

cd flaskapp
mkdir templates
mkdir static
touch app.py

cd flaskapp1
mkdir templates
mkdir static
touch app.py

Now, we will create the home.html file in templates directory on both applications respectively.

cd flaskapp
cd templates
vim home.html

Paste the below snippet in home file.

<html>
    <body><h1>Flask application 1</h1></body>
</html>

Same, procedure will be for another application that is flaskapp1. But, change Flask application 1 with Flask application 2 in the home.html file.

Moving along with app.py file in both applications. Navigate to each applications and execute vim app.py and paste below snippet.

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def home():
    render_template("home.html")

Now, we are all set with the structure of both the application. Hence, we start with creating the gunicorn as a service for both the application. You can refer one of the earlier tutorials on creating gunicorn as a service. We will follow the same method to create gunicorn as a service for both the application respectively.

cd /etc/systemd/system/
sudo vim flask1.service

Paste the below script in flask1 service file.

[Unit]
Description=Flask Application 1
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/flaskapp
ExecStart=/usr/bin/gunicorn3 --workers 3 --bind unix:flaskapp.sock -m 007 app:app

You can learn more about creating a service from here. Now, we will move forward with creating service for another application. Execute sudo vim flask2.service and paste below snippet.

[Unit]
Description=Flask Application 2
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/flaskapp1
ExecStart=/usr/bin/gunicorn3 --workers 3 --bind unix:flaskapp.sock -m 007 app:app

We will reload the daemon by executing sudo systemctl daemon-reload. And we will start the service.

sudo service flask1 start
sudo service flask2 start

And we can check the status of the service that whether it is running successfully or not.

sudo service flask1 status
sudo service flask2 status

We have successfully created and started the service. Hence, we will move on to configuring nginx part.

cd
cd /etc/nginx/sites-enabled/

We will create new config file by executing.

sudo vim flaskapp

Paste the below snippet and save the file.

server {
    listen 80;
    server_name 0.0.0.0;
    location / {
         proxy_pass http://unix:/home/ubuntu/flaskapp/flaskapp.sock;
    } 
}

server {
    listen 8080;
    server_name 0.0.0.0;
    location / {
         proxy_pass http://unix:/home/ubuntu/flaskapp/flaskapp.sock;
    } 
}

In the above script, we have defined two server directive for two flask application respectively. As a next step, we will restart the nginx server for new changes to take effect.

sudo service nginx restart

Before, we try to load the application on the browser. Make sure you have opened port 80 and 8080 via security group. Post opening the port, you can access both applications at respective ports. Please refer video tutorial mentioned below for step by step tutorial.


SSL is a crucial part of any web application. In the next tutorial, I will take you through on how to integrate OpenSSL with flask application. In a mean time refer my YouTube channel for more tutorials. Keep sharing and stay tuned for more. Follow me on Twitter