Django doesn't find CSS files for admin pages using uWSGI

20,936

Solution 1

Official deployment docs (independently by the WSGI server) do not cover serving static files (that is generally managed by the webserver). The right docs are here:

https://docs.djangoproject.com/en/dev/howto/static-files/deployment/

eventually serving static files is pretty easy with uWSGI:

http://uwsgi-docs.readthedocs.org/en/latest/StaticFiles.html

but if you can do it in nginx it is better

Solution 2

I have the same problem. My nginx server on Centos 7.6 can't access to static folder in path /home/user/app/mysyte/static/. In /var/log/nginx/error.log same error

open() "/home/user/app/mysyte/static/*.css" failed (13: Permission denied)

For solving and understanding this problem :=*

  1. run command getenforce
  2. if enforcing - cat /var/log/audit/audit.log | grep nginx

for me string with errrors looks like

type=AVC msg=audit(1558033633.723:201): avc:  denied  { read } for  pid=7758 comm="nginx" name="responsive.css" dev="dm-0" ino=17312394 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0
type=SYSCALL msg=audit(1558033633.723:201): arch=c000003e syscall=2 success=no exit=-13 a0=564f710dd55d a1=800 a2=0 a3=68632f656d6f682f items=0 ppid=7757 pid=7758 auid=4294967295 uid=998 gid=996 euid=998 suid=998 fsuid=998 egid=996 sgid=996 fsgid=996 tty=(none) ses=4294967295 comm="nginx" exe="/usr/sbin/nginx" subj=system_u:system_r:httpd_t:s0 key=(null)

copy id of audit msg 1558033633.723:201

  1. run command grep yours_audit_id /var/log/audit/audit.log | audit2why

output for me

[root@uwsgi ~]# grep 1558034479.384:221 /var/log/audit/audit.log | audit2why
type=AVC msg=audit(1558034479.384:221): avc:  denied  { read } for  pid=7758 comm="nginx" name="responsive.css" dev="dm-0" ino=17312394 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0

        Was caused by:
        The boolean httpd_read_user_content was set incorrectly.
        Description:
        Allow httpd to read user content

        Allow access by executing:
        # setsebool -P httpd_read_user_content 1

So as you can see answer here setsebool -P httpd_read_user_content 1 when you run this command you see your static content

Share:
20,936

Related videos on Youtube

Marco Sulla
Author by

Marco Sulla

Updated on September 18, 2022

Comments

  • Marco Sulla
    Marco Sulla over 1 year

    Even if I followed the official instructions, when I start a Django test site using uWSGI, CSS files for the admin interface are not loaded. If I open the URL of a CSS file, for example http://localhost:8443/static/admin/css/base.css, I get a 404 error. I searched for the local file and I guess its path is /usr/local/lib/python3.3/dist-packages/django/contrib/admin/static/admin/css/base.css; so I also tried to run uwsgi as root, but nothing changed.

    I have no problem using python3 manage.py runserver. If I open http://localhost:8000/static/admin/css/base.css, the file is loaded in the browser, and the style is applied to the admin page.

    This is the command I execute in bash:

    uwsgi --ini ~/.uwsgi/conf/django.ini --set-placeholder project_name=mysite --set-placeholder port=8443
    

    and this is the content of django.ini:

    [uwsgi]
    module = %(project_name).wsgi:application
    https = :%(port),/usr/local/nginx/conf/server.crt,/usr/local/nginx/conf/server.key,HIGH
    strict = true
    chdir = /home/marco/django-projects/%(project_name)
    env = DJANGO_SETTINGS_MODULE=%(project_name).settings
    socket = /home/marco/.uwsgi/%(project_name).socket
    pidfile = /home/marco/.uwsgi/%(project_name).pid
    daemonize = /home/marco/.uwsgi/%(project_name).log
    master = true
    enable-threads = true
    harakiri = 20
    max-requests = 5000
    vacuum = true
    
  • Marco Sulla
    Marco Sulla over 10 years
    Thank you, I also followed this simple tutorial: uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.h‌​tml , even if it does not remember to add to PROJECT/settings.py the line STATIC_ROOT = os.path.join(BASE_DIR, "static")and run python3 manage.py collectstatic.