部署到 Heroku 时 Django 应用程序崩溃 - Worker 无法启动
Posted
技术标签:
【中文标题】部署到 Heroku 时 Django 应用程序崩溃 - Worker 无法启动【英文标题】:Django App crashes when deployed to Heroku - Worker failed to boot 【发布时间】:2019-05-03 19:00:35 【问题描述】:很抱歉,这篇文章很长,但想提供尽可能多的信息。
在尝试将 django 应用程序部署到 heroku 上时遇到一些严重问题。。为此奋斗了好几天。
构建成功,部署也成功,但由于某种原因导航到站点地址时出现应用程序错误。
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoProject.settings")
application = get_wsgi_application()
settings.py
import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
SECRET_KEY = os.environ.get('SECRET_KEY')
DEBUG = False
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'users.apps.UsersConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.storage.CompressedManifestStaticFilesStorage'
]
ROOT_URLCONF = 'djangoProject.urls'
enter code here
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS':
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
]
WSGI_APPLICATION = 'djangoProject.wsgi.application'
DATABASES =
'default':
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
AUTH_PASSWORD_VALIDATORS = [
'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
,
'NAME':
'django.contrib.auth.password_validation.MinimumLengthValidator',
,
'NAME':
'django.contrib.auth.password_validation.CommonPasswordValidator',
,
'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
,
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOGIN_REDIRECT_URL = 'blog-home'
LOGIN_URL = 'login'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
USE_TLS = True
来自日志的错误消息:
2018-12-02T04:24:16.294582+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py ",第 129 行,在 init_process 2018-12-02T04:24:16.294583+00:00 应用 [web.1]: self.load_wsgi() 2018-12-02T04:24:16.294585+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py”,行138,在 load_wsgi 2018-12-02T04:24:16.294586+00:00 应用 [web.1]: self.wsgi = self.app.wsgi() 2018-12-02T04:24:16.294588+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py”,行67,在wsgi中 2018-12-02T04:24:16.294590+00:00 应用 [web.1]: self.callable = self.load() 2018-12-02T04:24:16.294591+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py”,行52,负载中 2018-12-02T04:24:16.294593+00:00 应用 [web.1]:返回 self.load_wsgiapp() 2018-12-02T04:24:16.294594+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py”,行41、在load_wsgiapp中 2018-12-02T04:24:16.294596+00:00 应用程序[web.1]:返回 util.import_app(self.app_uri) 2018-12-02T04:24:16.294598+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py”,第 350 行,在 import_app 2018-12-02T04:24:16.294599+00:00 app[web.1]: import(模块) 2018-12-02T04:24:16.294601+00:00 app[web.1]:文件“/app/djangoProject/wsgi.py”,第 15 行,在 2018-12-02T04:24:16.294602+00:00 应用程序 [web.1]: 应用程序 = get_wsgi_application() 2018-12-02T04:24:16.294604+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/core/wsgi.py”,行12、在get_wsgi_application中 2018-12-02T04:24:16.294605+00:00 应用 [web.1]: django.setup(set_prefix=False) 2018-12-02T04:24:16.294607+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/init .py",第 19 行,在设置中 2018-12-02T04:24:16.294608+00:00 app[web.1]:configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) 2018-12-02T04:24:16.294610+00:00 app[web.1]: 文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 57 行,在 getattr 中 2018-12-02T04:24:16.294611+00:00 app[web.1]: self._setup(name) 2018-12-02T04:24:16.294612+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 44 行,在 _setup 中 2018-12-02T04:24:16.294614+00:00 app[web.1]: self._wrapped = Settings(settings_module) 2018-12-02T04:24:16.294615+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 107 行,在 init 中 2018-12-02T04:24:16.294617+00:00 app[web.1]: mod = importlib.import_module(self.SETTINGS_MODULE) 2018-12-02T04:24:16.294618+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/importlib/init.py”,第 127 行,在 import_module 中 2018-12-02T04:24:16.294619+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level) 2018-12-02T04:24:16.294621+00:00 app[web.1]:文件“/app/djangoProject/settings.py”,第 27 行,在 2018-12-02T04:24:16.294622+00:00 app[web.1]: DEBUG = config('DEBUG', default=False, cast=bool) 2018-12-02T04:24:16.294657+00:00 app[web.1]: NameError: name 'config' is not defined 2018-12-02T04:24:16.294918+00:00 app[web.1]: [2018-12-02 04:24:16 +0000] [10] [INFO] 工人退出(pid:10) 2018-12-02T04:24:16.298282+00:00 app[web.1]: [2018-12-02 04:24:16 +0000] [11] [ERROR] 工作进程中的异常 2018-12-02T04:24:16.298285+00:00 app[web.1]:回溯(最近一次通话最后): 2018-12-02T04:24:16.298287+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 583 行,在 spawn_worker 2018-12-02T04:24:16.298288+00:00 应用 [web.1]: worker.init_process() 2018-12-02T04:24:16.298290+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py”,行129,在init_process中 2018-12-02T04:24:16.298291+00:00 应用 [web.1]: self.load_wsgi() 2018-12-02T04:24:16.298293+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py”,行138,在 load_wsgi 2018-12-02T04:24:16.298294+00:00 应用 [web.1]: self.wsgi = self.app.wsgi() 2018-12-02T04:24:16.298296+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py”,行67,在wsgi中 2018-12-02T04:24:16.298298+00:00 应用 [web.1]: self.callable = self.load() 2018-12-02T04:24:16.298299+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py”,行52,负载中 2018-12-02T04:24:16.298301+00:00 应用程序 [web.1]:返回 self.load_wsgiapp() 2018-12-02T04:24:16.298302+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py”,行41、在load_wsgiapp中 2018-12-02T04:24:16.298304+00:00 应用程序[web.1]:返回 util.import_app(self.app_uri) 2018-12-02T04:24:16.298305+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py”,第 350 行,在 import_app 2018-12-02T04:24:16.298307+00:00 app[web.1]: import(模块) 2018-12-02T04:24:16.298308+00:00 app[web.1]:文件“/app/djangoProject/wsgi.py”,第 15 行,在 2018-12-02T04:24:16.298310+00:00 应用程序 [web.1]: 应用程序 = get_wsgi_application() 2018-12-02T04:24:16.298311+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/core/wsgi.py”,行12、在get_wsgi_application中 2018-12-02T04:24:16.298313+00:00 app[web.1]: django.setup(set_prefix=False) 2018-12-02T04:24:16.298314+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/init .py",第 19 行,在设置中 2018-12-02T04:24:16.298316+00:00 app[web.1]:configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) 2018-12-02T04:24:16.298317+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 57 行,在 getattr 中 2018-12-02T04:24:16.298319+00:00 app[web.1]: self._setup(name) 2018-12-02T04:24:16.298320+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 44 行,在 _setup 中 2018-12-02T04:24:16.298322+00:00 app[web.1]: self._wrapped = Settings(settings_module) 2018-12-02T04:24:16.298323+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/django/conf/init.py",第 107 行,在 init 中 2018-12-02T04:24:16.298325+00:00 应用程序 [web.1]: mod = importlib.import_module(self.SETTINGS_MODULE) 2018-12-02T04:24:16.298326+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/importlib/init.py”,第 127 行,在 import_module 中 2018-12-02T04:24:16.298328+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level) 2018-12-02T04:24:16.298329+00:00 app[web.1]:文件“/app/djangoProject/settings.py”,第 27 行,在 2018-12-02T04:24:16.298331+00:00 app[web.1]: DEBUG = config('DEBUG', default=False, cast=bool) 2018-12-02T04:24:16.298365+00:00 app[web.1]: NameError: name 'config' is not defined 2018-12-02T04:24:16.298670+00:00 app[web.1]: [2018-12-02 04:24:16 +0000] [11] [INFO] 工人退出(pid:11) 2018-12-02T04:24:16.379203+00:00 app[web.1]:回溯(最近一次通话最后): 2018-12-02T04:24:16.379212+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 210 行,运行中 2018-12-02T04:24:16.379635+00:00 应用 [web.1]: self.sleep() 2018-12-02T04:24:16.379667+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 360 行,在睡眠中 2018-12-02T04:24:16.380038+00:00 app[web.1]: 准备好 = select.select([self.PIPE[0]], [], [], 1.0) 2018-12-02T04:24:16.380069+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 245 行,在句柄_chld 2018-12-02T04:24:16.380350+00:00 应用 [web.1]: self.reap_workers() 2018-12-02T04:24:16.380384+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 525 行,在 reap_workers 2018-12-02T04:24:16.380791+00:00 应用程序 [web.1]:提高 HaltServer(原因,self.WORKER_BOOT_ERROR) 2018-12-02T04:24:16.380959+00:00 应用 [web.1]: gunicorn.errors.HaltServer: 2018-12-02T04:24:16.380986+00:00 应用[web.1]: 2018-12-02T04:24:16.380988+00:00 app[web.1]:在处理上述异常的过程中,发生了另一个异常: 2018-12-02T04:24:16.380990+00:00 应用[web.1]: 2018-12-02T04:24:16.381018+00:00 app[web.1]:回溯(最近一次通话): 2018-12-02T04:24:16.381048+00:00 app[web.1]:文件“/app/.heroku/python/bin/gunicorn”,第 11 行,在 2018-12-02T04:24:16.381251+00:00 应用 [web.1]: sys.exit(run()) 2018-12-02T04:24:16.381282+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py”,行61,运行中 2018-12-02T04:24:16.381500+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() 2018-12-02T04:24:16.381539+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py”,行223,运行中 2018-12-02T04:24:16.381822+00:00 app[web.1]: super(Application, self).run() 2018-12-02T04:24:16.381852+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py”,行72,运行中 2018-12-02T04:24:16.382095+00:00 应用程序 [web.1]: 仲裁者(self).run() 2018-12-02T04:24:16.382125+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 232 行,运行中 2018-12-02T04:24:16.382399+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status) 2018-12-02T04:24:16.382428+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 345 行,停顿 2018-12-02T04:24:16.382782+00:00 应用 [web.1]: self.stop() 2018-12-02T04:24:16.382812+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 393 行,停止 2018-12-02T04:24:16.383169+00:00 应用 [web.1]: time.sleep(0.1) 2018-12-02T04:24:16.383199+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 245 行,在句柄_chld 2018-12-02T04:24:16.383475+00:00 应用 [web.1]: self.reap_workers() 2018-12-02T04:24:16.383505+00:00 app[web.1]:文件“/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py”,第 525 行,在 reap_workers 2018-12-02T04:24:16.383912+00:00 应用程序 [web.1]:提高 HaltServer(原因,self.WORKER_BOOT_ERROR) 2018-12-02T04:24:16.384052+00:00 应用 [web.1]: gunicorn.errors.HaltServer: 2018-12-02T04:24:16.525711+00:00 heroku[web.1]:状态从最多变为崩溃 2018-12-02T04:24:16.509117+00:00 heroku[web.1]:进程以状态 1 退出 2018-12-02T04:26:07.329462+00:00 heroku[路由器]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=a5pire-django-framework.herokuapp .com request_id=bdd71320-3817-48e6-8667-78b6e4bc3c4b fwd="125.239.63.128" dyno= connect= service= status=503 bytes= protocol=https
非常感谢任何帮助。非常感谢。
【问题讨论】:
你的 procfile 是什么样的? 只有一行:web: gunicorn djangoProject.wsgi --log-file - 由于问题似乎与 gunicorn 有关,您可以尝试 web: gunicorn djangoProject.wsgi。省略--log-file。我的 procfile 就是这样,构建成功。 感谢大家的建议。将尝试修改 Procfile 并报告。 【参考方案1】:我解决了这个问题:
1) 删除你是旧的requirement.txt 2) 使这个 Cmd pip freeze > requirements.txt 3) 混帐添加 -A 4) git commit -m "最后修改" 5) git push heroku master
6) 最后打开 heroku
【讨论】:
以上是关于部署到 Heroku 时 Django 应用程序崩溃 - Worker 无法启动的主要内容,如果未能解决你的问题,请参考以下文章
当我将 Django 应用程序部署到 Heroku 时,为啥 collectstatic 不会自动运行?
当我将django应用程序部署到heroku时,该站点停止工作
数据库更改时,Heroku 部署的 Django 网页不会更新