虚拟环境中找不到Gunicorn。

5

我正在部署一个 Django 应用,在手动运行时它能正常工作。我尝试使用 supervisor,但是当我运行 sudo supervisorctl status botApp 时,日志文件显示:

Starting botApp as ubuntu
/home/ubuntu/gunicorn_start.bash: line 28: exec: gunicorn: not found

我的gunicorn_start.bash文件如下:

#!/bin/bash

NAME="botApp"                                   # Name of the application
DJANGODIR=/home/ubuntu/chimpy               # Django project directory
SOCKFILE=/home/ubuntu/django_env/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=botApp.settings      # which settings file should Django use
DJANGO_WSGI_MODULE=botApp.wsgi              # WSGI module name
echo "Starting $NAME as `whoami`"

# Activate the virtual environment

cd $DJANGODIR
source /home/ubuntu/django_env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# 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 gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-

我的配置文件位于 /etc/supervisor/conf.d/botApp.conf:

[program:botApp]
command = /home/ubuntu/gunicorn_start.bash;
user = ubuntu;
stdout_logfile = /home/ubuntu/logs/gunicorn_supervisor.log;
redirect_stderr = true;
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8;

我的gunicorn bash出了什么问题吗?非常感谢。


2
有点傻的问题,但是你是否通过 pip install gunicorn 安装了gunicorn? - Games Brainiac
@GamesBrainiac 是的,我可以手动运行gunicorn_start.bash,而且它似乎运行得很好。 - Alberto Carmona
2
如果您在bash脚本中使用/home/ubuntu/django_env/bin/gunicorn而不是gunicorn,则您无需使用source命令来激活虚拟环境。 - Alasdair
@Alasdair 对不起,Alasdair,我对gunicorn完全是新手,我应该删除源代码并将gunicorn替换为“/home/ubuntu/django_env/bin/gunicorn”吗? - Alberto Carmona
2
是的,这就是我建议的。 - Alasdair
1
它起作用了,谢谢!(再次 :) ) - Alberto Carmona
1个回答

1

要检查虚拟环境是否处于活动状态,只需编写简单的脚本即可。

#!bin/bash

source /var/www/html/project_env/bin/activate

然后运行命令 -> sudo bash file_name.sh

如果你没有收到任何错误提示,那就意味着虚拟环境已经激活了。

注意 - 在终端或者shell上不会有任何虚拟环境激活的标记。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接