我一直试图通过Django,Gunicorn,nginx设置我的生产服务器(Linode 512),我在nginx和gunicorn中遇到错误。由于nginx部分纯粹与服务器问题有关,我必须在serverfault中发布。但对于Gunicorn的错误,它与我的项目结构相关
Django Project/
apps/
confs/
development/
production/gunicorn.conf
settings/
custom.py
database.py
development.py
production.py
default.py
static/
js/
css/
admin/
templates/
manage.py
urls.py
这个结构在Django开发服务器上运行良好但是当我尝试使用Gunicorn时,它显示RuntimeError("django project not found")
完整的错误详情是here
Gunicorn设置:
import os
def numCPUs():
if not hasattr(os, "sysconf"):
raise RuntimeError("No sysconf detected.")
return os.sysconf("SC_NPROCESSORS_ONLN")
bind = "unix:/opt/Project1432/shared/gunicorn.sock"
workers = numCPUs() * 2 + 1
pidfile = "/opt/Project1432/shared/Project1432.pid"
logfile = "/opt/Project1432/shared/Project1432.log"
daemon = True
项目路径:
/opt/Project1432/releases/current/Project1432/...
有人可以指导我解决这个问题吗?
谢谢!
更新:
Gunicorn跑命令: 我在上面给出的项目路径中运行此命令。
gunicorn_django --config /opt/Project1432/releases/current/Project1432/confs/production/gunicorn.conf
答案 0 :(得分:2)
个人而言,我使用manage.py run_gunicorn
并通过命令行传递所有配置参数
对于Path,我使用virtualenv并在shell scrpt
这是我的bash脚本:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myProject.exemple.com
# Required-Start: $syslog $nginx
# Required-Stop: $syslog $nginx
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: the django fast_cgi portal for myProject.exemple.com
### END INIT INFO
# with this config :
# the project is in /home/web/prod/myProject.exemple.com/src/myProject/
# the socket is in /var/run/django/myProject.sock
# the log files is /val/log/nginx/myProject.exemple.com/gunicorn.log
# the pid file is /var/run/django/myProject.pid
# note, for debuging purpose, you can set a DEBUG env variable to prevent gunicorn to fork and to see all the output.
# ie : sudo DEBUG=True /etc/init.d/django_myProject start
if [ -f "/lib/lsb/init-functions-color"] ;
then
. /lib/lsb/init-functions-color
else
. /lib/lsb/init-functions
fi
# not exactly used by workon, but used to source the activate
WORKON_HOME=/home/web/prod/Envs
# change on eveny new project
PROJECT_NAME=myProject
DOMAINE=$PROJECT_NAME.exemple.com
# fixed
PIDFILE=/var/run/django/$PROJECT_NAME.pid
LOGFILE=/var/log/nginx/$DOMAINE/gunicorn.log
SOCKFILE=/var/run/django/$PROJECT_NAME.sock
SOCKET=unix:$SOCKFILE
#SOCKET=192.9.201.3:8050
#This is the command to be run, give the full pathname
PROJECT_DIR=/home/web/prod/$DOMAINE/src/$PROJECT_NAME/
DAEMON="$PROJECT_DIR/manage.py run_gunicorn"
USER=www-data
GROUP=www-data
WORKERS=2
# could be changed
#DAEMON_OPTS="run_gunicorn --workers 1 \
# --worker-connections 5 \
# --daemon\
# --user www-data\
# --group www-data\
# -t 30\
# --bind unix:$SOCKET \
# --pid $PIDFILE \
# --error-logfile /var/log/nginx/$DOMAINE/gunicorn.log"
# --daemon \
DEBUG_ARGS=" --user=$USER \
--group=$GROUP \
--workers=$WORKERS \
--bind $SOCKET \
-t 90 \
--settings=$PROJECT_NAME.settings.prod \
--name=$DOMAINE"
DAEMON_OPTS=" $DEBUG_ARGS \
--pid=$PIDFILE \
--daemon \
--log-file=$LOGFILE \
--log-level=info"
start()
{
cd "$PROJECT_DIR"
log_begin_msg "starting django website" "$PROJECT_NAME"
mkdir -p `dirname $SOCKFILE`
chown www-data:www-data `dirname $SOCKFILE`
# echo "$SOCKET" | grep '^unix:' > /dev/null 2> /dev/null
# if [ ! -e "$SOCKFILE" -a "$?" -eq "0" ]
# then
# log_warning_msg "creation du socket $SOCKFILE"
#
# mkfifo $SOCKFILE
#
# fi
if [ -f "$PIDFILE" ]
then
ps faux | grep -v grep | grep "$DAEMON" | grep `cat $PIDFILE` > /dev/null
if [ $? -eq 0 ]
then
log_failure_msg "fichier pid deja present avec un pid existant"
log_end_msg 1
return 1
fi
rm "$PIDFILE"
log_warning_msg "pid file deja existant"
fi
if [ -f "$WORKON_HOME/$PROJECT_NAME/bin/activate" ];
then
. $WORKON_HOME/$PROJECT_NAME/bin/activate
else
echo "impossible d'activer l'environement $WORKON_HOME/$PROJECT_NAME/bin/activate"
exit 1
fi
if [ ! -z "$DEBUG" ] ;
then
echo "sourcing $WORKON_HOME/$PROJECT_NAME/bin/activate"
echo $DAEMON $DEBUG_ARGS
$DAEMON $DEBUG_ARGS
else
$DAEMON $DAEMON_OPTS
fi
if [ $? -eq 0 ]
then
log_end_msg 0
else
log_end_msg 1
fi
}
stop()
{
log_begin_msg "stoping django website" "$PROJECT_NAME"
if [ -f "$PIDFILE" ]
then
start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE 2> /dev/null
rm "$PIDFILE"
log_end_msg 0
else
log_failure_msg "pid file absent for $PROJECT_NAME django website"
#log_end_msg 1
fi
}
status()
{
if [ -f "$PIDFILE" ]
then
# fichier pid existant
ps faux | grep -v grep | grep $DAEMON | grep `cat $PIDFILE` > /dev/null
if [ $? -eq 0 ]
then
echo "$PROJECT_NAME django website running"
else
echo "WARN !! pid file present but $PROJECT_NAME django website stopped"
fi
else
ps faux | grep -v grep | grep $DAEMON > /dev/null
if [ $? -eq 0 ]
then
echo "WARN !! $PROJECT_NAME django website apear to be running but don't have a PID FILE"
else
echo "$PROJECT_NAME django website stopped"
fi
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
status)
status
;;
*)
echo "Usage: "$1" {start|stop|restart|status}"
exit 1
esac
exit 0