I have a Django instance set up on an Apache server running through uWSGI hunky dory. Everything looks perfect on my local machine when I use python manage.py runserver
. Unfortunately, the minute I uploaded the files to my pre-prepared production server, it refused to load all of Django's built-in static files, returning a 404 and I have no idea why...
The url that is being queried is listed as www.mysite.com/static/admin/css/fonts.css
and is similar for the admin backend(css/base.css
,css/dashboard.css
andcss/responsive.css
). At first I thought it might be something wrong with the packages so I force-reinstalled Django but no go. Still not loading...
Here is my uWSGI config .ini:
[uwsgi]
chdir = /path/to/top/level/mysite
module = mysite.wsgi:application
env = DJANGO_SETTINGS_MODULE=mysite.settings
master = true
pidfile = /path/to/project.pid
socket = 127.0.0.1:49152
; Dont use UNIX sockets as it confuses the proxy and alters the request
: URL's. Current Apache version cant handle it.
; socket=/var/run/swerth_django.sock
processes = 5
harakiri = 20
post-buffering = 1
max-requests = 5000
vacuum = true
home = /path/to/top/level/Virtualenv/directory
daemonize = /path/to/uWSGI.log
env = LANG=en_US.UTF-8
enable-threads = true
And I have included Static root and url as follows (comments were for my own understanding):
#The URL of which the static files in STATIC_ROOT directory are served
STATIC_URL = '/static/'
#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = '/home/swerth/public_html/swerth/static'
It is worth mentioning however, I do not have any static files of my own as of yet. I am not at that point in development. I simply want to use the templates shipped with Django.
Edit:
Ok, so I did some research and managed to narrow it down a bit (Thank you to @Alasdair for pointing out a particular page in the Django docs). It seems that I can run python manage.py collectstatic
just fine and it imports all the files I need into www.mysite.com/static
. For some reason however, django dosnt seem to be 'seeing' the files as they don't render in browser (404 not found).
In light of this I am assuming its my config in settings.py
?
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
#The URL of which the static files in STATIC_ROOT directory are
#served
STATIC_URL = '/static/'
#The absolute path to the directory where ./manage.py collectstatic
# will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
My Apache is set up as a reverse proxy using the following config:
RewriteEngine off
ProxyPreserveHost on
ProxyPass "/" "uwsgi://127.0.0.1:49152/"
ProxyPassReverse "/" "uwsgi://127.0.0.1:49152/"
Am I doing this wrong or something? (P.s. I am trying to avoid having to edit the HTML files static file link as I really would prefer to know WHAT I'm doing wrong so it doesn't happen again since Django should be able to serve straight from www.mysite.com/static
)
Edit Round 2:
Ok, so I did as suggested and tried to set up my proxy to exclude the static directory and just serve the files normally and added an alias to map the URL to the actual directory but it still isn't working properly. It seems to completely ignore my ProxyPass
exception and sends it off to Django even though I included it above the less specific rule? Interesting issue, when I specify /admin/
instead of just /
for the less-specific ProxyPass
, it only proxies that URL to Django, however, all of a sudden Django kicks up a 404 for /admin/
. Additionally, I get a 403
forbidden for the static files I was trying to serve?!?! My current .conf looks like this:
Alias /static/ /home/swerth/public_html/swerth/static/
<Directory /home/swerth/public_html/swerth/static/>
Require all granted
</Directory>
<Directory /home/swerth/public_html/swerth/swerth/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ProxyPreserveHost on
ProxyPass /whm-server-status/ !
ProxyPass /static/ !
ProxyPass / uwsgi://127.0.0.1:49152/
ProxyPassReverse / uwsgi://127.0.0.1:49152/