我在 CentOS 7 测试服务器上部署 django 应用程序时遇到问题。最初通过服务器上的 root 配置文件安装 miniconda,然后执行应用程序的副本,并将由 conda 命令创建的 env 添加为 1002:1002 组中的应用程序。
之后,我发布了创建的 bash 脚本,该脚本用于激活 Gunicorn 和应用程序的内部文件夹,以获得 755 和 + x 权限,并添加组 1002:1002。
但无论如何,将 miniconda3 文件夹和 djangoapp 应用程序文件夹添加到 100x: 100x 组我无法部署。
我的剧本
#!/bin/bash
NAME="djangoapp"
DJANGODIR=/var/www/dangoapp
SOCKFILE=/var/www/djangoapp/run/gunicorn.sock
USER=nginx
GROUP=webdata
NUM_WORKERS=5
DJANGO_SETTINGS_MODULE=app.settings
DJANGO_WSGI_MODULE=app.wsgi
TIMEOUT=3600
echo "Starting $NAME as `whoami`"
cd $DJANGODIR
source activate djangoapp
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
#exec gunicorn ${DJANGO_WSGI_MODULE}:application \
exec /home/adm/miniconda3/envs/ws/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE \
--timeout $TIMEOUT
我收到 Gunicorn 错误消息:
./gunicorn.sh
Starting djangoapp as root
[2018-12-22 00:01:07 -0200] [17316] [INFO] Starting gunicorn 19.7.1
[2018-12-22 00:01:07 -0200] [17316] [INFO] Listening at: unix:/var/www/djangoapp/run/gunicorn.sock (17316)
[2018-12-22 00:01:07 -0200] [17316] [INFO] Using worker: sync
[2018-12-22 00:01:07 -0200] [17323] [INFO] Booting worker with pid: 17323
[2018-12-22 00:01:07 -0200] [17323] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/workers/base.py", line 126, in init_process
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
File "/home/nlt-adm/miniconda3/envs/ws/lib/python3.4/site-packages/gunicorn/util.py", line 352, in import_app
File "/var/www/djangoapp/app/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named 'django'
[2018-12-22 00:01:07 -0200] [17323] [INFO] Worker exiting (pid: 17323)
[2018-12-22 00:01:07 -0200] [17316] [INFO] Shutting down: Master
[2018-12-22 00:01:07 -0200] [17316] [INFO] Reason: Worker failed to boot.
编辑:在没有脚本的情况下使用gunicorn进行测试,它可以工作!...
gunicorn --bind 0.0.0.0:8000 djangoapp.wsgi:application
[2018-12-22 01:47:50 -0200] [17869] [INFO] Starting gunicorn 19.7.1
[2018-12-22 01:47:50 -0200] [17869] [INFO] Listening at: http://0.0.0.0:8000 (17869)
[2018-12-22 01:47:50 -0200] [17869] [INFO] Using worker: sync
[2018-12-22 01:47:50 -0200] [17872] [INFO] Booting worker with pid: 17872
^C[2018-12-22 01:48:15 -0200] [17869] [INFO] Handling signal: int
[2018-12-21 21:48:15 -0600] [17872] [INFO] Worker exiting (pid: 17872)
[2018-12-22 01:48:15 -0200] [17869] [INFO] Shutting down: Master
我怎么解决这个问题?
答案1
我想出了如何解决这个问题,一开始是在gunicorn的bash执行脚本中。找到的解决方案是删除参数 $USER,因为不允许执行 miniconda 的 env。
需要修改的代码是gunicorn部分的代码:
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
#--user $USER
--workers $NUM_WORKERS \
--bind $PORT \
--bind=unix:$SOCKFILE \
--timeout $TIMEOUT
--access-logfile - $LOGDIR \
--access-logformat '"%(r)s" %(s)s %(b)s'