用于virtualenvwrapper的Gunicorn设置

时间:2016-12-18 12:30:53

标签: django nginx gunicorn

我正在尝试部署使用Djangovirtualenvwrapper制作的测试网站。我想使用nginx进行请求。我使用了Taskbuster的教程。所以我的项目层类似如下:

--abctasarim                       **main folder
--manage.py                        **django manage file
----/yogavidya                     ** project folder
----/yogavidya/wsgi.py             **wsgi file
----/yogavidya/settings/base.py    ***settings

我准备了一个与gunicorn一起使用的脚本。我将virtualenv发送到了virtualenvwrapper envs

#!/bin/bash

NAME="yogavidya"                              #Name of the application (*)
DJANGODIR=/home/ytsejam/public_html/abctasarim     # Django project directory (*)
SOCKFILE=/home/ytsejam/public_html/abctasarim/run/gunicorn.sock        # we will communicate using this unix socket (*)
USER=ytsejam                                        # the user to run as (*)
GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=yogavidya.settings.base           # which settings file should Django use (*)
DJANGO_WSGI_MODULE=yogavidya.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /home/ytsejam/.virtualenvs/yv_dev/bin/activate
#export /home/ytsejam/.virtualenvs/yv_dev/bin/postactivate
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 /home/ytsejam/public_html/abctasarim/gunicorn  \
  --name $NAME \
  --workers $NUM_WORKERS \
  --env DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE \
  --pythonpath $DJANGODIR \
  --user $USER \
  --bind=unix:$SOCKFILE yogavidya.wsgi:application

当我尝试运行它时,我的服务文件出错:

...
ImportError: No module named ' '
...

如何修复我的脚本以正确提供网站?

由于

1 个答案:

答案 0 :(得分:1)

virtualenvwrapper应该是你在开发中使用的。您希望使用virtualenvwrapper包构建virtualenv的程序包进行部署。我为您提供的最佳建议是尝试通常用于启动virtualenvwrapper环境的步骤,即获取shell脚本然后使用workon

NAME="yogavidya"                              #Name of the application (*)
DJANGODIR=/home/ytsejam/public_html/abctasarim     # Django project directory (*)
SOCKFILE=/home/ytsejam/public_html/abctasarim/run/gunicorn.sock        # we will communicate using this unix socket (*)
USER=ytsejam                                        # the user to run as (*)
GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=yogavidya.settings.base           # which settings file should Django use (*)
DJANGO_WSGI_MODULE=yogavidya.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGO_DIR
source /path/to/virtualenvwrapper.sh
workon yv_dev

你应该在gunicorn你的virtualenv之后尝试从命令行调用activat

以下是使用virtualenv

执行此操作的方法
cd /home/ytsejam/public_html/abctasarim 
sudo pip install virtualenv
virtualenv .
. bin/activate
pip install -r requirements.txt
pip install gunicorn

gunicorn脚本:

NAME="yogavidya"                              #Name of the application (*)
DJANGODIR=/home/ytsejam/public_html/abctasarim     # Django project directory (*)
SOCKFILE=/home/ytsejam/public_html/abctasarim/run/gunicorn.sock        # we will communicate using this unix socket (*)
USER=ytsejam                                        # the user to run as (*)
GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)

cd $DJANGO_DIR
. bin/activate

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

exec /home/ytsejam/public_html/abctasarim/bin/gunicorn  \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE yogavidya.wsgi:application