Django 应用程序不是 运行 Gunicorn 和 Supervisor

Django application not running with Gunicorn and Supervisor

我正在尝试 运行 来自 gunicorn 和 supervisord 的 Django 应用程序。我已经将 bash 脚本配置为来自 gunicorn 的 运行 应用程序,它工作正常并且我能够看到 GUI 但是当我试图从 supervisorctl 启动这个应用程序时。我看不到 GUI。但是,Gunicorn 进程是 运行ning。

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 进程:

(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$

看来是环境变量的问题。因为当我尝试 运行 python manage.py runserver 时,它也不起作用。但是当我 运行ning ANALYTICS_ENV=dev python manage.py runserver 时,它就可以工作了。

但是,无论如何我都无法运行使用gunicorn。

有什么想法吗?

这已通过以下步骤解决。

  1. 源虚拟环境
  2. 导出ANALYTICS_ENV。
  3. 导出 virtualenv 的 PYTHONPATH。

主管配置:

[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