Flask + Gunicorn 部署到 Heroku服务器,sqlite数据库,loggins一

简单部署步骤


哈哈:我的小站已经上线啦:http://tianya.heroku.com


1. 准备本地 Flask applicaion

venv

git init

修改.gitignore

pip freeze > requirements.txt(Heroku服务器会自动安装这个文件内的modules)

gunicorn==19.6.0

psycopg2==2.6.1

hello.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '

Hello World!

' @app.route('/user/') def user(name): return '

Hello, %s!

' % name if __name__ == '__main__': app.run(debug=False)

Procfile (Heroku服务器读取这个文件,来启动web 服务)

web: gunicorn hello:app     #(hello.py 是Flask主程序,app 是Flask 实例的名称)

2. 注册Heroku,并初始化

安装 CLI https://devcenter.heroku.com/articles/getting-started-with-python#set-up


heroku login

heroku create XXX (指定名字或者省略)
git add .
git commit -m "deploy 1"
git push heroku master

heroku addons:add heroku-postgresql:hobby-dev
heroku pg:promote heroku-postgresql  (提升为主database, flask里访问:os.environ.get('DATABASE_URL'))
heroku ps:scale web=1

heroku open 
heroku logs -t


3. BINGO!可以访问啦

我的小站:天涯脱水机VIPhttp://tianya.heroku.com


跑通之后,就可以把你自己的真正Flask app上线啦!


Tips:

  • 入门链接:Getting Started on Heroku with Python
  • Heroku 不支持sqlite,数据可以迁移到Postges,开发时使用SQLAlchemy就没问题,Sqlite转换
  • 免费版本,只支持Postges 10000条记录
  • 免费版本,平均每天只支持访问18小时。无连接30分钟后休眠
  • Logging:不支持写到本地文件,转而写到stdout(屏幕) 
  • Flask 如果调用运行其它python文件(subprocess.call(cmdline, shell=True)),print不能含中文
  • $heroku run bash 则相当于登录到Linux系统上,可以执行相关命令,包括运行python文件


常见问题:

1. 本地 sqlite migrate后,远程 postgres不能 migrate

已经找到原因,解决方案:http://blog.csdn.net/kevin_qq/article/details/52900151

2. 有时候git push失败

fatal: unable to access 'https://git.heroku.com/tianya.git/': The requested URL returned error: 500)

则使用git方式:

git clone https://git.heroku.com/tianya.git
cd tianya
git remote -v

现在应该没问题了,修改代码后,
git push origin master




设置环境变量:> heroku config:set VAR=value

heroku config:set FLASK_CONFIG=heroku


查看剩余Dynos: > heroku ps -a tianya
每个月有550小时Free Dynos,查看剩余的:
Free dyno hours quota remaining this month: 544h 6m (98%)

=== web (Free): gunicorn flask_ty:app (1)
web.1: up 2016/10/20 08:37:17 +0800 (~ 44m ago)


服务器 Log: > heroku logs -t 

2016-10-18T06:28:16.878608+00:00 heroku[api]: Deploy 0278c57 by [email protected]
2016-10-18T06:28:16.878656+00:00 heroku[api]: Release v10 created by [email protected]
2016-10-18T06:28:17.179122+00:00 heroku[slug-compiler]: Slug compilation started
2016-10-18T06:28:17.179132+00:00 heroku[slug-compiler]: Slug compilation finished
2016-10-18T06:28:17.360373+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-18T06:28:20.961459+00:00 heroku[web.1]: Starting process with command `gunicorn flask_ty:app`

2016-10-18T06:28:22.610402+00:00 app[web.1]: [2016-10-18 06:28:22 +0000] [3] [INFO] Starting gunicorn 19.6.0
2016-10-18T06:28:22.610801+00:00 app[web.1]: [2016-10-18 06:28:22 +0000] [3] [INFO] Listening at: http://0.0.0.0:40556 (3)
2016-10-18T06:28:22.610885+00:00 app[web.1]: [2016-10-18 06:28:22 +0000] [3] [INFO] Using worker: sync
2016-10-18T06:28:22.614948+00:00 app[web.1]: [2016-10-18 06:28:22 +0000] [9] [INFO] Booting worker with pid: 9
2016-10-18T06:28:22.661676+00:00 app[web.1]: [2016-10-18 06:28:22 +0000] [10] [INFO] Booting worker with pid: 10
2016-10-18T06:28:23.333913+00:00 heroku[web.1]: State changed from starting to up

2016-10-18T06:29:07.085419+00:00 heroku[router]: at=info method=GET path="/" host=tianya.herokuapp.com request_id=ccddd50c-e9d1-4ea9-9876-25a953d1f2ce
 fwd="158.255.208.122" dyno=web.1 connect=0ms service=9ms status=200 bytes=2085
2016-10-18T06:29:07.538530+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=tianya.herokuapp.com request_id=2098610a-f8f8-401e-a66c-6
f495dbbd3ae fwd="158.255.208.122" dyno=web.1 connect=0ms service=25ms status=404 bytes=386
2016-10-18T06:29:07.912919+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=tianya.herokuapp.com request_id=509fdd4d-1de3-49c5-b9a6-8
e4e536935ec fwd="158.255.208.122" dyno=web.1 connect=0ms service=9ms status=404 bytes=386
2016-10-18T06:29:07.902087+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=tianya.herokuapp.com request_id=4c84f872-a4a3-4a8a-921b-2
e5a9c9297a5 fwd="158.255.208.122" dyno=web.1 connect=0ms service=1ms status=404 bytes=386

^CTerminate batch job (Y/N)? y

查看数据库使用: >  heroku pg:info 

C:\git\tianya>heroku pg:info
===
Plan:        Hobby-dev
Status:      Available
Connections: 0/20
PG Version:  9.5.4
Created:     2016-10-18 06:16 UTC
Data Size:   7.4 MB
Tables:      0
Rows:        0/10000 (In compliance)
Fork/Follow: Unsupported
Rollback:    Unsupported
Add-on:      postgresql-adjacent-XXX



你可能感兴趣的:(Python,Flask,heroku,Python,heroku,Flask)