• 如何在heroku上部署你的Django程序


    自己写了个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

    本地启动app

    $ python manage.py collectstatic           #报错了需根据错误信息解决问题,上传到heroku也有这个过程搜集静态文件
    $ heroku local web -f Procfile.windows     #本地启动app服务

    打开http://localhost:5000 访问本地app

    通过heroku上传本地更新的内容

    $ git add .
    $ git commit -m "Demo"
    $ git push heroku master
    $ heroku open

    部署后的控制台

    可以通过heroku run命令打开控制台

    $ heroku run python manage.py shell
    $ heroku run python manage.py makemigrations        
    $ heroku run python manage.py migrate
    $ heroku run python manage.py createsuperuser

    Django部署的一些特别设置

    安装Gunicorn web server

    $ pipenv install gunicorn

    安装django-heroku

    安装whitenoise

    settings文件设置

    import django_heroku
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = False         #把debug模式设置成false
    
    INSTALLED_APPS = [
        'whitenoise.runserver_nostatic',      加到app最上面
        'django.contrib.admin',
    ]
    
    MIDDLEWARE = [
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',        加到中间件
    ]
    ###########静态文件设置###############
    # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) # Django用户上传的都叫media文件 MEDIA_URL = "/media/" # media配置,用户上传的文件都默认放在这个文件夹下 MEDIA_ROOT = os.path.join(BASE_DIR, "media")
    #数据库设置
    if os.getenv('DATABASE_URL') is not None: import dj_database_url DATABASES['default'] = dj_database_url.config() STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' # Activate Django-Heroku. django_heroku.settings(locals())

    数据库相关

    django数据库上传不上去

    heroku用的是heroku-postgresql数据库服务

    所以部署好app后得重新写入数据库

  • 相关阅读:
    有哪些学习openCV的网站或书籍?
    开发者自述:我是这样学习 GAN 的
    FSGAN Subject Agnostic Face Swapping and Reenactment翻译+重点标注
    01-GAN公式简明原理之铁甲小宝篇
    CentOS 7 之 hostapd.conf 配置说明
    dlib库实现人脸68个特征点的定标操作的原理有哪些?
    生成对抗网络GAN---生成mnist手写数字图像示例(附代码)
    deepfake 资源总结
    大话CNN经典模型:VGGNet
    欧几里得距离
  • 原文地址:https://www.cnblogs.com/blue-day/p/9347185.html
Copyright © 2020-2023  润新知