AWS beanstalk + Django:502 错误网关 - ModuleNotFoundError:没有名为“应用程序”的模块

Nat*_*Les 11 python django amazon-web-services amazon-elastic-beanstalk

我正在尝试将 Django 项目部署到 Elastic Beanstalk。我正在遵循他们的指南:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html 但毕竟eb open页面返回“502 Bad Gateway”。

我的 AWS 管理员将 django 环境显示为“降级”。请看截图:在此输入图像描述

我的日志似乎带有“ModuleNotFoundError:没有名为“应用程序”的模块”:

----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Jan 27 19:30:21 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Jan 27 19:30:21 ip-172-31-32-137 web: ModuleNotFoundError: No module named 'application'
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3483] [INFO] Worker exiting (pid: 3483)
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3476] [INFO] Shutting down: Master
Jan 27 19:30:21 ip-172-31-32-137 web: [2021-01-27 19:30:21 +0000] [3476] [INFO] Reason: Worker failed to boot.
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Starting gunicorn 20.0.4
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Listening at: http://127.0.0.1:8000 (3488)
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3488] [INFO] Using worker: threads
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3495] [INFO] Booting worker with pid: 3495
Jan 27 19:30:22 ip-172-31-32-137 web: [2021-01-27 19:30:22 +0000] [3495] [ERROR] Exception in worker process
Jan 27 19:30:22 ip-172-31-32-137 web: Traceback (most recent call last):
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
Jan 27 19:30:22 ip-172-31-32-137 web: worker.init_process()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
Jan 27 19:30:22 ip-172-31-32-137 web: super().init_process()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
Jan 27 19:30:22 ip-172-31-32-137 web: self.load_wsgi()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
Jan 27 19:30:22 ip-172-31-32-137 web: self.wsgi = self.app.wsgi()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Jan 27 19:30:22 ip-172-31-32-137 web: self.callable = self.load()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
Jan 27 19:30:22 ip-172-31-32-137 web: return self.load_wsgiapp()
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
Jan 27 19:30:22 ip-172-31-32-137 web: return util.import_app(self.app_uri)
Jan 27 19:30:22 ip-172-31-32-137 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
Jan 27 19:30:22 ip-172-31-32-137 web: mod = importlib.import_module(module)
Jan 27 19:30:22 ip-172-31-32-137 web: File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
Jan 27 19:30:22 ip-172-31-32-137 web: return _bootstrap._gcd_import(name[level:], package, level)
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Jan 27 19:30:22 ip-172-31-32-137 web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Jan 27 19:30:22 ip-172-31-32-137 web: ModuleNotFoundError: No module named 'application'
Run Code Online (Sandbox Code Playgroud)

我猜想wsgi配置可能有问题。

django.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo.wsgi
Run Code Online (Sandbox Code Playgroud)

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')

application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)

配置.yaml

branch-defaults:
  main:
    environment: django-env
    group_suffix: null
global:
  application_name: todo
  branch: null
  default_ec2_keyname: null
  default_platform: Python 3.7 running on 64bit Amazon Linux 2
  default_region: eu-north-1
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: eb-cli
  repository: null
  sc: git
  workspace_type: Application

Run Code Online (Sandbox Code Playgroud)

非常感谢一些帮助!

小智 1

根据您喜欢部署的 Amazon Linux 版本,Django 配置会有所不同。

如果您要将应用程序部署到Amazon Linux 2

.ebextensions/django.conf

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: todo.settings
  "aws:elasticbeanstalk:environment:proxy:staticfiles":
    "/static/": "todo/static/"
Run Code Online (Sandbox Code Playgroud)

如果您要将应用程序部署到旧版本:

.ebextensions/django.conf

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: todo/wsgi
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: todo.settings
  "aws:elasticbeanstalk:environment:proxy:staticfiles":
    "/static/": "todo/static/"
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您可能想使用 AWS 控制台检查 Elastic beanstalk 配置(容器选项)中的 WSGIPath:

EBS 容器选项