Django application not running with Gunicorn and Supervisor

1.1k Views Asked by At

I am trying to run a Django application from gunicorn and supervisord. I have configured bash script to run application from gunicorn which is working fine and I am able to see the GUI but when I was trying to start this application from supervisorctl. I am not able to see the GUI. However, Gunicorn processes are running.

gunicorn_start2

#!/bin/bash
export ANALYTICS_ENV="dev"
NAME="analytics"                                  # Name of the application
DJANGODIR=/home/ubuntu/code/current/analytics/analytics/analysis/             # Django project directory
SOCKFILE=/home/ubuntu/code/current/analytics/analytics/run/gunicorn.sock  # we will communicte using this unix socket
USER=ubuntu                                       # the user to run as
GROUP=ubuntu                                     # the group to run as
NUM_WORKERS=3                                     # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=analytics.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=analytics.wsgi                     # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
echo $DJANGODIR
source /home/ubuntu/code/current/analytics/analytics/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
#export PYTHONPATH=$DJANGODIR:$PYTHONPATH
#export PYTHONPATH=/home/ubuntu/code/analytics/bin/python
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/ubuntu/code/current/analytics/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=all \
  --log-file=-

gunicorn_start.conf

[program:analytics]
command = bash /home/ubuntu/code/current/analytics/analytics/analysis/gunicorn_start2                    ; Command to start app
user = ubuntu                                    ; User to run as
stdout_logfile = /home/ubuntu/code/current/analytics/analytics/analysis/gunicorn_start2.log   ; Where to write log messages
redirect_stderr = true                                                ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,ANALYTICS_ENV=dev                     ; Set UTF-8 as default encoding

Gunicorn Processes:

(analytics) ubuntu@ip-172-31-25-151:~/code/current/analytics/analytics/analysis$ ps aux | grep [g]unicorn
ubuntu   16039  0.3  0.9  57152 18436 ?        S    14:47   0:00 /home/ubuntu/code/current/analytics/bin/python2 /home/ubuntu/code/current/analytics/bin/gunicorn analytics.wsgi:application --name analytics --workers 3 --user=ubuntu --group=ubuntu --bind=unix:/home/ubuntu/code/current/analytics/analytics/run/gunicorn.sock --log-level=all --log-file=-
ubuntu   16050  1.1  2.3 276744 48792 ?        Sl   14:47   0:00 /home/ubuntu/code/current/analytics/bin/python2 /home/ubuntu/code/current/analytics/bin/gunicorn analytics.wsgi:application --name analytics --workers 3 --user=ubuntu --group=ubuntu --bind=unix:/home/ubuntu/code/current/analytics/analytics/run/gunicorn.sock --log-level=all --log-file=-
ubuntu   16053  1.1  2.3 276760 47212 ?        Sl   14:47   0:00 /home/ubuntu/code/current/analytics/bin/python2 /home/ubuntu/code/current/analytics/bin/gunicorn analytics.wsgi:application --name analytics --workers 3 --user=ubuntu --group=ubuntu --bind=unix:/home/ubuntu/code/current/analytics/analytics/run/gunicorn.sock --log-level=all --log-file=-
ubuntu   16056  1.1  2.3 276768 47228 ?        Sl   14:47   0:00 /home/ubuntu/code/current/analytics/bin/python2 /home/ubuntu/code/current/analytics/bin/gunicorn analytics.wsgi:application --name analytics --workers 3 --user=ubuntu --group=ubuntu --bind=unix:/home/ubuntu/code/current/analytics/analytics/run/gunicorn.sock --log-level=all --log-file=-
(analytics) ubuntu@ip-172-31-25-151:~/code/current/analytics/analytics/analysis$

It seems that the issue is with the environmental variable. Because when I am trying to run python manage.py runserver then its also not working. But when I am running ANALYTICS_ENV=dev python manage.py runserver then it's working.

However, I am unable to run it with gunicorn in anyway.

Any idea?

1

There are 1 best solutions below

0
On BEST ANSWER

This has been solved by below steps.

  1. source virtualenv
  2. Export ANALYTICS_ENV.
  3. Export PYTHONPATH of virtualenv.

Supervisor Configuration:

[program:analytics]
command = gunicorn analytics.wsgi:application --name analytics --workers 4 --user=ubuntu --timeout=3600 --bind 127.0.0.1:8000 ; Command to start app
user = ubuntu ; User to run as
stdout_logfile = /home/code/example/gunicorn_start2.log ; Where to write log messages
redirect_stderr = true ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,ANALYTICS_ENV=dev,PYTHONPATH=/home/ubuntu/code/current/analytics/bin/python ; Set UTF-8 as default encoding