Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache is not serving static files from Django app

I don't know what's wrong in my virtualhost for django project but the simply question is no matter what modification I do over this file stills output the same in error log from apache and not load any css or js files, what I can see is that Apache is looking for static and media file in the root web folder: /var/www

[Fri May 30 00:58:08 2014] [error] [client 192.168.1.145] File does not exist: /var/www/static, referer: http://192.168.1.143/dgp/login/

I set up virtual host file as follows:

  WSGIPythonPath /var/www/dgp_python2_7/bin/python2.7:/var/www/dgp_python2_7/lib/python2.7/site-packages
  WSGIScriptAlias /dgp /var/www/dgp/dgp/dgp/wsgi.py

  <VirtualHost 127.0.0.1:80>
      ServerName www.dgp.dev
      ServerAlias dgp.dev


     AliasMatch ^/([^/]*\.css) /var/www/dgp/dgp/static/$1

     Alias /media/ /var/www/dgp/dgp/media/
     Alias /static/ /var/www/dgp/dgp/static/
     Alias /images/ /var/www/dgp/dgp/images/

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

    <Directory /var/www/dgp/dgp/media/>
      Order deny,allow
      Allow from all
    </Directory>

     ErrorLog /var/www/dgp/dgp/error.log
     CustomLog /var/www/dgp/dgp/access.log combined
  </VirtualHost>

And in settings.py STATIC_ROOT with '/var/www/dgp/dgp/static/' where is located all the css content.

How can I tell apache or Django to looking for the proper directory '/var/www/dgp/dgp/static/'? It's driving me crazy, I don't understand how something so elemental in development it's so complex for production.

Regards!

Edit with the solution

The really problem was that I didn't disable the default site for Debian Apache (that is the version I'm working for) and has another method for stablish virtualhost, at beginning we have to disable default site with the follow command: a2dissite defaultand everything works now like a charm!

like image 341
Enot Avatar asked Nov 11 '22 07:11

Enot


1 Answers

You can tell where your static files are being looked for in your project's rendered html. Just view the source in your browser and look for a stylesheet or javascript include, what is the full path to the file?

My guess, you have to run Django's collect static script, which will collect all the static scripts in all of your project's app and put them into one location. This is a core part of deploying Django projects and unavoidable if you use multiple "apps" in your project.

in your terminal go to the Django projects root folder and type this:

python manage.py collectstatic

Read more at https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

like image 163
macguru2000 Avatar answered Nov 15 '22 05:11

macguru2000