You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署在DigitalOcean的Django应用:如何修改Gunicorn命令启动Django-Q

解决Django-Q与Gunicorn共存的启动问题

你没办法直接修改Gunicorn命令来同时启动Django-Q——Gunicorn是处理HTTP请求的WSGI服务器,而Django-Q的worker是独立的后台任务进程,两者需要分开管理。以下是几种可行的生产环境方案:

方案1:临时后台启动(适合测试)

先把Django-Q进程放到后台运行,再启动Gunicorn:

# 启动Django-Q并后台运行,日志输出到qcluster.log
nohup python manage.py qcluster > qcluster.log 2>&1 &
# 正常启动Gunicorn
gunicorn reservationSystem.wsgi:application --bind 0.0.0.0:8080 --worker-tmp-dir /dev/shm

停止Django-Q时,用ps aux | grep qcluster找到进程ID,再执行kill <进程ID>即可。

方案2:用Supervisor管理进程(生产环境推荐)

Supervisor可以同时监控Gunicorn和Django-Q进程,进程崩溃时自动重启,适合稳定运行。

  1. 安装Supervisor:
sudo apt update && sudo apt install supervisor
  1. 在/etc/supervisor/conf.d/目录下创建两个配置文件:

Gunicorn配置(gunicorn.conf):

[program:gunicorn]
directory=/你的项目根目录绝对路径
command=gunicorn reservationSystem.wsgi:application --bind 0.0.0.0:8080 --worker-tmp-dir /dev/shm
user=你的服务器用户名
autostart=true
autorestart=true
stdout_logfile=/var/log/gunicorn.log
stderr_logfile=/var/log/gunicorn.err.log

Django-Q配置(django-q.conf):

[program:django-q]
directory=/你的项目根目录绝对路径
command=python manage.py qcluster
user=你的服务器用户名
autostart=true
autorestart=true
stdout_logfile=/var/log/django-q.log
stderr_logfile=/var/log/django-q.err.log
  1. 加载配置并启动服务:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start gunicorn django-q

方案3:用Systemd服务管理(生产环境另一种推荐方式)

创建两个systemd服务文件来分别管理Gunicorn和Django-Q:

Gunicorn服务文件(/etc/systemd/system/gunicorn.service):

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=你的服务器用户名
Group=www-data
WorkingDirectory=/你的项目根目录绝对路径
ExecStart=gunicorn reservationSystem.wsgi:application --bind 0.0.0.0:8080 --worker-tmp-dir /dev/shm

[Install]
WantedBy=multi-user.target

Django-Q服务文件(/etc/systemd/system/django-q.service):

[Unit]
Description=django-q worker daemon
After=network.target postgresql.target  # 如果你用PostgreSQL就加这个,其他数据库对应修改

[Service]
User=你的服务器用户名
Group=www-data
WorkingDirectory=/你的项目根目录绝对路径
ExecStart=python manage.py qcluster
Restart=always

[Install]
WantedBy=multi-user.target
  1. 启动并设置开机自启:
sudo systemctl daemon-reload
sudo systemctl start gunicorn django-q
sudo systemctl enable gunicorn django-q

前置测试建议

先单独运行python manage.py qcluster,确认Django-Q能正常启动无报错,排除是配置本身的问题导致崩溃后,再整合到进程管理器中。

内容的提问来源于stack exchange,提问作者Jakub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:20:07