Django Static File Hosting an Apache
Asked Answered
D

5

23

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.

Dolorous answered 15/4, 2011 at 22:7 Comment(2)
What version of Django are you using? Are you using contrib.staticfiles?Algerian
So dumb, I had an extra slash after the static directory alias in my apache config! shakes headDolorous
D
52

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
Dolorous answered 16/4, 2011 at 3:17 Comment(4)
Goodness, thanks for posting! I just spent hours tracking this down and about to give up.Goldiegoldilocks
Same with me.. they really should change that thing in official docsMima
Well I tried every posibilities with trailing slashes and without them on both sides, and nothing chnagedStir
You are amazing for posting this.Violetavioletta
A
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?

Algerian answered 16/4, 2011 at 0:36 Comment(0)
R
2

If you are dealing with Proxypass setup.

<VirtualHost *:80>
ServerName example.us

<Proxy *>
    Allow from localhost
</Proxy>

ProxyPreserveHost On

# Exclude /media/ from proxying
ProxyPass /media/ !
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

# Serve static files from /media/ directly
Alias /media/ /path/to/media/
<Directory "/path/to/media">
    Options FollowSymLinks
    Require all granted
</Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.us
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Reredos answered 12/1, 2023 at 14:51 Comment(1)
ProxyPass /media/ ! This part saved me.Dimidiate
D
1

I knew this is the old thread, but in case if anyone still looking for the answer here is step by step procedures(using django 4.1). Sorry for my bad english.

  1. add the static url, static root and static directory to settings.py

     STATIC_URL = '/static/'
     STATIC_ROOT = os.path.join(BASE_DIR, "static")
     STATICFILES_DIRS = os.path.join(BASE_DIR, "static")
    
  2. run python collectstatic command, this will copy all files from your static folders into the STATIC_ROOT directory

     python manage.py collectstatic
    
  3. update the apache config file

     Alias /robots.txt /path/to/your-prj/static/robots.txt
     Alias /favicon.ico /path/to/your-prj/static/favicon.ico
    
     Alias /media/ /path/to/your-prj/media/
     Alias /static/ /path/to/your-prj/static/
    
     <Directory /path/to/your-prj/static>
     Require all granted
     </Directory>
    
     <Directory /path/to/your-prj/media>
     Require all granted
     </Directory>
    
  4. restart apache

Here is the official documentation in case if you want to check.

Demitria answered 1/11, 2022 at 10:5 Comment(0)
A
0

I know it's an old question, but I had the same problem and I've tried everything. It turned out that I enabled my app.conf in my apache but forgot to disable 000-default.conf in my apache server.

Algie answered 23/5, 2021 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.