Django Static File Hosting an Apache
Solution 1
Figured it out. I had an apache config error on this line:
Alias /static/ /home/daifotis/code/feris/sitestatic
I should have written static without the trailing slash. With the trailing slash Apache will not expand the URL path.
Alias /static /home/daifotis/code/feris/sitestatic
Solution 2
I'm assuming you're using Django 1.3, and the 'new' way of serving static files. Could you please show us the settings in your settings.py
file relating to MEDIA and STATIC? Both the ROOT AND URL versions. Also show your STATICFILES_DIRS.
The most likely cause is that Django is only configured to serve static files with contrib.staticfiles
. It's possible that you'll need to run the management command python manage.py collectstatic
to collect all your application static files into a directory ready to be served by apache.
See my answer here regarding settings for using django.contrib.staticfiles
.
If your static files exist, 100%, at the directory pointed to by Alias /static/ /home/daifotis/code/feris/sitestatic
, then your permissions for apache are probably configured incorrectly. Have you checked the /var/log/httpd/error_log
file to see if there is an IOPermissions error when trying to serve the static content?
Related videos on Youtube
themaestro
I have professionally worked in Python, SQL, R, C#, and VB (in Excel). I have also had extensive experience in C/C++, Assembly, and Java on my own time. I'm currently the Chief Risk Officer at Even Responsible Finance.
Updated on July 09, 2022Comments
-
themaestro almost 2 years
I'm trying to move a Django site I have been working on out of the dev server stage and into a real hosting environment. For the time being, I'm just hosting on my personal machine. I already have Apache and mod-wsgi installed, but I'm having issues getting static files up. I'm pretty sure it has to do with Apache. Here is my config file for the site:
<VirtualHost *:80> ServerName localhost ServerAlias daifotis.dyndns.org ServerAdmin [email protected] DocumentRoot /home/daifotis/code/ Alias /media/ /home/daifotis/code/feris/sitestatic Alias /static/ /home/daifotis/code/feris/sitestatic #AliasMatch ^/([^/]*\.css) /home/daifotis/code/feris/sitestatic/$1 <Directory /home/daifotis/code/feris/sitestatic> Order allow,deny Allow from all </Directory> <Directory /home/daifotis/code/feris> Order allow,deny Allow from all </Directory> <Directory /home/daifotis/code/feris/jobsite> Order allow,deny Allow from all </Directory> WSGIDaemonProcess feris processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup feris WSGIScriptAlias / /home/daifotis/code/feris/apache/django.wsgi <Directory /home/daifotis/code/feris/apache> Order allow,deny Allow from all </Directory> </VirtualHost>
I'm trying to host the files from the directory I alias with static. When I try to load the site, all the content comes up but no css. Also, when I hit my url www.server.com/static/, the page displays with the proper content of the directory. What I don't understand though, is why if I click on a link to view a file, it says that URL does not exist. I've been stuck on this for awhile so any help would be much appreciated.
-
Josh Smeaton about 13 yearsWhat version of Django are you using? Are you using contrib.staticfiles?
-
themaestro about 13 yearsSo dumb, I had an extra slash after the static directory alias in my apache config! shakes head
-
-
aterrel about 11 yearsGoodness, thanks for posting! I just spent hours tracking this down and about to give up.
-
Ljubisa Livac about 10 yearsSame with me.. they really should change that thing in official docs
-
Alex Jolig over 5 yearsWell I tried every posibilities with trailing slashes and without them on both sides, and nothing chnaged
-
frostbyyte almost 5 yearsYou are amazing for posting this.