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

Django+Celery+Redis部署Ubuntu22.04遇Broker配置报错求助

Django-Celery-Redis部署Ubuntu 22.04连接异常排查

问题现象

Windows本地运行正常,部署到Ubuntu 22.04后,Celery日志出现No hostname was supplied. Reverting to default 'localhost'错误,程序无法识别Redis Broker配置,转而尝试连接未安装的RabbitMQ,通过Supervisor启动进程后问题依旧。

相关配置文件

settings.py

# CELERY SETTINGS
CELERY_BROKER_URL  = 'redis://127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE or 'UTC'

CELERY_RESULT_BACKEND  = 'django-db'

# CELERY BEAT SETTINGS
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

celery.py

from __future__ import absolute_import, unicode_literals
import os

from celery import Celery
from django.conf import settings
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dear.settings')

app = Celery('dear')
app.conf.broker_url = 'redis://localhost:6379/0'
app.conf.result_backend = 'redis://localhost:6379/0'
app.conf.enable_utc = False

app.conf.update(timezone = 'Asia/Dubai')

app.config_from_object(settings, namespace='CELERY')

celery.conf(Supervisor配置)

[program:dearapi]
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery worker --loglevel=INFO
directory=/home/djangoadmin/pyapps/dearapi/dear
user=djangoadmin
numprocs=1
stdout_logfile=/var/log/supervisor/celery.log
stderr_logfile=/var/log/supervisor/celery.log
autostart=true
autorestart=true
startsecs=0

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998

celerybeat.conf(Supervisor配置)

; ========================
;  celery beat supervisor
; ========================

; the name of your supervisord program
[program:dearapibeat]

; Set full path to celery program if using virtualenv
command=/home/djangoadmin/pyapps/venv/bin/celery -A celery beat --loglevel=INFO

; The directory to your Django project
directory=/home/djangoadmin/pyapps/dearapi/dear

; If supervisord is run as the root user, switch users to this UNIX user account
; before doing any processing.
user=djangoadmin

; Supervisor will start as many instances of this program as named by numprocs
numprocs=1

; Put process stdout output in this file
stdout_logfile=/var/log/supervisor/celerybeat.log

; Put process stderr output in this file
stderr_logfile=/var/log/supervisor/celerybeat.log

; If true, this program will start automatically when supervisord is started
autostart=true

; May be one of false, unexpected, or true. If false, the process will never
; be autorestarted. If unexpected, the process will be restart when the program
; exits with an exit code that is not one of the exit codes associated with this
; process’ configuration (see exitcodes). If true, the process will be
; unconditionally restarted when it exits, without regard to its exit code.
autorestart=true

; The total number of seconds which the program needs to stay running after
; a startup to consider the start successful.
startsecs=0

; if your broker is supervised, set its priority higher
; so it starts first
priority=999

排查与修复步骤

1. 修正Supervisor启动命令的App指定

Supervisor配置中启动命令的-A celery参数错误,你的Celery实例命名为dear(app = Celery('dear')),且settings模块为dear.settings,正确的App指定应为-A dear。错误的参数会导致Celery无法加载正确配置, fallback到默认RabbitMQ broker。

修改后的celery.conf命令:

command=/home/djangoadmin/pyapps/venv/bin/celery -A dear worker --loglevel=INFO

修改后的celerybeat.conf命令:

command=/home/djangoadmin/pyapps/venv/bin/celery -A dear beat --loglevel=INFO

2. 统一配置加载逻辑(可选优化)

celery.py中手动设置的broker_url会被后续app.config_from_object(settings, namespace='CELERY')覆盖,建议去掉手动配置,完全依赖settings中的参数,避免冲突:

修改后的celery.py:

from __future__ import absolute_import, unicode_literals
import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dear.settings')

app = Celery('dear')
app.conf.enable_utc = False
app.conf.update(timezone = 'Asia/Dubai')

# 从settings加载带CELERY_前缀的配置
app.config_from_object(settings, namespace='CELERY')

3. 验证Redis服务状态

在Ubuntu上确认Redis是否正常运行:

sudo systemctl status redis-server

若未运行,启动并设置开机自启:

sudo systemctl start redis-server
sudo systemctl enable redis-server

4. 重启Supervisor服务

修改配置后,重启Supervisor使设置生效:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart dearapi dearapibeat

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 12:07:06