Linux

將應用程序部署到測試伺服器時出錯?

  • January 2, 2019

我在 CentOS 7 測試伺服器上部署我的 django 應用程序時遇到問題。最初通過伺服器上的根配置文件安裝 miniconda,然後執行應用程序的副本並將由 conda 命令創建的兩個 env 添加為 1002:1002 組中的應用程序。

之後,我發布了為啟動 gunicorn 和應用程序的內部文件夾而創建的 bash 腳本,以獲得 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

我怎麼解決這個問題?

我想出瞭如何解決這個問題,一開始是在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'

引用自:https://unix.stackexchange.com/questions/490537