Heroku环境配置
- 注册Heroku账户
- 安装Hroku Toolbelt
- 创建程序:首先要确保程序在 Git 源码控制系统中,然后在程序的顶级目录下运行创建命令。当你创建一个 app, 将创建并关联一个名为
heroku的远端到你的本地仓库. - 安装Postgres
学习Heroku的基本功能
参考教程 https://devcenter.heroku.com/articles/getting-started-with-python#set-up
1. 准备一个App
git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started
2. 部署App
在python-getting-started的子目录中执行
$ heroku create #你会在这里收到一个变量,类似infinite-fjord-41827
$ git push heroku master
如果要清空所有App,可以使用命令
$ for app in $(heroku apps); do heroku apps:destroy --app $app --confirm $app; done如果push的时候发现语言不正确,在把heroku create换成
$ heroku create --buildpack heroku/python
If you have some problem when you run the code, notice that before you can push an app to Heroku, you’ll need to initialize a local Git repository and commit your files to it. For example, if you have an app in a directory, myapp, then create a new repository for it:
Reference: https://devcenter.heroku.com/articles/git
$ cd myapp
$ git init
Initialized empty Git repository in .git/
$ git add .
$ git commit -m "my first commit"
Created initial commit 5df2d09: my first commit
44 files changed, 8393 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 Procfile
create mode 100644 app/controllers/source_file
$ heroku git:remote -a falling-wind-1624
接着让这个App中的至少一个实例运行
$ heroku ps:scale web=1
接着你就可以打开网页http://infinite-fjord-41827.herokuapp.com/了
当然你也可以直接用CLI来打开这个网址,这样比较方便
$ heroku open
3.查看Logs
$ heroku logs --tail
4. Define a Procfile
现在你有了一个程序,并且做好了部署,但是用户点击网站时,具体应该执行哪个文件,则需要Procfile文件来指明。这个文件必须放在程序的顶级文件夹中。它每一行声明一条需要运行的命令,格式如下
$ web: gunicorn gettingstarted.wsgi --log-file -
web表示你部署的是一个web应用。
参考:
Procfile文件内容的格式很简单:在每一行中指定一个任务名,后跟一个冒号,然后是执行这个任务的命令。名为web的任务比较特殊任务,Heroku 使用这个任务启动 Web 服务器。
依赖库操作
heroku要求app根目录下必须有requirement.txt,才能部署应用,并且克隆的应用也要安装指定的依赖库.
- 导出依赖库 pip freeze >> requirement.txt
- 安装依赖库 pip install -r requirement.txt