简体   繁体   中英

Deploying django app with static content on AWS EB

I am trying to deploy a django app with static files to AWS EB following this guide https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/

I have the standard folder structure with project and app set up. I am using sqlite so no database set up on AWS is required. My python.config file looks like this

container_commands:
  01_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python         manage.py collectstatic --noinput"

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "myappproject.settings"
    "PYTHONPATH": "/opt/python/current/app/myappproject:$PYTHONPATH"
    "ALLOWED_HOSTS": "myappawseb.eu-central-1.elasticbeanstalk.com/"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: "myappproject/wsgi.py"
    NumProcesses: 3
    NumThreads: 20
  "aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"

I previously did pip freeze and have my requirements.txt file in the project folder.

Everything works as planned on localhost. After eb deploy my static files wont load correctly. In my settings.py I defined STATIC_URL and STATIC_ROOT:

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

My eb error log looks like this:

[Fri Oct 27 14:02:35.716529 2017] [:error] [pid 4533]     for pattern in self.url_patterns: 
[Fri Oct 27 14:02:35.716531 2017] [:error] [pid 4533]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__ 
[Fri Oct 27 14:02:35.716533 2017] [:error] [pid 4533]     res = instance.__dict__[self.name] = self.func(instance) 
[Fri Oct 27 14:02:35.716534 2017] [:error] [pid 4533]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/urls/resolvers.py", line 405, in url_patterns 
[Fri Oct 27 14:02:35.716536 2017] [:error] [pid 4533]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) [Fri Oct 27 14:02:35.716546 2017] [:error] [pid 4533]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__ [Fri Oct 27 14:02:35.716553 2017] [:error] [pid 4533]     res = instance.__dict__[self.name] = self.func(instance) [Fri Oct 27 14:02:35.716554 2017] [:error] [pid 4533]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/urls/resolvers.py", line 398, in urlconf_module 
[Fri Oct 27 14:02:35.716556 2017] [:error] [pid 4533]     return import_module(self.urlconf_name) 
[Fri Oct 27 14:02:35.716558 2017] [:error] [pid 4533]   File "/opt/python/run/baselinenv/lib64/python3.4/importlib/__init__.py", line 109, in import_module 
[Fri Oct 27 14:02:35.716560 2017] [:error] [pid 4533]     return _bootstrap._gcd_import(name[level:], package, level) 
[Fri Oct 27 14:02:35.716562 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import 
[Fri Oct 27 14:02:35.716563 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load 
[Fri Oct 27 14:02:35.716565 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked 
[Fri Oct 27 14:02:35.716567 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked 
[Fri Oct 27 14:02:35.716568 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 1129, in _exec 
[Fri Oct 27 14:02:35.716570 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 1471, in exec_module 
[Fri Oct 27 14:02:35.716572 2017] [:error] [pid 4533]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed

最后修复它的是将 settings.py 中的静态根更改为

STATIC_ROOT = os.path.join(BASE_DIR, "static")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM