How to Deploy a Flask Application on Amazon EC2

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.

image.png

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!

image.png

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

截屏2021-08-26 下午5.30.17.png

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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,501評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,673評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,610評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,939評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,668評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,004評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,001評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,173評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,705評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,426評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,656評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,139評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,833評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,247評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,580評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,371評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,621評論 2 380

推薦閱讀更多精彩內容