2
votes

I'm deploying a Django project using:

  • virtualenv
  • nginx
  • gunicorn

following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps


My configuration

django settings

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATIC_URL = '/static/'

/etc/nginx/sites-available/esmart2

GNU nano 2.0.9                 File: /etc/nginx/sites-available/esmart2

server {
    server_name 192.168.30.17;
    access_log off;

    location /static/ {
        alias /new_esmart/esmart2/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

Running

(esmart_env) [root@eprssrv09 esmart2]# /new_esmart/esmart_env/bin/gunicorn --bind 192.168.30.17:8001 esmart2.wsgi:application

My Django project is running but:

Not Found: /static/admin/css/login.css

Any advice?


UPDATE 1 I think that nginx settings are been avoided:

deleting any reference in 'sites-enabled' folder, django project runs and the problem concerning static files remain.

UPDATE 2 if in Django settings debug=True I get

Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/login.css
Not Found: /static/admin/css/base.css
2
Did you read the static documentation? What are your STATIC* settings? Did you run collectstatic? - Daniel Roseman
@DanielRoseman I've edited my question with django static settings, I've run collectstatic and all static files are in /new_esmart/esmart2/static/ - Massimo Variolo

2 Answers

2
votes

It seems I solved BUT I don't think is the better solution. I mean, it's a good solution depending on the project requirements: mine allows using this solution.


install package

pip install whitenoise

on wsgi.py I added:

from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

This way nginx site conf is ignored (but I think they had already ignored!)

Django WhiteNoise documentation

With a couple of lines of config WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on nginx, Amazon S3 or any other external service. (Especially useful on Heroku, OpenShift and other PaaS providers.)

It’s designed to work nicely with a CDN for high-traffic sites so you don’t have to sacrifice performance to benefit from simplicity.

WhiteNoise works with any WSGI-compatible app but has some special auto-configuration features for Django.

WhiteNoise takes care of best-practices for you, for instance:

Serving compressed content (gzip and Brotli formats, handling Accept-Encoding and Vary headers correctly) Setting far-future cache headers on content which won’t change Worried that serving static files with Python is horribly inefficient? Still think you should be using Amazon S3? Have a look at the Infrequently Asked Questions.

1
votes

It looks to me like the issue is two-fold:

  1. It looks like you may have an error in your nginx config. Is new_esmart/ really in your root directory/ or is it somewhere like /home/sam/new_esmart/? If it is not in your root directory, make sure to provide the complete path.

  2. It looks like your STATIC_ROOT will point to path/to/new_esmart/static/ rather than /new_esmart/esmart2/static/. Keep in mind that STATIC_ROOT is the place where your static files will be copied to when you run collectstatic not the place you personally place static files when creating them initially.

So what you probably need to do is confirm that you really want your static files collected to /actual/path/to/new_esmart/static/ and then use that same path in your nginx config.

Also, as discussed in the comments, you could try adding listen 8001; to your nginx config:

server {
    listen      8001;
    server_name 192.168.30.17;
    access_log off;
    ...