如何在服务器重启后通过Django manage.py启动gunicorn

时间:2013-01-24 17:43:28

标签: django ubuntu gunicorn

我通过调用python manage.py run_gunicorn(在virtualenv中)启动gunicon。

如何在重启Ubuntu 12.04服务器后重启gunicorn?

3 个答案:

答案 0 :(得分:3)

您可以使用主管在启动时启动应用程序并在崩溃时重新启动。

安装主管并使用以下内容创建配置文件/etc/supervisor/conf.d/your_app.conf

[program:your_app]
directory=/path/to/app/working/dir
command=/path/to/virtualenv_dir/bin/python /path/to/manage_py/manage.py run_gunicorn
user=your_app_user
autostart=true
autorestart=true

答案 1 :(得分:3)

由于我在Ubuntu上并且喜欢使用发行版中已包含的工具,因此在启动计算机后使用 Upstart 启动了gunicorn。

我将以下代码放入/etc/init/django_sf_flavour.conf

description "Run Custom Script"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/USER/bin/start_gunicorn.sh

启动后执行此文件(/home/USER/bin/start_gunicorn.sh):

#!/bin/bash
set -e
cd MY_PROJ_ROOT
source venv/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec python MY_PROJ_PATH/manage.py run_gunicorn

答案 2 :(得分:0)

这是基于@ j7nn7k的回答,但有点改动

1 - 在/ etc / init目录中创建.conf文件以使用upstart运行

cd /etc/init
nano django_sf_flavour.conf

2 - 在django_sf_flavour文件中添加以下行并保存。

description "Run Custom Script"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/start_gunicorn.sh

3 - 在主目录中创建start_gunicorn.sh文件,其中包含以下行

cd /home
nano start_gunicorn.sh

4 - 将这些代码放入其中并保存。

#!/bin/bash
set -e
cd mysite
source myenv/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn —bind 0.0.0.0:80 mysite.wsgi:application

5 - 设置start_gunicorn.sh的可运行权限

cd /home
chmod 775 start_gunicorn.sh

**额外**

使用此命令检查语法django_sf_flavour.conf

init-checkconf /etc/init/django_sf_flavour.conf

并且答案应该是这样的:

File /etc/init/django_sf_flavour.conf: syntax ok

如果需要,您可以在upstart日志文件中看到问题:

cat /var/log/upstart/django_sf_flavour.log

在不重启的情况下测试你的django_sf_flavour.conf文件

service django_sf_flavour start

测试你的bash文件" start_gunicorn.sh"像这样

cd /home 
bash start_gunicorn.sh 

检查django_sf_flavour的状态

initctl list | grep django_sf_flavour