Flask Traditional Deployments: Zero to One <Setup a VPS>

The following article wil introduce how to set up a virtual private server, and some basic linux commands.

Author: v1siuol

Last-Modified: 2018.08.13

Background

  • A quite developed web project ( As a matter of fact, you don't have to prepare any codes, but deploying some simple easy sites costs a lot. For Flask case, flask run listens 127.0.0.1:5000 by default. However, flask run --host 0.0.0.0 listens a.b.c.d:5000 under the same wifi. Have fun with it first, then come and playing with VPS! )
  • This series is a supplyment for Tradition Deploments in Chapter 17. Deployment in Flask Web Development, 2nd Edition written by Miguel Grinberg.
  • Project address: https://github.com/v1siuol/v1siuol-site
  • $ something refers to command line in bash

0. How to Select a Virtual Private Server

地区 服务 备注
国内 阿里云 需备案
国内 腾讯云 需备案
海外 AWS 国内的AWS由北京的光环新网运营

以上是些主流的云服务器啦,贵贵的。

I select AWS here since AWS provides free tier for one year. For details, refer to https://aws.amazon.com/free/.

AWS Free Tier includes 750 hours of Amazon Elastic Compute Cloud (EC2) Linux t2.micro instance usage (1 GiB of memory and 32-bit and 64-bit platform support). Saying we can run 1 instance continuously for one month, or 2 instances in the same time continuouly for half month.

It also includes 30 GB of Amazon Elastic Block Storage (EBS) in any combination of General Purpose (SSD) or Magnetic, plus 2 million I/Os (with EBS Magnetic) and 1 GB of snapshot storage.

1. How to Create the Amazon EC2

Official documents: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/get-set-up-for-amazon-ec2.html

Basic steps:

  1. Sign Up for AWS
  2. Create an IAM User
  3. Create a Key Pair
  4. Create a Virtual Private Cloud (VPC)
  5. Create a Security Group

My VPC is like:

  1. AMI: Ubuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-5e8bb23b

    Ubuntu Server 16.04 LTS (HVM),EBS General Purpose (SSD) Volume Type.

  2. Instance type: t2.micro (Variable ECUs, 1 vCPUs, 2.5 GHz, Intel Xeon Family, 1 GiB memory, EBS only)

2. How to Connect to my VPC

Official documents: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Two commands using in my Mac terminal:

  1. $ chmod 400 /path/my-key-pair.pem

    This is used to set the permssions of your private key file as readonly.

    The link to learn more on chmod : https://chmodcommand.com/chmod-400/

  2. $ ssh -i /path/my-key-pair.pem user_name@public_dns_name

    Default user_name is like:

    • For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.
    • For a Centos AMI, the user name is centos.
    • For a Debian AMI, the user name is admin or root.
    • For a Fedora AMI, the user name is ec2-user or fedora.
    • For a RHEL AMI, the user name is ec2-user or root.
    • For a SUSE AMI, the user name is ec2-user or root.
    • For an Ubuntu AMI, the user name is ubuntu.
    • Otherwise, if ec2-user and root don't work, check with the AMI provider.

    public_dns_name is like:ec2-198-51-100-1.compute-1.amazonaws.com

  3. The updated version of my second command: $ ssh -o TCPKeepAlive=yes -o ServerAliveInterval=60 -i /path/my-key-pair.pem user_name@public_dns_name

    In my practice, I usually encounter if I do nothing in terminal for like 5 minutes.

    packet_write_wait: Connection to <ip> port 22: Broken pipe

    So I update the my connect by sending the 'HeartBeat‘ to server to hold my connection.

3. How to Close my VPC Connection

$ logout : then you will see Connection to <public_dns_name> closed. .

4. (Optional) How to transfer files between my PC and EC2

  1. Send files from PC to EC2: PC --> EC2

    $ scp -i /path/my-key-pair.pem /path/SampleFile.txt user_name@public_dns_name:/path

    /path : my suggestion is to use ~ or /tmp . If sending to other paths in your EC2, it might say permission denied .

    So after scp the file to /tmp , ssh to your server and use command mv /tmp/SamepleFile.txt /path/you_want .

  2. Send files from EC2 to PC: EC2 --> PC

    $ scp -i /path/my-key-pair.pem user_name@public_dns_name:/path_in_EC2/SampleFile.txt ~/path_on_PC

Thank you.

Reference:

项目地址:https://github.com/v1siuol/v1siuol-site

网站地址:http://www.v1siuol.com/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容