A good practice on production server | Nginx+ Gunicorn + supervisor + Django +React

Sanghmitra Rathore
2 min readMay 8, 2021

--

Deploying a project on the production server is not enough until all security, efficiency and ease of handling not be taken care.

I know many coder who spent comparatively more time on CSS then security and efficiency of a site, may be most of the clients may tolerate hidden things then observable front end.

Here we will continue our optimization of production server which we deployed in previous article , if you feel any difficulty in this article then please go and refer previous one first.

In this article we will handle gunicorn server by using Supervisor. but why? reasons are
1. To monitor and control a number of process
2. Auto start the process at boot time, in case of error.
3. save time to write lots of code again and again during any changes in the process directory.
4. provides management of output logs and error logs

Step 1:
install supervisor

sudo apt-get supervisor

step 2:
Configure supervisor

create a directory at supervisor and write it config file in it

cd /etc/supervisor/conf.d/
sudo touch gunicorn.conf
sudo vi gunicorn.conf

config settings of below file structure

project location: /user/django/project/
project name: myProject

[program:gunicorn]Directory=/home/django/project/command=/home/django/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8080 myproject.wsgi:applicationautostart=trueautorestart=truestderr_logfile=/var/log/gunicorn/gunicorn.err.logstdout_logfile=/var/log/gunicorn.out.log[group:mygrp]

Note: binding address should be same as written in Nginx config file other wise 502 error will come

Create directory for log files

sudo mkdir /var/log/gunicorn 

Starting supervisor process

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status

now your django project is started in process created by supervisor

you can stop supervisor by

sudo supervisorctl stop

--

--

Sanghmitra Rathore
Sanghmitra Rathore

Written by Sanghmitra Rathore

AWS, Webapp, IOS, Android || Nexts,Reactjs,NodeJs, MongoDb

No responses yet