css - 使用 mod_wsgi 部署时 Django 静态文件 404 错误

标签 css django mod-wsgi

当我尝试在生产中部署它们时,我的所有静态文件都出现 404 错误

A live page of the site is available here .在 Chromes 控制台或 Firebug 中检查页面将显示以下问题。

enter image description here

我不明白为什么会发生这种情况,因为上述 404 错误与我的 CSS 文件的位置完全匹配 /var/www/bias_experiment/static/survey/css/bootstrap.css

我已经尝试解决了几天,但无法解决问题。

以下是我的所有设置。我认为最好包括所有可能有用的内容以及我采取的所有步骤。为长度道歉。

settings.py

DEBUG = True

STATIC_ROOT = '/var/www/bias_experiment/static/'
STATIC_URL = '/bias_experiment/static/'

STATICFILES_DIRS = ( 
                    '/var/www/bias_experiment/src/survey/static/survey/css', 
                    '/var/www/bias_experiment/src/survey/static/survey/js', 
                    '/var/www/bias_experiment/src/survey/static/survey/fonts', 
)

我正在使用 STATIC_URL = '/bias_experiment/static/' 因为我正在使用一个通过 http://phaedrus.scss.tcd.ie/bias_experiment/ 公开可用的 VM

如果我使用 STATIC_URL = '/static/' 我会收到 403 禁止错误。

slide_test.html

{% load staticfiles %}      
<!-- THESE WORK LOCALY BUT DO NOT WORK ON THE SERVER -->              
<link rel = "stylesheet" href ="{% static "survey/css/bootstrap.min.css" %}" >
<link rel = "stylesheet" href ="{% static "survey/css/bootstrap.css" %}">               
<link rel="stylesheet" href ="{% static "survey/css/jquery-ui.css" %}">                 
<link rel="stylesheet" href ="{% static "survey/css/slider.css" %}">                

<script src="{% static "survey/js/jquery.min.js" %}"></script>               
<script src="{% static "survey/js/jquery-ui.js" %}"></script>   
<script src="{% static "survey/js/slider.js" %}"></script>

我的默认 apache 文件/etc/apache2/sites-available/default

Alias /static/ /var/www/bias_experiment/static/
<Directory /var/www/bias_experiment/static>
Order deny,allow
Allow from all
</Directory>


WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi
<Directory /var/www/bias_experiment/src/bias_experiment>
<Files index.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>

...
standard apache default settings here
...

</VirtualHost>

根据 DjangoProject 1.6 文档(我使用的是 Apache/2.2.22 (Ubuntu))

If you are using a version of Apache older than 2.4, replace Require all granted with Allow from all and also add the line Order deny,allow above it.

我已经重新启动并重新加载了 Apache

sudo service apache2 restart sudo service apache2 reload

我运行collectstatic没问题

enter image description here

我已使用以下命令将所收集文件的所有权设置为 www-data:www-data

sudo chown -R www-data:www-data /var/www/bias_experiment/static/

enter image description here

我什至禁用并重新启用了 apache2 中的默认文件

sudo a2dissite default

sudo a2ensite default

然后重新启动

sudo service apache2 restart sudo service apache2 reload

我正在使用

  • Ubuntu 12.04
  • Apache 2.22
  • Django 1.6
  • mod_wsgi 版本:3.3-4ubuntu0.1

其中一个 CSS 文件的绝对路径是 http://phaedrus.scss.tcd.ie/bias_experiment/static/survey/css/bootstrap.min万一有用。

但是我似乎无法获得 test deployment page I am working to show its CSS or JS . 我真的不确定我遗漏了什么。如果有人可以提出任何建议,将不胜感激。

几天前我问了一个类似的问题,我已经删除了,因为我的错误太多了,既缺乏细节又缺乏明确的问题。

谢谢

最佳答案

对于:

STATIC_URL = '/bias_experiment/static/'

在 Django 设置模块中,您应该使用:

Alias /bias_experiment/static/ /var/www/bias_experiment/static/

在 Apache 配置中。

关于css - 使用 mod_wsgi 部署时 Django 静态文件 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24965057/

相关文章:

css - vue js v-for循环仅显示元素一次

python - 从模板集命名变量调用 Django 方法。这怎么可能?

django - 使用 Apache WSGI 通过 DJANGO_SETTINGS_MODULE 环境变量设置 Django 设置文件

python - 无法使用 django 和 postgreSQL 调试 psycopg2 的错误消息

Heroku 上的 Django + MongoDB

python - 在 RedHat 上部署 Django 项目

django - Apache 剥离 "Authorization" header

HTML 表格行 - 如何使唯一单元格具有 100% 的宽度? (附截图说明)

css - 960.gs 布局问题

javascript - 我可以获得链接样式表的文本源吗?