与Heroku的Django部署

我试图用heroku部署现有的django项目,然后按照heroku提供的步骤进行部署,并根据需要进行调整以适合我的项目。 只是为了快速完成我迄今为止所做的工作:

  • 在我的virtualenv中安装了django-toolbelt
  • 在名为Procile项目的根目录下创建了一个Procfile,其中包括:

    web: gunicorn projectname.wsgi
    
  • 使用foreman start确认我的项目仍在本地正常运行

  • 使用pip freeze创建requirements.txt并将其放置在我的项目的根目录中
  • 将以下内容添加到settings.py

    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()
    
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    # Allow all host headers
    ALLOWED_HOSTS = ['*']
    
    # Static asset configuration
    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/static/'
    
     STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
    )
    
  • 这是我有点困惑的一点,演练没有提及我现有的数据库设置会发生什么,保持现状,删除,更改?

  • 将以下内容添加到wsgi.py中:

    from django.core.wsgi import get_wsgi_application
    from dj_static import Cling
    
    application = Cling(get_wsgi_application())
    
  • 将heroku添加为git remote,并将我的项目推到heroku。 有效。

  • heroku ps:scale web=1
  • 但是,当我尝试访问我的项目时,出现Application Error 。 当我用heroku heroku ps --app projectname检查状态时,它说heroku ps --app projectname已经崩溃。 重新启动什么也不做。

    heroku logs --app projectname产量:

    2013-07-18T00:01:14.246956+00:00 heroku[web.1]: Starting process with command `gunicorn projectname.wsgi`
    2013-07-18T00:01:16.054952+00:00 app[web.1]: 2013-07-18 00:01:16 [2] [INFO] Starting gunicorn 17.5
    2013-07-18T00:01:16.058972+00:00 app[web.1]: 2013-07-18 00:01:16 [2] [INFO] Listening at: http://0.0.0.0:55131 (2)
    2013-07-18T00:01:16.061566+00:00 app[web.1]: 2013-07-18 00:01:16 [2] [INFO] Using worker: sync
    2013-07-18T00:01:16.080073+00:00 app[web.1]: 2013-07-18 00:01:16 [7] [INFO] Booting worker with pid: 7
    2013-07-18T00:01:16.091347+00:00 app[web.1]: Traceback (most recent call last):
    2013-07-18T00:01:16.091347+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    2013-07-18T00:01:16.091347+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    2013-07-18T00:01:16.091347+00:00 app[web.1]:     worker.init_process()
    2013-07-18T00:01:16.091347+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
    2013-07-18T00:01:16.091347+00:00 app[web.1]:     self.callable = self.load()
    2013-07-18T00:01:16.091558+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 353, in import_app
    2013-07-18T00:01:16.091347+00:00 app[web.1]:     return util.import_app(self.app_uri)
    2013-07-18T00:01:16.091558+00:00 app[web.1]:     __import__(module)
    2013-07-18T00:01:16.091347+00:00 app[web.1]: 2013-07-18 00:01:16 [7] [ERROR] Exception in  worker process:
    2013-07-18T00:01:16.091347+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 33, in load
    2013-07-18T00:01:16.091558+00:00 app[web.1]: Traceback (most recent call last):
    2013-07-18T00:01:16.091771+00:00 app[web.1]:     return util.import_app(self.app_uri)
    2013-07-18T00:01:16.091771+00:00 app[web.1]:     __import__(module)
    2013-07-18T00:01:16.091558+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    2013-07-18T00:01:16.091558+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 115, in wsgi
    2013-07-18T00:01:16.091771+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 33, in load
    2013-07-18T00:01:16.091347+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 115, in wsgi
    2013-07-18T00:01:16.091558+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    2013-07-18T00:01:16.091558+00:00 app[web.1]:     self.callable = self.load()
    2013-07-18T00:01:16.091771+00:00 app[web.1]: ImportError: No module named projectname.wsgi
    2013-07-18T00:01:16.091558+00:00 app[web.1]: ImportError: No module named projectname.wsgi
    2013-07-18T00:01:16.091558+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
    2013-07-18T00:01:16.091558+00:00 app[web.1]:     worker.init_process()
    2013-07-18T00:01:16.099257+00:00 app[web.1]: 2013-07-18 00:01:16 [7] [INFO] Worker exiting (pid: 7)
    2013-07-18T00:01:16.091771+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 353, in import_app
    2013-07-18T00:01:16.286985+00:00 app[web.1]: 2013-07-18 00:01:16 [2] [INFO] Shutting down: Master
    2013-07-18T00:01:16.288144+00:00 app[web.1]: 2013-07-18 00:01:16 [2] [INFO] Reason: Worker failed to boot.
    2013-07-18T00:01:17.956769+00:00 heroku[web.1]: Process exited with status 3
    2013-07-18T00:01:17.981029+00:00 heroku[web.1]: State changed from starting to crashed
    2013-07-18T00:11:46.404151+00:00 heroku[web.1]: State changed from crashed to starting
    2013-07-18T00:11:50.427658+00:00 heroku[web.1]: Starting process with command `gunicorn projectname.wsgi`
    2013-07-18T00:11:51.405718+00:00 app[web.1]: 2013-07-18 00:11:51 [2] [INFO] Starting gunicorn 17.5
    2013-07-18T00:11:51.406995+00:00 app[web.1]: 2013-07-18 00:11:51 [2] [INFO] Listening at: http://0.0.0.0:21344 (2)
    2013-07-18T00:11:51.407226+00:00 app[web.1]: 2013-07-18 00:11:51 [2] [INFO] Using worker: sync
    2013-07-18T00:11:51.418300+00:00 app[web.1]: 2013-07-18 00:11:51 [7] [INFO] Booting worker with pid: 7
    2013-07-18T00:11:51.425145+00:00 app[web.1]: Traceback (most recent call last):
    2013-07-18T00:11:51.425145+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 33, in load
    2013-07-18T00:11:51.425145+00:00 app[web.1]: 2013-07-18 00:11:51 [7] [ERROR] Exception in worker process:
    2013-07-18T00:11:51.425145+00:00 app[web.1]:     self.callable = self.load()
    2013-07-18T00:11:51.425145+00:00 app[web.1]:     worker.init_process()
    2013-07-18T00:11:51.425145+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
    2013-07-18T00:11:51.425145+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 115, in wsgi
    2013-07-18T00:11:51.425145+00:00 app[web.1]:     return util.import_app(self.app_uri)
    2013-07-18T00:11:51.425145+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    2013-07-18T00:11:51.425145+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    2013-07-18T00:11:51.425341+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 353, in import_app
    2013-07-18T00:11:51.425341+00:00 app[web.1]:     __import__(module)
    2013-07-18T00:11:51.425341+00:00 app[web.1]: ImportError: No module named projectname.wsgi
    2013-07-18T00:11:51.425341+00:00 app[web.1]: Traceback (most recent call last):
    2013-07-18T00:11:51.425341+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 473, in spawn_worker
    2013-07-18T00:11:51.425341+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    2013-07-18T00:11:51.425341+00:00 app[web.1]:     self.callable = self.load()
    2013-07-18T00:11:51.425341+00:00 app[web.1]:     worker.init_process()
    2013-07-18T00:11:51.425341+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 115, in wsgi
    2013-07-18T00:11:51.425341+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
    2013-07-18T00:11:51.425513+00:00 app[web.1]:     return util.import_app(self.app_uri)
    2013-07-18T00:11:51.425513+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 33, in load
    2013-07-18T00:11:51.425513+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 353, in import_app
    2013-07-18T00:11:51.425513+00:00 app[web.1]:     __import__(module)
    2013-07-18T00:11:51.425513+00:00 app[web.1]: ImportError: No module named projectname.wsgi
    2013-07-18T00:11:51.425868+00:00 app[web.1]: 2013-07-18 00:11:51 [7] [INFO] Worker exiting (pid: 7)
    2013-07-18T00:11:51.550395+00:00 app[web.1]: 2013-07-18 00:11:51 [2] [INFO] Shutting down: Master
    2013-07-18T00:11:51.550395+00:00 app[web.1]: 2013-07-18 00:11:51 [2] [INFO] Reason: Worker failed to boot.
    2013-07-18T00:11:52.851413+00:00 heroku[web.1]: Process exited with status 3
    2013-07-18T00:11:52.867339+00:00 heroku[web.1]: State changed from starting to crashed
    2013-07-18T00:14:14.285978+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=projectname.herokuapp.com fwd="173.54.54.86" dyno= connect= service= status=503 bytes=
    2013-07-18T00:14:14.405875+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=projectname.herokuapp.com fwd="173.54.54.86" dyno= connect= service= status=503 bytes=
    

    任何帮助/建议将不胜感激,我发现没有办法直接联系heroku,一切都指您在这里。


    这是我有点困惑的一点,演练没有提及我现有的数据库设置会发生什么,保持现状,删除,更改?

    我也跟着他们的演练,也遇到了同样的问题。 对我来说,这是DATABASES声明造成的错误。 我用他们提供的数据库配置替换了我的数据库配置,但只能将其添加到已由数据库设置创建的字典中。 因此,不要替换你的数据库语句,只需添加:

    DATABASES['default'] =  dj_database_url.config()
    

    在构建过程中,Heroku有可能确定了错误的应用程序类型。 这发生在我身上:

    (venv)josephs-mbp:myproject josephfusaro$ git push heroku master
    Fetching repository, done.
    Counting objects: 27, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (13/13), done.
    Writing objects: 100% (14/14), 1.26 KiB | 0 bytes/s, done.
    Total 14 (delta 9), reused 0 (delta 0)
    
    -----> Node.js app detected
    

    这跳了出来,因为我正在构建一个Python / Django应用程序。 我遇到了这个Stackoverflow线程,它解释了它是如何发生的,并展示了如何覆盖构建包。 在这个例子中,我们将指定Python构建包,但是您可以指示Heroku为您构建的任何类型的应用程序使用构建包(点击此处查看完整列表)

    heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-python
    

    那么不太确定。 尽管heroku的文档建议您尝试过。 我真的不确定gunicorn在heroku中的安装位置,以及它如何导入django项目。 使用django设置的流程/服务器更可能的方式似乎是

    web:bin / gunicorn_django --workers = 4 --bind = 0.0.0.0:$ PORT django_project / settings.py

    如下所示:http://blog.abhiomkar.in/2011/09/17/deploying-django-on-heroku-mac-os-x/

    链接地址: http://www.djcxy.com/p/72879.html

    上一篇: Django Deployment With Heroku

    下一篇: Get Max Count from Group by