Skip to content

Instantly share code, notes, and snippets.

@ceessay
Forked from fabiopiovam/readme.md
Created March 21, 2018 10:16
Show Gist options
  • Save ceessay/36227a04bcdd82f04d34c1c28b0999a2 to your computer and use it in GitHub Desktop.
Save ceessay/36227a04bcdd82f04d34c1c28b0999a2 to your computer and use it in GitHub Desktop.
Django deploy using Nginx + Gunicorn + Supervisor

Django deploy using Nginx + Gunicorn + Supervisor

Introduction

This implementation was made using Debian 8.

Requirements

  • Install packages:

    aptitude install nginx supervisor
    

General Configurations:

  • A. First, make the clone of your project in some area of preference on your server:

     cd /store/projects/
     git clone my-site.git
    
  • B. Create virtualenv

  • C. Install Gunicorn into venv of project:

    (venv)$ pip install gunicorn
    

Got to NGINX

  1. Access the sites-available of nginx

    cd /etc/nginx/sites-available/
    
  2. Create a file of project

    vim mydomain.localhost
    
  3. With content (Attention: Check that the chosen port is already in use (eg use command sudo netstat -tlpn | grep 800)):

     upstream MY_DOMAIN_ALIAS_NAME {
       	server	127.0.0.1:8000;
     }
    
  4. Create symbolic link of this file:

    sudo ln -s /etc/nginx/sites-available/mydomain.localhost /etc/nginx/sites-enabled/
    
  5. Create a nginx configuration file (if it already exists, you just add the contents of step 6):

    sudo vim /etc/nginx/conf.d/vhost_autogen.conf
    
  6. Inside this file vhost_autogen.conf we will add a "block" for the appointment of the service:

     server {
       listen                *:80;
       listen 443 ssl;
     
       server_name           bancodequestoes.local.convergetecnologia.com.br; 
     
       access_log            /var/log/nginx/MEU_DOMINIO.access.log;
       error_log             /var/log/nginx/MEU_DOMINIO.error.log;
      
       location /static/ {
         alias /path/to/static/of/project/django/;
       }
       
       location /media/ {
         alias /path/to/media/of/project/django/;
       }
     
       location / {
         proxy_pass            http://MY_DOMAIN_ALIAS_NAME;
         proxy_read_timeout    90;
         proxy_connect_timeout 90;
         proxy_redirect        off;
       }
     }
    
  7. Once this is done, go to the /etc/hosts file and add a line:

    sudo vim /etc/hosts
    

    With content:

     ....
     127.0.0.1	my-domain.com
     ....
    

Supervisor configuration

  1. Create a supervisor configuration file:

    sudo vim /etc/supervisor/conf.d/mysite.conf  (Warning: Please note the .conf extension)
    
  2. With content:

     [program:MYPROJECTALIASSUPERVISOR]
     command=/PATH/TO/VIRTUALENV/bin/gunicorn [PROJECTNAME].wsgi:application -b 127.0.0.1:8000 -w 3 --timeout 600 --access-logfile /tmp/gunicorn.access-MYDOMAIN.log --error-logfile /tmp/gunicorn.error-MYDOMAIN.log
     directory=/PATH/TO/PROJECT/
     user=USER-OF-BASH
     autostart=true
     autorestart=true
     redirect_stderr=True
     environment=DJANGO_SETTINGS_MODULE="app.settings"
    
  3. All right! Now we will launch the following commands to start the settings previously made:

     sudo supervisorctl reread
     sudo supervisorctl update
     sudo supervisorctl start MYPROJECTALIASSUPERVISOR
    
  4. Lastly:

    sudo service nginx restart
    
  5. You can test the access via browser, in case of error 500 you have the log files:

    /tmp/gunicorn.error-MYDOMAIN.log
    

    OR

    /tmp/gunicorn.access-MYDOMAIN.log
    

Thanks for the tutorial:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment