5

Everything worked very well before gunicorn and nginx, static files were served to the website. But now, it doesn't work anymore.

Settings.py

STATICFILES_DIRS = [
'/root/vcrm/vcrm1/static/'
]

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

MEDIA_ROOT = '/root/vcrm/vcrm1/vcrm/media/'
MEDIA_URL = '/media/'

/etc/nginx/sites-available/vcrm

server {
listen 80;
server_name 195.110.58.168;

location = /favicon.ico { access_log off; log_not_found off; }
location /static {
    root /root/vcrm/vcrm1/vcrm;
}

location = /media {
    root /root/vcrm/vcrm1/vcrm;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
}

}

When I run collectstatic:

You have requested to collect static files at the destination
location as specified in your settings:

/root/vcrm/vcrm1/vcrm/static

This will overwrite existing files!
Are you sure you want to do this?

and then:

Found another file with the destination path 'admin/js/vendor/jquery/jquery.min.js'. It will be 
ignored since only the first encountered file is collected. If this is not what you want, make sure 
every static file has a unique path.

0 static files copied to '/root/vcrm/vcrm1/vcrm/static', 251 unmodified.
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
NbaRo
  • 83
  • 2
  • 10
  • `STATIC_ROOT` is where collectstatic collects the static files and it should be an empty folder. Apparently, in your case it's not empty. – Pooya Kamranjam Apr 24 '21 at 23:50
  • Can you clarify what you mean by not working? Specifically, are you getting 404, 403, 502, or something else? Also, what is the URL you are requesting? – Mia Apr 25 '21 at 00:59
  • the url is http://195.110.58.168/ you can enter to see. I m not getting any error, just like i see the website but without any static files, like css etc. Please any help ? My configuration looks good ? – NbaRo Apr 25 '21 at 06:11
  • I cannot visit that IP address. In addition, when you said "without any static files", what do you mean by without? Again, what error are you getting? I'm suspecting you are confusing Nginx's `root` and `alias` directives but I cannot tell for sure because I don't know the error type. – Mia Apr 27 '21 at 08:00

3 Answers3

6

NGINX + Gunicorn + Django

Django project:

 djangoapp
 - ...
 - database
 - djangoapp
   - settings.py
   - urls.py
   - ...
 - media
 - static
 - manage.py
 - requirements.txt

Server: install venv, requirements.txt:

sudo apt-get update
sudo apt-get install -y git python3-dev python3-venv python3-pip supervisor nginx vim libpq-dev
--> cd djangoapp
pathon3 -m venv venv
source venv/bin/activate
(venv) pip3 install -r requirements.txt 

Server: install NGINX:

sudo apt-get install nginx
sudo vim /etc/nginx/sites-enabled/default

Server: NGINX config:

   server {
        listen 80 default_server;
        listen [::]:80 default_server;


        location /static/ {
            alias /home/ubuntu/djangoapp/static/; 
        }


        location /media/ {
            alias /home/ubuntu/djangoapp/media/; 
        }


        location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect off;
            add_header P3P 'CP="ALL DSP COR PSAa OUR NOR ONL UNI COM NAV"';
            add_header Access-Control-Allow-Origin *;
        }
}

Server: setup supervisor:

cd /etc/supervisor/conf.d/
sudo vim djangoapp.conf

Server: supervisor config:

[program:djangoapp]
command = /home/ubuntu/djangoapp/venv/bin/gunicorn djangoapp.wsgi  -b 127.0.0.1:8000 -w 4 --timeout 90
autostart=true
autorestart=true
directory=/home/ubuntu/djangoapp 
stderr_logfile=/var/log/game_muster.err.log
stdout_logfile=/var/log/game_muster.out.log

Server: update supervisor with the new process:

sudo supervisorctl reread
sudo supervisorctl update

sudo supervisorctl restart djangoapp
Timeless
  • 104
  • 5
1
  1. Run python manage.py collectstatic from your project directory.
  2. Access nginx's webserver config file and add a location in your config file.
  3. Reload nginx server with sudo systemctl reload nginx.
N1ngu
  • 2,862
  • 17
  • 35
  • I tried your solution but it didn't work because you missed adding these two lines in the main urls.py: 1. from django.contrib.staticfiles.urls import staticfiles_urlpatterns 2. urlpatterns += staticfiles_urlpatterns(). Without the 1. and 2. obviously. – Robin Aug 09 '22 at 04:13
0

I solve the issue after install django package whitenoise.

I tried staticfiles_urlpatterns but only works when Debug = True in settings.py file which you will not for production. and I am not able to update nginx config file.

Wei Jing
  • 613
  • 5
  • 11