在 Heroku 上简单部署 Flask 应用

创建 Heroku 帐户

如果 您还没有 Heroku 帐户,可前往 Heroku 注册一个免费帐户。






下载并安装 Git 与 Heroku CLI

Heroku CLI 是 Heroku 的命令行界面工具,它使直接从终端创建和管理 Heroku 应用变得容易。

要安装 Heroku CLI 需要先安装本控制系统 Git。如果您尚未安装 Git,请在安装 CLI 之前请先安装 Git 并完成初始化设置。

安装好这两个工具后我们可以在命令行测试下,看其使用是否正常:

$ git --version
git version 2.33.0.windows.1
$ heroku
CLI to interact with Heroku

VERSION
  heroku/7.53.0 win32-x64 node-v12.21.0
  ...






创建新的 Heroku App

我们在 Heroku 网站上点击选择 Create New App,填写好应用名称,注意这个名称要求是全网站唯一的,如果别人使用了你想要的名称,请在选择别的名字。






准备 Flask 文件和配置

我们先编写一个最级别的 Flask 文件,命名为 app.py

# app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'hello world'
    
if __name__ == '__main__':
    app.run()

另外我们还需要编写 requirements.txt 文件,来告诉 Heroku 需要安装上面 Python 库;当然你也可以使用 pip freeze >requirements.txt 命令来生成。

注意,虽然我们在本地测试网站时候用不上,但必须带上 gunicorn 库,这是部署 Flask 应用时必备的。

requirements.txt 文件

click==8.0.1
colorama==0.4.4
Flask==2.0.1
gunicorn==20.1.0
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
Werkzeug==2.0.1

另外,我们还需编写 Procfile 文件(注意:这是个没有后缀名的文件),来告诉 heroku 应用的入口在哪。

Procfile 文件

web: gunicorn app:app

第一个 app 代表 Flask 应用的入口 python 文件的名称(本例是: app.py)。

第二个 app 代表在 Flask 入口文件里定义的应用全局变量名称(本例是: app = Flask(__name__)

完成了这几项准备后我们看看现在我目录结构:

flask-microblog-diego/
    app.py
    requirements.txt
    Procfile






登录 Heroku CLI

在命令行使用 Heroku CLI 工具首先需要登录,登录命令如下:

$ heroku login

执行后会自动跳转到浏览器相应页面,届时输入邮箱和密码等信息即可。

如果想在命令行直接输入邮箱和密码登录可以使用该命令:

heroku login -i






使用 Git 推送 Flask 文件到云端

1.初始化 git
$ git init
2.添加远程 heroku git 存储库

flask-microblog-diego 是我创建的 app 的名称,请你们换成自己使用的名字。

$ heroku git:remote -a flask-microblog-diego
set git remote heroku to https://git.heroku.com/flask-microblog-diego.git
3.推送 Flask 文件到云端

添加要推送的文件:

$ git add .

$ git commit -am "make it better"
[master (root-commit) 1bf63b5] make it better
 3 files changed, 19 insertions(+)
 create mode 100644 Procfile.txt
 create mode 100644 app.py
 create mode 100644 requirements.txt

把本地的 master 分支推送到 heroku 的 master 分支:

$ git push heroku master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 306 bytes | 306.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/python
remote: -----> Python app detected
remote: -----> No Python version was specified. Using the same version as the last build: python-3.9.6
remote:        To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> No change in requirements detected, installing from cache
remote: -----> Using cached install of python-3.9.6
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 52.7M
remote: -----> Launching...
remote:        Released v4
remote:        https://flask-microblog-diego.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/flask-microblog-diego.git
   1bf63b5..6690f4f  master -> master

这时候,我们的应用已经成功部署到:https://flask-microblog-diego.herokuapp.com/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容