pydata

Keep Looking, Don't Settle

deploy flask on heroku

  1. 安装git,如果已经有git,可以忽略 sudo apt-get install git

  2. 建立一个文件夹,比如叫flask_heroku,专门用来放flask的东西 到这个文件夹下 cd flask_heroku

  3. install pip on ubuntu if don't have it sudo apt-get install python-pip

  4. install virtualenv in ubuntu sudo apt-get install python-virtualenv on windows, you can do pip install virtualenv

  5. run to create temperorary enviroment called venv sudo virtualenv venv 这时候你的flask_heroku下面会多了一个文件夹venc,下面安装所有的东西都要使用venv/bin/pip,不能使用系统自带的pip

  6. activate the virtual enviroment source venv/bin/activate deactivate the virtual enviroment deactivate 下面用venv/bin/pip安装flask,flask-wtf和其他所有东西的时候,都要在virtual enviroment下面安装,否则requirements.txt会有问题

  7. install flask(确认已经激活virtual enviroment) venv/bin/pip install Flask use venv/bin/pip list or venv/bin/pip show flask to check if you have flask installed.

  8. install useful packages if you want(确认已经激活virtual enviroment) venv/bin/pip install flask-wtf venv/bin/pip install flask-mail

  9. register on heroku.com to get username/password

  10. download heroku toolbelt on ubuntu wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

  11. log on to heroku from your command line heroku login

  12. create heroku app from your command line or from the heroku.com from comman line heroku create my-heroku-name from heroku.com do it directly in https://dashboard.heroku.com/apps 可以更改你的app的名字 https://dashboard.heroku.com/apps/pydata/settings

  13. 配置postgres数据库
    heroku create my-heroku-app-cn

  14. install prodcution web server gunicorn(确认已经激活virtual enviroment) venv/bin/pip install gunicorn

  15. 产生必要的两个文件requirements.txt和Procfile 产生requirements.txt venv/bin/pip freeze > requirements.txt 产生Procfile 用任意文本编辑器建立一个名字为Procfile的文件,在里面输入web: gunicorn app:app

  16. git部署 去掉不必要的文件,只留下必要的文件和文件夹 git git add . git commit -m "first flask app test" git push heroku master

Reference

  1. The Flask Mega-Tutorial, Part I: Hello, World!
  2. The Flask Mega-Tutorial, Part XVIII: Deployment on the Heroku Cloud
  3. 如何部署Python Web应用:记录一次Heroku部署完整过程