使用Heroku进行Django部署

时间:2013-07-18 00:30:36

标签: django heroku

我正在尝试使用heroku部署我现有的django项目,跟随heroku提供的步骤并根据需要调整以适应我的项目。只是为了快速完成我到目前为止所做的事情:

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

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

  • 使用requirements.txt创建pip freeze并将其放在项目的根目录中
  • 将以下内容添加到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远程并将我的项目推送到heroku。它奏效了。

  • heroku ps:scale web=1

但是,当我尝试访问我的项目时,我得到Application Error。当我用heroku heroku ps --app projectname检查状态时,它说dyno已经崩溃了。重启没有任何作用。

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,所有内容都指的是你。

4 个答案:

答案 0 :(得分:2)

  

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

我也跟着他们的演练并遇到了同样的问题。对我来说,是DATABASES语句导致错误。我用他们提供的数据库配置替换了我的数据库配置,而它必须添加到已经由数据库设置创建的字典中。因此,不要替换数据库语句,只需添加:

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

答案 1 :(得分:2)

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应用程序。我遇到了this Stackoverflow thread,它解释了它是如何发生的,并展示了如何覆盖构建包。对于此示例,我们将指定Python buildpack,但您可以指示Heroku将buildpack用于您正在构建的任何类型的应用程序(click here for a full list

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

答案 2 :(得分:0)

不太确定。虽然heroku的文档建议你尝试的方式。我真的不确定在heroku中安装了gunicorn以及它是如何导入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/

答案 3 :(得分:0)

我用过:

DATABASES = {
'default': dj_database_url.config(default='sqlite:///db.sqlite')
}

此表单可以在本地和heroku上使用