Rk..
Rk..

Reputation: 753

Static Files not served via Apache for Django Application

I have spend the entire day on this. I couldn't get my static files served. My Django application is working fine. I am using: Python 3.4, Django 1.8.13, mod_wsgi 4.5.2. I have solved most of the errors in my httpd.conf using other questions in SO. What more is required to make this work.

httpd.conf

ServerRoot "/home/rajkumar2014/webapps/allure/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so
LoadModule alias_module      modules/mod_alias.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/rajkumar2014/logs/user/access_allure.log combined
ErrorLog /home/rajkumar2014/logs/user/error_allure.log

Listen 21188
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIRestrictEmbedded On
WSGILazyInitialization On

<VirtualHost *:21188>

    Alias /media "/home/webapps/allure_media"
    Alias /static "/home/webapps/allure_static"

    <Directory /home/rajkumar2014/webapps/allure_media>
    Require all granted
    </Directory>

    <Directory /home/rajkumar2014/webapps/allure_static>
    Require all granted
    </Directory>

    WSGIDaemonProcess allure processes=2 threads=12 python-path=/home/rajkumar2014/webapps/allure/allure:/home/rajkumar2014/webapps/allure/lib/python3.4/site-packages:/home/rajkumar2014/webapps/allure/lib/python3.4
    WSGIProcessGroup allure

    WSGIScriptAlias / /home/rajkumar2014/webapps/allure/allure/allure/wsgi.py
    <Directory /home/rajkumar2014/webapps/allure/apache2>
    Require all granted
    </Directory>

</VirtualHost>

settings.py

STATIC_URL = '/static/'

STATIC_ROOT = '/home/rajkumar2014/webapps/allure_static/'

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

# Media files (All user uploaded content)

MEDIA_ROOT = '/home/rajkumar2014/webapps/allure_media/'

MEDIA_URL = '/media/'

Kindly help.

Upvotes: 0

Views: 694

Answers (2)

RaZ
RaZ

Reputation: 1296

I used to have trouble serving static file with Webfaction, depending on the way I was installing Django (using the webfaction installer you can setup either a full django app or just an apache/mod_wsgi app where you can deploy your django release).

Webfaction has an extra nginx process running at root level for serving static files. So you should check the access control lists - ACLs.

For me this ACLs used to be the problem, nginx user had no read access to the static folder.

So maybe you can login to UNIX terminal on webfaction and run getfacl (you should get something similar to this output):

[rajkumar2014@webXXX ~]$getfacl /home/rajkumar2014/webapps/allure_static/

# file: home/rajkumar2014/webapps/allure_static/
# owner: rajkumar2014
# group: rajkumar2014
user::rwx
user:apache:r-x
user:nginx:r-x
group::rwx
mask::rwx
other::r-x

[rajkumar2014@webXXX ~]$ getfacl /home/rajkumar2014/webapps/allure/allure/allure/wsgi.py
getfacl: Removing leading '/' from absolute path names
# file: home/rajkumar2014/webapps/allure/allure/allure/wsgi.py
# owner: rajkumar2014
# group: rajkumar2014
user:apache:r-x
user:nginx:r-x
group::rw-
mask::rwx
other::--

The ideea is that the apache user and the nginx user should have read access to your static folder and also to the wsgi.py (if it was uploaded from an external source - your laptop).

Also check for errors and references to your static folder in the logs:

  • Frontend Logs, the nginx process serving static; location:

    /home/rajkumar2014/logs/frontend/

  • User Logs, for user installed applications (like Django)

    /home/rajkumar2014/logs/user/

I would be interested to know:

  • did you use the Webfaction Django installer?
  • did you create a webfaction app to serve static assets?
  • can you find references to your static folder in the frontend logs?
  • you have an old (Centos 6.5, Apache 2.2) or new (Centos 7, Apache 2.4) Webfaction plan?

More about this in the Webfaction Docs

Upvotes: 0

max
max

Reputation: 3716

i'm new to djano/python myself and fought this battle last month for my first django app ... im not a server admin so i can't say what exactly you're doing wrong but it seems your syntax is bit different than what i did , i have it like

 Alias /static/ /home/ksjdsd3/public_html/mysite/static/
<Directory /home/ksjdsd3/public_html/mysite/static>
  Require all granted
</Directory>

most notably there is a / after static and path to directory is not in the quotation

Upvotes: 2

Related Questions