Deepend
Deepend

Reputation: 4155

Django static file 404 errors when deployed with mod_wsgi

I am getting 404 errors on all of my static files when I try to deploy them in production

A live page of the site is available here. Inspecting the page in Chromes Console or Firebug will show the below issues.

enter image description here

I cant understand why this is happening as the above 404 errors match perfectly to the locations of my CSS files /var/www/bias_experiment/static/survey/css/bootstrap.css

I've tried to solve this for a few days but cant crack the problem.

Below is all of my setup. I thought it best to include everything that might be useful and all the steps I have taken. Apologies for the length.

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', 
)

I am using STATIC_URL = '/bias_experiment/static/' because I am using a VM which has been made publicly available through http://phaedrus.scss.tcd.ie/bias_experiment/

If I use STATIC_URL = '/static/' I get 403 forbidden errors.

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>

My default apache file /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>

As per the DjangoProject 1.6 Documentation (I am using 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.

I have restarted and reloaded Apache

sudo service apache2 restart sudo service apache2 reload

I have no problem running collectstatic

enter image description here

I have set ownership of the collected files to www-data:www-data with the below command

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

enter image description here

I have even disabled and re-enabled the default file in apache2

sudo a2dissite default

sudo a2ensite default

and then restarted

sudo service apache2 restart sudo service apache2 reload

I am using

An absolute path to one of the CSS files is http://phaedrus.scss.tcd.ie/bias_experiment/static/survey/css/bootstrap.min in case it is of any use.

However I cant seem to get the test deployment page I am working to show its CSS or JS. I really am not sure what I'm leaving out. If anyone can make any suggestions it would be hugely appreciated.

I asked a similar question to this a few days ago which i have deleted as there were too many mistakes on my part and it lacked both detail and a clear issue.

Thanks

Upvotes: 0

Views: 2168

Answers (2)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

For:

STATIC_URL = '/bias_experiment/static/'

in the Django settings module, you should be using:

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

in the Apache configuration.

Upvotes: 2

hamidfzm
hamidfzm

Reputation: 4685

Don't use STATIC_ROOT in STATICFILES_DIRS. They shouldn't be the same. Because when you run python manage.py collectstatic for deployment they will be overwritten and also take a look at this page Managing static files (CSS, images) #settings.py STATIC_URL = '/static/' # STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# Introduce templates
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'somewhereelse'),
    os.path.join(BASE_DIR, 'templates')]

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'dajaxice.finders.DajaxiceFinder',
)

And this code for WSGI works for me

import os
import sys
import pprint
os.environ['DJANGO_SETTINGS_MODULE'] = 'Yourapp.settings'
import django.core.handlers.wsgi
class LoggingMiddleware:
    def __init__(self, application):
        self.__application = application

    def __call__(self, environ, start_response):
        errors = environ['wsgi.errors']
        pprint.pprint(('REQUEST', environ), stream=errors)

        def _start_response(status, headers, *args):
            pprint.pprint(('RESPONSE', status, headers), stream=errors)
            return start_response(status, headers, *args)

        return self.__application(environ, _start_response)

application = LoggingMiddleware(django.core.handlers.wsgi.WSGIHandler())

path = '/var/www/example.com'
if path not in sys.path:
    sys.path.append(path)

Upvotes: 0

Related Questions