VicVer
VicVer

Reputation: 153

Gunicorn does not show Django static files

I've seen this question asked before but none of the responses have helped.

I'm trying to deploy my Python3/Django project to AWS with gunicorn + nginx. The project name is vicver, and when I try to run

 $ gunicorn --bind 0.0.0.0:8000 vicver.wsgi

I can see my site on the AWS public IP port 8000 but without any js/css or images. When I do the same with the runserver command it works fine.

My settings.py contains the following paths:

STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR,'vicver/static') ]

And here's a screenshot of the terminal https://i.sstatic.net/N76Bo.jpg

Upvotes: 0

Views: 591

Answers (2)

Toan Quoc Ho
Toan Quoc Ho

Reputation: 3378

It works when you run runserver command because you're in development mode. But when you want to deploy your application to production env, you should use some of these options to serve static files Deploying static files

Normally I use Nginx to serve static files, and create a reverse proxy to proxy other requests into the app which served by Gunicorn. You can take a look on this Deploying django application to serve your app by using Gunicorn too.

Upvotes: 1

reyhaneh pahlevan
reyhaneh pahlevan

Reputation: 18

You can not serve your static and media file in the production mode(Debug=False). The best way to serve statics and media with this structure is by using Nginx. First, you need to collect your statics using the following command:

python manage.py collectstatic

Then you need to edit your "nginx.conf" file using the following codes. Be careful to replace "/usr/src/app/" with your project path.

server {

    listen 80 backlog=2048;

    location = /favicon.ico { access_log off; log_not_found off;}
    location /static/ {
        alias   /usr/src/app/vicver/static/;
    }
    location /media/ {
        alias  /usr/src/app/vicver/media/;
    }
  .
  .
  .
}

Upvotes: 0

Related Questions