Heroku Basics
思考题
- 什么是 Heroku?
Heroku是一个支持多种编程语言的云平台即服务
- 如何在 Heroku 中运行 Flask 应用?
- 运行时如何获得服务端的日志?
- 能在本地事先进行应用开发吗?
- 调试过程中的错误说的是什么意思?
进一步...
- Heroku 中支持什么数据库?
- 如何在应用到自己的程序里呢?
- 如何备份?
Setup
Install Postgres
not sure if it worked...
Download Heroku CLI
downlaod and install
$ heroku login
按提示输入用户名密码
Prepare the app
$ git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started
Deploy the app
将app部署到Heroku.
$ heroku create
Creating app... done, ⬢ enigmatic-brook-42131
https://enigmatic-brook-42131.herokuapp.com/ | https://git.heroku.com/enigmatic-brook-42131.git
创建app时,一个git remote(叫heroke
)被同时创建并与本地的git repository关联。
$ git push heroku master
Counting objects: 274, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (123/123), done.
Writing objects: 100% (274/274), 42.80 KiB | 0 bytes/s, done.
Total 274 (delta 133), reused 274 (delta 133)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-2.7.13
remote: $ pip install -r requirements.txt
remote: Collecting dj-database-url==0.4.1 (from -r /tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/requirements.txt (line 1))
remote: Downloading dj-database-url-0.4.1.tar.gz
remote: Collecting Django==1.9.7 (from -r /tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/requirements.txt (line 2))
remote: Downloading Django-1.9.7-py2.py3-none-any.whl (6.6MB)
remote: Collecting gunicorn==19.6.0 (from -r /tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/requirements.txt (line 3))
remote: Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
remote: Collecting psycopg2==2.6.1 (from -r /tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/requirements.txt (line 4))
remote: Downloading psycopg2-2.6.1.tar.gz (371kB)
remote: Collecting whitenoise==2.0.6 (from -r /tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/requirements.txt (line 5))
remote: Downloading whitenoise-2.0.6-py2.py3-none-any.whl
remote: Installing collected packages: dj-database-url, Django, gunicorn, psycopg2, whitenoise
remote: Running setup.py install for dj-database-url: started
remote: Running setup.py install for dj-database-url: finished with status 'done'
remote: Running setup.py install for psycopg2: started
remote: Running setup.py install for psycopg2: finished with status 'done'
remote: Successfully installed Django-1.9.7 dj-database-url-0.4.1 gunicorn-19.6.0 psycopg2-2.6.1 whitenoise-2.0.6
remote:
remote: $ python manage.py collectstatic --noinput
remote: 58 static files copied to '/tmp/build_f7297f457ded2fcc00e2c79d8e6d9454/gettingstarted/staticfiles', 58 post-processed.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 43.3M
remote: -----> Launching...
remote: Released v4
remote: https://enigmatic-brook-42131.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/enigmatic-brook-42131.git
* [new branch] master -> master
app已经部署好了,ensure that at least one instance of the app is running:
heroku ps:scale web=1
Scaling dynos... done, now running web at 1:Free
可以通过提供的URL访问https://enigmatic-brook-42131.herokuapp.com/
或者在Terminal $ heroku open
访问
View logs
heroku logs --tail
Define a Procfile
Procfile
是一个放在根目录里的文本,申明应用需要用什么文件启动
web: gunicorn gettingstarted.wsgi --log-file -
声明一个单一的进程类型, web
gunicorn
is a pure-Python HTTP server for WSGI applications. It allows you to run any Python application concurrently by running multiple Python processes within a single dyno. It provides a perfect balance of performance, flexibility, and configuration simplicity.
不确定是不是所有的python在heroku web 应用都是一样的。。。
Declare app dependencies
Heroku通过根目录中的requirements.txt
文件判断是一个Python app。
Heroku读取这个文件,运行一下语句安装所需的Python dependencies
pip install -r requirements.txt
在本地运行,需要创建一个虚拟环境
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
Collecting dj-database-url==0.4.1 (from -r requirements.txt (line 1))
Downloading dj-database-url-0.4.1.tar.gz
Collecting Django==1.9.7 (from -r requirements.txt (line 2))
Downloading Django-1.9.7-py2.py3-none-any.whl (6.6MB)
100% |████████████████████████████████| 6.6MB 153kB/s
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 3))
Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
100% |████████████████████████████████| 122kB 7.2MB/s
Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
Downloading psycopg2-2.6.1.tar.gz (371kB)
100% |████████████████████████████████| 378kB 2.3MB/s
Collecting whitenoise==2.0.6 (from -r requirements.txt (line 5))
Downloading whitenoise-2.0.6-py2.py3-none-any.whl
Building wheels for collected packages: dj-database-url, psycopg2
Running setup.py bdist_wheel for dj-database-url ... done
Stored in directory: /Users/mac/Library/Caches/pip/wheels/a6/ee/0b/fa5aa1269e9e877fc925294ecd7752e9265f42ee18d38c37dd
Running setup.py bdist_wheel for psycopg2 ... done
Stored in directory: /Users/mac/Library/Caches/pip/wheels/21/21/80/89c21755b92a5874b6a8930d015bf1c4fc1b253f24e8d880cb
Successfully built dj-database-url psycopg2
Installing collected packages: dj-database-url, Django, gunicorn, psycopg2, whitenoise
Successfully installed Django-1.9.7 dj-database-url-0.4.1 gunicorn-19.6.0 psycopg2-2.6.1 whitenoise-2.0.6
Run the app locally
this example is for Django
$ python manage.py collectstatic
# response with "yes"
$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
22:14:25 web.1 | [2017-02-22 22:14:25 +0800] [23662] [INFO] Starting gunicorn 19.6.0
22:14:25 web.1 | [2017-02-22 22:14:25 +0800] [23662] [INFO] Listening at: http://0.0.0.0:5000 (23662)
22:14:25 web.1 | [2017-02-22 22:14:25 +0800] [23662] [INFO] Using worker: sync
22:14:25 web.1 | [2017-02-22 22:14:25 +0800] [23665] [INFO] Booting worker with pid: 23665
在 http://localhost:5000 可以访问
Push local changes
修改requirements.txt
添加requests==2.9.1
$ pip install -r requirements.txt
$ heroku local
Pushing to github
$ git add .
$ git commit -m "Demo"
$ git push heroku master
$ heroku open
Change Log
2017.02.24 Initial Draft