问题描述
昨天,我创建了此帖: DjangoRestFramework可浏览api在本地与在服务器上部署时看起来不一样?
Yesterday, I created this post: DjangoRestFramework browsable api looks different locally vs when deployed on server?
基本上,当我执行python manage.py runserver
时,出现了以下内容:
Basically, when I did python manage.py runserver
, this showed up:
但是在将其部署到AWS(eb deploy
)之后,这是我访问该站点时看到的内容:
But after I deployed it to AWS (eb deploy
), this is what I see when I access the site:
对以上帖子的回答提到,这是因为我的静态文件丢失了.因此,我搜索了如何在AWS上部署静态文件并遇到了本教程: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-update-app
The answer to the post above mentioned that it is because my static files were missing. So I searched how to deploy static files on AWS and came across this tutorial: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-update-app
在创建站点管理员"部分下,它提到为了提供静态文件,我必须首先在settings.py
中定义STATIC_ROOT
(所以我做了:STATIC_ROOT = os.path.join(BASE_DIR, "ebdjangoapp/static/")
),然后又做了eb deploy
.但是,该站点仍然看起来与第二个图像相同(没有静态文件).然后,我尝试执行python manage.py collectstatic
(这在其中创建了带有rest_framework
目录的静态文件夹,其中包含css
文件等),然后再次执行eb deploy
,但是站点stil看起来与第二个图像相同.
Under the "Create a Site Administrator" section, it mentions that in order to serve static files, I must first define STATIC_ROOT
in settings.py
(so I did: STATIC_ROOT = os.path.join(BASE_DIR, "ebdjangoapp/static/")
) and then I did eb deploy
. However, the site still looks the same as the 2nd image (without static files). I then tried doing python manage.py collectstatic
(this created the static folder with the rest_framework
directory inside it, containing the css
files etc.) and then did eb deploy
again but the site stil looks the same as the 2nd image.
为什么仍然没有显示静态文件?
How come the static files still aren't showing up?
请注意,我四处搜寻并发现了这篇文章:没有部署Django应用加载静态文件,答案为:
Note, I searched around and came across this post: Django app deployment not loading static files and the answer says:
然后,您需要通过选择的Web服务器在settings.STATIC_URL
处提供settings.STATIC_ROOT
服务,通常是nginx作为Apache-mod_wsgi应用服务器后面的反向代理."
"You then need to serve settings.STATIC_ROOT
at settings.STATIC_URL
via your web server of choice, very commonly nginx as a reverse proxy behind your Apache-mod_wsgi app server."
但是我不知道Web服务器(nginx,反向代理,Apache-mod_wsgi)如何工作.我有一个使用python manage.py runserver
在本地运行的Django应用,并且我有AWS Elastic beantalk.我通过执行eb deploy
将Django应用程序部署到AWS.我需要采取什么步骤才能使静态文件出现在部署中(假设我不知道如何配置nginx,反向代理等)??
But I have no idea how web servers (nginx, reverse proxy, Apache-mod_wsgi) works. I have a Django app I run locally with python manage.py runserver
, and I have AWS elastic beanstalk. I deploy my Django app to AWS by doing eb deploy
. What steps do I need to take in order for the static files to appear on deployment (assuming I don't know how to configure nginx, reverse proxy etc.).?
推荐答案
有关于从RealPython将Django应用程序部署到AWS Elastic Beanstalk的权威指南-.它完整介绍了静态文件以及如何使用eb进行配置,而您无需了解有关nginx/apache等的任何信息.
There is definitive guide about deploying a django app to AWS Elastic Beanstalk from RealPython - here it is.It has whole section about static files and how to configure it with eb and you don't need to know anything about nginx/apache etc.
基本上,您应该在eb config中定义container_commands
,这些命令将在应用程序部署完成后执行.例如migrate
和collectstatic
,因此这是eb配置文件中此类部分的示例:
Basically you should define container_commands
in your eb config, these commands will be executed after application deploy is finished. For example migrate
and collectstatic
, so this is an example of such section in eb config file:
container_commands:
01_migrate:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py migrate --noinput"
leader_only: true
02_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python iotd/manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "iotd.settings"
"PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: iotd/iotd/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
请注意aws:elasticbeanstalk:container:python:staticfiles
部分.另外,您还应该在Django设置文件中定义此部分:
Pay attention to aws:elasticbeanstalk:container:python:staticfiles
part.And also you should define this part in your django settings file:
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATIC_URL = '/static/'
我几乎完全从上面的文章中复制了这个示例,您应该检查一下,它很棒.
I copied this example almost entirely from article above, you should really check it, it's awesome.
UPD:如何调试丢失的静态文件.我通常这样做(它涉及到ssh实例):
UPD: how to debug missing staticfiles.I usually do this (it involves sshing to your eb instance):
- 确保
INSTALLED_APPS
已包含在我的INSTALLED_APPS
中. - 在浏览器控制台网址中签入丢失的文件,例如
/static/js/somefile.js
- 确保我的Django设置
STATIC_URL
是相同的,例如/static/
. - 检查
STATIC_ROOT
中的实际值,并检查该文件夹是否实际包含生产服务器中的静态文件. - 检查我的eb配置是否指向正确的文件夹(在配置的
option_settings
部分下)
- Make sure that
django.contrib.staticfiles
is included in myINSTALLED_APPS
. - Check in browser console url to missing file e.g.
/static/js/somefile.js
- Make sure in my django settings
STATIC_URL
is the same e.g./static/
. - Check actual value in
STATIC_ROOT
and check that this folder actually contains your static files in production server. - Check that my eb config is pointing to correct folder (under your
option_settings
section in config)
此外,您还可以尝试将静态信息收集到生产服务器上的/static
目录中(默认情况下,eb会在dir中查找它们).突然,它开始起作用-这意味着您的设置无法覆盖默认设置,您应该检查其他定义位置.
Also you can try to collect static into /static
dir on your production server (it's where eb looks for them by default). If all of a sudden it starts working - it means that your setting failed to override default one and you should check where else it was defined.
我希望这些步骤将帮助您找到正确的方向.
I hope these steps will help you to find right direction.
这篇关于部署Django应用时如何将静态文件提供给AWS("python manage.py collectstatic"无效)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!