0

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.cssandcss/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/
Community
  • 1
  • 1
Graeme Ford
  • 166
  • 1
  • 2
  • 16
  • 4
    You need to tell Apache to serve your static root. See the Django docs on [serving files](https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/#serving-files) – Alasdair Oct 16 '18 at 11:02
  • 2
    have you already tried to run python manage.py collectstatic on your server? – matyas Oct 16 '18 at 11:05
  • @matyas yes I have. It said it pulled 60 files, 59 of which remained unchanged. – Graeme Ford Oct 16 '18 at 11:22
  • ok then could you please post your apache configuration? The most likely reason is that apache does not serve your static files because of some sort of misconfiguration. – matyas Oct 16 '18 at 11:42
  • You still don't seem to have anything in your Apache config to serve the static files. You want Apache to serve the static files, not Django. You then need to tell Apache not to use the uwsgi server for `/static/`. I think you can use `ProxyPassMatch` for this, but you'll have to search for the exact usage. – Alasdair Oct 16 '18 at 16:06

1 Answers1

1

This is probably because you've not set up Apache to serve your static files. Try doing the following

  1. Go to your 'static' file folder and get the absolute path to that folder using pwd. Looks like /home/swerth/public_html/swerth/static in your case. Also make sure that the user running the Apache httpd's process has read access to the static folder.
  2. Now edit your VirtualHost by doing

    sudo nano /etc/apache2/sites-available/000-default.confsudo nano /etc/apache2/sites-available/000-default.conf Replace 000-default.conf with your own if you're using a different config file.

  3. Add the following to the virtual hosts block:

<VirtualHost *:80>
  . . .

  Alias /static /home/swerth/public_html/swerth/static
  <Directory /home/swerth/public_html/swerth/static>
      Require all granted
  </Directory>

  . . .
</VirtualHost>

Also make sure you place this above your uwsgi <Directory> part 4. Restart apache by doing sudo systemctl restart apache2

This is a good tutorial I found on how to deploy a Django app to production.

Hope this helps.

Bharath
  • 69
  • 5
  • Does the proxy config necessarily have to be in a Virtual Host tag? (Honestly not sure why/when to use it). Also, side question... Does indentation affect the .config file? – Graeme Ford Oct 20 '18 at 05:47
  • I'm in no way an expert at Apache, but have a look at [this SO question](https://stackoverflow.com/questions/10985342/apache-403-while-serving-django-static-files) for a possible solution to your 403 issue with static files. In my experience, nginx feels like a better candidate for Django apps because of it's easy of use and speed. – Bharath Oct 22 '18 at 06:22
  • Think I may have found the issue. Still, have no clue how to fix it though. The 404 is because it's trying to default to 403.shtml, a system 403 page which honestly isn't there. The issue seems to be forbidden access. I took a look in the error log for apache as well. This is where it gets interesting. Apache is looking for .htaccess in my `/public_html/` directory?? Its no-where near there so why is it looking for it there? – Graeme Ford Oct 22 '18 at 09:42