How to Deploy a Flask Applicatio
The guide will follow these steps:
1.How to create an EC2 instance
2.How to deploy your own flask app on the created instance
1. How to create an EC2 instance
After creating a new account on AWS, you have to search “EC2” under “services” and then “compute” section. Once EC2 dashboard is open, click on “instances” in the list view on the left and then on Launch instances.
In the next page you can choose the machine image you prefer. Let’s choose Deep Learning AMI (Ubuntu 18.04) Version 48.0.
Choose a t2.medium. After that, the following default settings may be fine for an application without special needs.
截屏2021-08-26 下午2.15.12.png
select IAM Role which have access to EC2 Instance.
截屏2021-08-26 下午2.23.09.png
configure EC2 Instance,add Elastic Inference for EC2 Instance.
截屏2021-08-26 下午2.24.03.png
Before launching the instance it is important to configure the security group: if you have not one yet, you need to create a new one and you can change its name for a better association with your application. After that, beyond port 22 for SSH connection, it is necessary to open a port for the HTTP protocol. If you want to release your application, the right choice is to open port 80, but an application needs root privileges to access it, so if you have not them or you don’t want to release the application but only deploy it on the server maybe for testing, you can choose the port 8080.
image.png
In the review section you can check the details of your instance and if all is fine then click on the Launchbutton. To access your virtual machine via SSH you have to create a new key pair and download the generated* .pem *file (click *here *to learn more about pem files). It is absolutely fundamental that you store this file in a secure place because it is the only way to access your virtual machine, furthermore if it ends up in wrong hands someone can access your instance!
After the Key Pair download, you can finally launch the instance!
Being free when associated with a running instance, it is a good idea to create an elastic IP and use this IP to point the server. To do that, you have to go to the “Elastic IPs” section, allocate a new Elastic IP and associate it to your instance. The main benefit of having an Elastic IP is that if an instance is shut down you can easily associate it to another instance to keep your web application reachable.
image.png
SSH into the virtual machine
First of all, enter in your bash and locate the .pem file previously downloaded. Type chmod 600 ./<PEM_NAME>.pem to restrict read and write permission to the private key, so only the user/owner can read and write, but no one can execute. After that, you can log into the virtual machine typing
$ ssh -i path/<PEM-NAME>.pem ubuntu@<IP-ADDRESS>
2. How to deploy your own flask app on the created instance
Once created your ec2 instance and verified it is running, let’s see how to access it from bash and copy the files of the flask application into the Ubuntu server.
Deploy to Production
In this guide we are talking about deployment, but especially in Flask applications it is important to be able to distinguish between this term and production. When running publicly rather than in development, you should not use the built-in development server.
A better choice is to use a production WSGI server, for example Gunicorn3. If you don’t want to publicly release your web application, you can skip the rest of this subsection, otherwise I recommend you to install Waitress and use this WSGI server instead of the built-in one.
First,we need to install nginx.
$ sudo apt-get install nginx
Then,we need to install Gunicorn3
sudo apt-get install gunicorn3
We should upload our application zip file to ubuntu des path,typing
$ scp -i path/<PEM-NAME>.pem filePath ubuntu@<IP-ADDRESS>:path
cd path where the application zip file is
$ cd despath
and unzip your appliction zip typing
$ unzip xxx.zip
then del the source zip file
$ del xxx.zip
cd nginx path
$ cd /etc/nginx
$ cd sites-enabled/
$ sudo vi flaskapp
open gunicorn,you can see
copy this text to flaskapp
截屏2021-08-26 下午5.32.01.png
sudo service nginx restart
cd application path
gunicorn3 app:app
you can stop nginx by:
sudo service nginx stop
Thanks.