自己写了个Django博客,最后成功的部署到了heroku上,记录一下艰辛的过程(windows上)

准备阶段

首先得有个heroku账号,我是用gmail注册的

然后就可以按照基础教程一步一步部署到heroku上

https://devcenter.heroku.com/articles/getting-started-with-python

安装好git(版本控制工具)以及heroku客户端

安装好后将heroku启动路径加入环境变量,接下来就能在cmd登入heroku

$ heroku login
Enter your Heroku credentials.
Email: user@example.com                       #  账号
Password:                                     #  密码

接下来可以按照官方教程下一个Django app试验一下流程

$ git clone https://github.com/heroku/python-getting-started.git           
$ cd python-getting-started

上传操作

上传前需要安装一些包才能正常使用静态文件以及数据库服务见下文django设置,如果只是用官方示例app直接进行下方操作就行

上传操作其实和上传到github的操作大致一致,先进入Django目录下git init—》git add . ——》git commit -m “first commit”

$ heroku create (可加一个app name)#在heroku上创建app
Creating lit-bastion-5032 in organization heroku... done, stack is cedar-14
http://lit-bastion-5032.herokuapp.com/ | https://git.heroku.com/lit-bastion-5032.git
Git remote heroku added 

$ git push heroku master          #把本地文件部署到heroku上   

$ heroku ps:scale web=1           #确保app在运转

$ heroku open                     #打开网站

查看日志了解你部署的app运转情况,ctrl+C退出

$ heroku logs --tail
2014-08-15T15:17:55.780361+00:00 app[web.1]: 2014-08-15 15:17:55 [2] [INFO] Listening at: http://0.0.0.0:19585 (2)
2014-08-15T15:17:55.780488+00:00 app[web.1]: 2014-08-15 15:17:55 [2] [INFO] Using worker: sync
2014-08-15T15:17:55.830489+00:00 app[web.1]: 2014-08-15 15:17:55 [7] [INFO] Booting worker with pid: 7
2014-08-15T15:17:55.779494+00:00 app[web.1]: 2014-08-15 15:17:55 [2] [INFO] Starting gunicorn 19.0.0
2014-08-15T15:17:56.321151+00:00 heroku[web.1]: State changed from starting to up
2014-08-15T15:17:57.847806+00:00 heroku[router]: at=info method=GET path="/" host=lit-bastion-5032.herokuapp.com request_id=7fd99883-20ec-43d5-8b2d-5204351cdf2d fwd="94.174.204.242" dyno=web.1 connect=1ms service=240ms status=200 bytes=679

上传前的准备工作

在项目根目录下创建一个Procfile文本文件,用于定义app启动需要执行的命令

web: gunicorn gettingstarted.wsgi --log-file -        中间的gettingstarted.wsgi需替换成项目名.wsgi,不替换会报错找不到gettingstarted

还可以定义一个在本地windows上运行的Procfile.windows文件

web: python manage.py runserver 0.0.0.0:5000

定义项目所依赖的环境,安装的包

如果你使用的是Pipenv创建的虚拟环境根目录下会有Pipfile这个文件

使用其他方法的话需要新建一个requirements.txt文件,可以进入虚拟环境后输入命令pip freeze > requirement.txt写入

这个文件让heroku知道要安装哪些包

beautifulsoup4==4.6.0
bs4==0.0.1
dj-database-url==0.5.0
dj-static==0.0.6
Django==2.0.7
django-toolbelt==0.0.1
gunicorn==19.9.0
Pillow==5.2.0
psycopg2==2.7.5
pytz==2018.5
static3==0.7.0
whitenoise==3.3.1
django-heroku==0.3.1
certifi==2018.4.16    
chardet==3.0.4    
idna==2.7    
requests==2.19.1    
setuptools==28.8.0
urllib3==1.23
requirement.txt

相关文章:

  • 2022-12-23
  • 2021-11-06
  • 2021-10-09
  • 2021-12-01
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2021-10-08
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案