Django deploy sample config (gunicorn + nginx + supervisord)

This example use:

1supervisord process runner/restarter

2gunicornPython WSGI HTTP Server that will run Django application and listen for requests on some ports

3nginx a proxy that will filter requests by domain names and proxy them to the destination port on which gunicorn listens. Also, it serves static for us without involving Django

in /etc/supervisor/conf.d/app.conf:

[program:your_supervisor_service]
command=/envs/your_env/bin/gunicorn your_app.wsgi:application -b localhost:8005 -t 120
directory=/srv/your_app_dir/  # where your_app/wsgi.py 
user=your_user
autostart=true
autorestart=true
#optional:
environment = PATH="/some/bin/path/if/you/want", OTHER_VAR="val"

sudo supervisorctl start your_supervisor_service

Example of nginx proxy to gunicorn (with https):

/etc/nginx/conf.d/your_app.conf

server {
    listen      80;
    server_name your.domain.here your2.domain.here your3.domain.here;
    return 301 https://$server_name$request_uri;
}

server {
    listen      443;
    server_name your.domain.here your2.domain.here your3.domain.here;
    client_max_body_size 20M;

    ssl on;
    ssl_certificate /etc/ssl/some.crt;
    ssl_certificate_key /etc/ssl/some.key;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://localhost:8005;
    }

    location /static/ {
        alias     /srv/your_app/static/;
    }

    location /media/ {
        alias    /srv/your_app/media/;
    }
}

#nginx #django #gunicorn #supervisord
1
Ivan Borshchov profile picture
Nov 18, 2016
by Ivan Borshchov
Did it help you?
Yes !
No

Best related