[Django]-Django Static File Hosting an Apache

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

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?

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]

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.

👤ZZA

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.

Leave a comment