Django collectstatic命令在AWS Elastic Beanstalk Amazon Linux 2 Python 3平台中失败 [英] Django collectstatic command fails in AWS Elastic Beanstalk Amazon Linux 2 Python 3 platform

查看:46
本文介绍了Django collectstatic命令在AWS Elastic Beanstalk Amazon Linux 2 Python 3平台中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使用Amazon Linux 2 Python 3.7平台将Django应用程序部署到Elastic Beanstalk.

I've been struggling for several days now to deploy my Django application to Elastic Beanstalk using the Amazon Linux 2 Python 3.7 platform.

在设法部署了应用程序之后,我无法运行命令 python3 manage.py collectstatic --noinput ,以便nginx能够提供静态文件并因此拥有所有必需的CSSDjango管理页面的样式.

After managing to deploy the app I can't run the command python3 manage.py collectstatic --noinput in order for nginx to be able to serve the static files and thus have all the necessary CSS styles for the Django Admin page.

现在我有一个名为"static.config"的文件,在具有以下YAML代码的.ebextensions目录中:

Right now I have a file named "static.config" in the .ebextensions directory that has the following YAML code:

container_commands:
  collectstatic:
    command: |
      source $PYTHONPATH/activate
      python3 manage.py collectstatic --noinput

我还有一个名为"01_python.config"的文件,使用以下代码:

I also have a file named "01_python.config" with the following code:

option_settings: 
  "aws:elasticbeanstalk:application:environment": 
    DJANGO_SETTINGS_MODULE: "backend_project.settings" 
    "PYTHONPATH": "/var/app/current:$PYTHONPATH"
  "aws:elasticbeanstalk:container:python": 
    WSGIPath: backend_project.wsgi:application
    NumProcesses: 3
    NumThreads: 20
  "aws:elasticbeanstalk:environment:proxy:staticfiles":
    "/static/": "static/"

这是我在cfn-init.log中看到的堆栈跟踪:

This is the stacktrace I'm seeing in the cfn-init.log:

2020-11-04 14:06:29,217 [ERROR] Command collectstatic (source $PYTHONPATH/activate
python3 manage.py collectstatic --noinput  ) failed
2020-11-04 14:06:29,218 [ERROR] Error encountered during build of postbuild_1_eduvaluer_api: Command collectstatic failed
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command collectstatic failed
2020-11-04 14:06:29,218 [ERROR] -----------------------BUILD FAILED!------------------------
2020-11-04 14:06:29,218 [ERROR] Unhandled exception during build: Command collectstatic failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 171, in <module>
    worklog.build(metadata, configSets)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 129, in build
    Contractor(metadata).build(configSets, self)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 530, in build
    self.run_config(config, worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command collectstatic failed

在阅读此跟踪信息时,我想到的一个问题是:为什么使用python 2执行任何脚本?

One question that comes to my mind when reading this trace is: why is python 2 being used to execute any scripts?

如果有人设法解决此问题,我将非常感谢您的帮助.我可以找到的所有文档和答案都已经过时了(对于Amazon Linux Python 3.6平台).

If anyone managed to solve this issue I would very much appreciate any help. All the documentation and answers I could find where quite outdated (for the Amazon Linux Python 3.6 platform).

我的Django应用程序唯一需要的静态文件是Django Admin页面的样式,因为该应用程序是Angular应用程序的后端.

The only static files my Django app needs are the styles for the Django Admin page as the application is the backend of an Angular application.

提前谢谢!

  1. "PYTHONPATH":"/var/app/current:$ PYTHONPATH".我假设$ PYTHONPATH应该是一个环境变量,但是当我执行命令 env 来查看服务器的环境变量时,PYTHONPATH不是其中之一.

  1. "PYTHONPATH":"/var/app/current:$PYTHONPATH". I assume $PYTHONPATH should be an environment variable but when I execute the command env to see the environment variables of the server, PYTHONPATH is not one of them.

按照注释中的要求,我尝试将SSH转换为EB实例并运行 python3 manage.py collectstatic --noinput 命令.这就是我得到的:

As requested in the comments I tried to ssh into de EB instance and run the python3 manage.py collectstatic --noinput command. This is what I got:

(分段)[ec2-user @ ip-172-31-94-128当前] $ python3 manage.py collectstatic --noinput/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py:639:用户警告:读取/var/app/current/.env时出错-如果您未配置环境分开检查.单独检查环境,".%env_file)追溯(最近一次通话):get_value中的文件"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py",第273行值= self.ENVIRON [var]在 getitem 中的文件"/usr/lib64/python3.7/os.py",第681行从None提高KeyError(key)KeyError:"SECRET_KEY"

(staging) [ec2-user@ip-172-31-94-128 current]$ python3 manage.py collectstatic --noinput /var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py:639: UserWarning: Error reading /var/app/current/.env - if you're not configuring your environment separately, check this. "environment separately, check this." % env_file) Traceback (most recent call last): File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py", line 273, in get_value value = self.ENVIRON[var] File "/usr/lib64/python3.7/os.py", line 681, in getitem raise KeyError(key) from None KeyError: 'SECRET_KEY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    main()
  File "manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/management/__init__.py", line 231, in fetch_command
    settings.INSTALLED_APPS
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
    self._setup(name)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/conf/__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/conf/__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/app/current/backend_project/settings.py", line 45, in <module>
    SECRET_KEY =env("SECRET_KEY")
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py", line 123, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/environ/environ.py", line 277, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

我在运行命令之前激活了虚拟环境,并且知道环境变量(包括SECRET_KEY)已加载,因为当我运行命令/opt/elasticbeanstalk/bin/get-config environment 全部他们中的人被打印在控制台上.这使越来越混乱.

I activated the virtual environment before running the command and I know the environment variables (including the SECRET_KEY) are loaded because when I ran the command /opt/elasticbeanstalk/bin/get-config environment all of them were printed on the console. This keeps getting more and more confusing.

另一件事可能有用的是,所有静态文件似乎都在/static目录中.

Another thing that could be useful is that all the static files seem to be in the /static directory.

在我的settings.py文件中,我具有以下内容:

In my settings.py file I have the following:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

据我了解,这应该表明Django在静态目录中查找静态文件.也许我理解错了,这就是问题所在.

From what I understood this should indicate Django to look for the static files in the static directory. Perhaps I understood wrong and here lies the problem.

推荐答案

嘿,我刚刚使用静态文件成功部署了django应用.这是我的配置.

Hey I just deployed my django app successfully with static files. Here's what my config looks like.

django.config

django.config

container_commands:
  01_collectstatic:
    command: "source $PYTHONPATH/activate && python manage.py collectstatic --noinput"
option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: appname.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

settings.py

settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = 'static'

我认为环境变量适用于ebs环境,而不是EC2实例本身.在ssh进入EC2实例时,我也没有看到PYTHONPATH,但是在Environment>中的ebs环境的环境变量中确实看到了PYTHONPATH.配置>AWS控制台上的软件

The environment variables are for the ebs environment not the EC2 instance itself I think. I also don't see PYTHONPATH when I ssh into the EC2 instance but I do see it in the environment variables for my ebs environment in Environment > Configuration > Software on AWS Console

/var/app/venv/staging-LQM1lest/bin

这是我的文件结构:

┣ .ebextensions/
┃ ┗ django.config
┣ .elasticbeanstalk/
┃ ┗ config.yml
┣ appname/

这篇关于Django collectstatic命令在AWS Elastic Beanstalk Amazon Linux 2 Python 3平台中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆