1

EDITED TO SHOW UPDATED CONFIGURATION

No static files are being shown in Production. Files are showing correctly in Development

settings.py

BASE_DIR = os.path.dirname(__file__)

STATIC_ROOT = '/opt/soundshelter/static/'

print "Base Dir: " + BASE_DIR #this returns /opt/soundshelter/soundshelter/soundshelter
print "Static Root: " + STATIC_ROOT #this returns /opt/soundshelter/static/


STATIC_URL = '/static/'

STATICFILES_DIRS = (

    os.path.join(BASE_DIR, 'static'),

) #/opt/soundshelter/soundshelter/soundshelter/static

Files are being called in the applications with <link href="{% static "css/portfolio-item.css" %}" rel="stylesheet">

Using Nginx and Gunicorn.

Nginx config:

server {
    server_name 46.101.56.50;

    access_log yes;

    location /static {
        autoindex       on;
        alias /opt/soundshelter/static/;
}

    location / {

        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }

#       error_log /opt/soundshelter/soundshelter/soundshelter/nginx-error.log;

}

Running python manage.py collectstatic reports files were successfully copied but are still not being displayed.

Deployment handled by Fabric

from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm

env.hosts = []

print "Here we go..."

def commit():
    local("git add . && git commit")

def push():
    local("git push origin master")

def prepare_deploy():

    commit()
    push()

def deploy():
    code_dir = '/opt/soundshelter/soundshelter/'
    with cd(code_dir):
        run("git pull")
        run("python manage.py collectstatic -v0 --noinput")
        run("service nginx restart")
        run("ps aux |grep gunicorn |xargs kill -HUP")
        run("gunicorn -b PROD_IP soundshelter.wsgi:application")

commit()
push()
deploy()
Franco
  • 2,846
  • 7
  • 35
  • 54
  • 3
    How are you serving those files to begin with? Using the `staticfiles` app? It its default configuration, it only works in `DEBUG` mode. You might want to [start with the docs](https://docs.djangoproject.com/en/1.8/howto/static-files/). – Thomas Orozco Oct 10 '15 at 07:51
  • 1
    @Franco no, you didn't. You haven't said anything at all about how you're serving these files. – Daniel Roseman Oct 10 '15 at 07:56
  • misread the comment, updated question now – Franco Oct 10 '15 at 08:05
  • 1
    By the way, why your `settings.py` file is located in static folder in dev? It should not be there. Moreover, based on that, your BASE_DIR is `/static/`... – chem1st Oct 10 '15 at 08:32
  • @chem1st typo :) updated now – Franco Oct 10 '15 at 08:33
  • 2
    Add this in your **nginx conf**, `error_log /opt/soundshelter/soundshelter/soundshelter/nginx-error.log;`. And then check the error log. – JRodDynamite Oct 10 '15 at 08:40
  • @JRodDynamite No errors actually being reported upon running... – Franco Oct 11 '15 at 05:31

3 Answers3

1

First of all, it seems to me that your edited STATICFILES_DIRS points to the wrong folder, since you have /static/ folder, not /staticfiles/. Should be as it was originally:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Second, STATIC_ROOT should point to the static folder which will be served by webserver in pro (but preferably not in project folder). In your case:

STATIC_ROOT="/opt/soundshelter/soundshelter/soundshelter/static/"

I usually place static folder near the project one and use dynamic STATIC_ROOT instead of hardcoding:

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
#this will alow to collect your static files in /opt/soundshelter/soundshelter/staticfiles/static
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles/static')

Now you should do collectstatic and it will collect all static files in the STATIC_ROOT directory.

chem1st
  • 1,624
  • 1
  • 16
  • 22
  • thanks. I followed this exactly but unfortunately it didn't work. Same issue. – Franco Oct 10 '15 at 09:17
  • One more time, your `STATIC_ROOT` points at `/opt/soundshelter/static/` but printing `STATIC_ROOT` results in `/opt/soundshelter/soundshelter/staticfiles/static`. How this can be? What I recommend you is to set your `STATICFILES_DIRS` and `STATIC_ROOT` as I showed in my answer and do `collectstatic` then. Next try to open some of your static files (css file or img) by directly requesting it in the url. Print what you'll get here. – chem1st Oct 11 '15 at 18:12
  • Sorry, that was a typo, updated now to reflect the correct output. Thanks for the comment. – Franco Oct 11 '15 at 21:25
  • Running Collectstatic copies files as expected and viewing files such as http://www.domain.com/js/ajax.js returns a 404 – Franco Oct 11 '15 at 21:27
  • "As expected" means that all files are collected in `/opt/soundshelter/static/`? You get 404 cause you are trying to open a file in a wrong place, should be `domain.com/static/js/ajax.js` – chem1st Oct 11 '15 at 23:02
0

In your Nginx Config did you try to set:

location /static {

instead of

location /static/ {

also make sure the user running nginx has read permissions to the static folder.

Titusz
  • 1,435
  • 1
  • 22
  • 30
  • just tried that and it didn't make any difference, unfortunately. Restarted Nginx after making the change. Also updated read permissions using 'sudo chmod -R 755 /opt/soundshelter/static' – Franco Oct 11 '15 at 08:03
  • I also tried all the variations here http://stackoverflow.com/a/1474427/545966 but nothing worked. – Franco Oct 11 '15 at 08:03
-1

BAD SOLUTION - DO NOT USE IN PRODUCTION

https://docs.djangoproject.com/en/1.8/howto/static-files/

Adding this to urls.py did the trick

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Franco
  • 2,846
  • 7
  • 35
  • 54
  • 1
    No! As it is said in docs: "This is not suitable for production use!". It can be used only during development. – chem1st Oct 10 '15 at 11:38
  • @chem1st Noted and removed from urls.py ......and now it doesn't work again :-) Question updated with current configurations. – Franco Oct 11 '15 at 05:32