部署Flask项目到heroku

狄赞
2023-12-01

部署到heroku

登录

heroku login

输入在官网注册的账户和 密码

创建应用

heroku create appname (名字可以自己选,将作为自己的域名)

heroku create –buildpack heroku/python

Creating heroku-postgresql on ⬢ floating-ravine-41608… free
https://floating-ravine-41608.herokuapp.com/ | https://git.heroku.com/floating-ravine-41608.git

添加数据库

heroku addons:create heroku-postgresql -a floating-ravine-41608

Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-parallel-90394 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

显示git和网页地址

git remote show heroku

进入数据库

heroku pg:psql -a floating-ravine-41608

DATABASE_URL: postgres://xriuxrfpqtfmpj:f10a0a38f76e0e2be3bc52e69aa477d161736775976c93ce6e0470bce8c92d82@ec2-107-21-126-193.compute-1.amazonaws.com:5432/d9uqv1edl13ka8

如果运行git push heroku出错,运行这步

heroku git:remote -a floating-ravine-41608

push上去 即可运行

git push heroku master

部署flask 需要的文件

runtime.txt 表示运行时的python环境
python-2.7.14
requirements.txt 用 pip freeze > requirements.txt 生成
Flask==0.10.1
Flask-SQLAlchemy==2.0
Flask-WTF==0.12
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.8
WTForms==2.0.2
Werkzeug==0.10.4
click==4.1
decorator==4.0.2
geocoder==1.4.1
gunicorn==19.3.0
itsdangerous==0.24
psycopg2==2.7.3.1
ratelim==0.1.6
requests==2.7.0
six==1.9.0
wsgiref==0.1.2
Procfile 显示路由的app和 运行的服务器
web: gunicorn routes:app
.env.env 配置环境变量 ,heroku会直接从这里读取
FLASK_APP=flasky.py
FLASK_CONFIG=heroku
MAIL_USERNAME=fjl2401
MAIL_PASSWORD=youpass
 类似资料: