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

如何在Hostinger VPS上部署搭载Celery、Django Channels的Django项目(Apache)

部署带Celery、Channels的Django项目到Hostinger VPS(搭配Apache)

一、服务器初始化

  1. 通过SSH连接Hostinger VPS(终端/PuTTY均可)
  2. 更新系统包:
    sudo apt update && sudo apt upgrade -y
    
  3. 安装核心依赖:
    sudo apt install python3-pip python3-dev python3-venv apache2 libapache2-mod-wsgi-py3 redis-server
    
    注:Redis作为Channels的通道层和Celery的消息代理,必须安装

二、项目环境搭建

  1. 创建项目目录并授权:
    sudo mkdir /var/www/django_project && sudo chown $USER:$USER /var/www/django_project
    
  2. 进入目录,创建并激活虚拟环境:
    cd /var/www/django_project
    python3 -m venv venv
    source venv/bin/activate
    
  3. 安装项目依赖:
    pip install django celery channels channels-redis gunicorn uvicorn django-redis
    
    注:uvicorn用于Gunicorn的ASGI Worker,适配Channels的WebSocket需求

三、项目上传与配置

  1. 通过Git/SFTP将本地项目上传至/var/www/django_project
  2. 修改项目settings.py关键配置:
    • 允许域名访问:
      ALLOWED_HOSTS = ['your-domain.com', '服务器IP']
      
    • 配置Channels:
      INSTALLED_APPS = [
          # 原有APP
          'channels',
      ]
      ASGI_APPLICATION = '你的项目名.asgi.application'
      CHANNEL_LAYERS = {
          'default': {
              'BACKEND': 'channels_redis.core.RedisChannelLayer',
              'CONFIG': {
                  "hosts": [('127.0.0.1', 6379)],
              },
          },
      }
      
    • 配置Celery:
      CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
      CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/0'
      
    • 配置静态文件路径:
      STATIC_ROOT = '/var/www/django_project/static/'
      
  3. 执行数据库迁移与静态文件收集:
    python manage.py migrate
    python manage.py collectstatic
    

四、Gunicorn ASGI服务配置

  1. 创建Gunicorn配置文件gunicorn_config.py:
    bind = "127.0.0.1:8000"
    workers = 3
    worker_class = "uvicorn.workers.UvicornWorker"
    
  2. 测试启动Gunicorn:
    gunicorn 你的项目名.asgi:application -c gunicorn_config.py
    
    无报错则按Ctrl+C停止,后续配置为系统服务后台运行

五、Apache反向代理配置

  1. 启用必要模块:
    sudo a2enmod proxy proxy_http proxy_wstunnel rewrite
    
  2. 创建站点配置文件/etc/apache2/sites-available/django_project.conf:
    <VirtualHost *:80>
        ServerName your-domain.com
        ServerAlias www.your-domain.com
    
        # 静态文件直接由Apache服务
        Alias /static/ /var/www/django_project/static/
        <Directory /var/www/django_project/static/>
            Require all granted
        </Directory>
    
        # WebSocket反向代理
        ProxyPass /ws ws://127.0.0.1:8000/ws
        ProxyPassReverse /ws ws://127.0.0.1:8000/ws
    
        # HTTP请求反向代理到Gunicorn
        ProxyPass / http://127.0.0.1:8000/
        ProxyPassReverse / http://127.0.0.1:8000/
    
        ErrorLog ${APACHE_LOG_DIR}/django_project_error.log
        CustomLog ${APACHE_LOG_DIR}/django_project_access.log combined
    </VirtualHost>
    
  3. 启用站点并重启Apache:
    sudo a2ensite django_project.conf
    sudo a2dissite 000-default.conf
    sudo systemctl restart apache2
    

六、Celery后台服务配置

  1. 创建Celery服务文件/etc/systemd/system/celery.service:
    [Unit]
    Description=Celery Worker Service
    After=network.target redis-server.service
    
    [Service]
    User=你的用户名
    Group=www-data
    WorkingDirectory=/var/www/django_project
    Environment="PATH=/var/www/django_project/venv/bin"
    ExecStart=/var/www/django_project/venv/bin/celery -A 你的项目名 worker --loglevel=info
    
    [Install]
    WantedBy=multi-user.target
    
  2. 如需定时任务,创建Celery Beat服务/etc/systemd/system/celery-beat.service:
    [Unit]
    Description=Celery Beat Service
    After=network.target redis-server.service
    
    [Service]
    User=你的用户名
    Group=www-data
    WorkingDirectory=/var/www/django_project
    Environment="PATH=/var/www/django_project/venv/bin"
    ExecStart=/var/www/django_project/venv/bin/celery -A 你的项目名 beat --loglevel=info
    
    [Install]
    WantedBy=multi-user.target
    
  3. 启动并启用服务:
    sudo systemctl daemon-reload
    sudo systemctl start celery
    sudo systemctl enable celery
    # 若配置了Beat:
    sudo systemctl start celery-beat
    sudo systemctl enable celery-beat
    

七、Gunicorn后台服务配置

  1. 创建Gunicorn服务文件/etc/systemd/system/gunicorn.service:
    [Unit]
    Description=Gunicorn ASGI Server
    After=network.target
    
    [Service]
    User=你的用户名
    Group=www-data
    WorkingDirectory=/var/www/django_project
    Environment="PATH=/var/www/django_project/venv/bin"
    ExecStart=/var/www/django_project/venv/bin/gunicorn 你的项目名.asgi:application -c /var/www/django_project/gunicorn_config.py
    
    [Install]
    WantedBy=multi-user.target
    
  2. 启动并启用:
    sudo systemctl daemon-reload
    sudo systemctl start gunicorn
    sudo systemctl enable gunicorn
    

八、部署验证

  • 访问http://your-domain.com确认Django站点正常加载
  • 测试WebSocket连接:通过前端代码连接ws://your-domain.com/ws/你的路由/,验证通信正常
  • 检查Celery任务:执行异步任务后,通过sudo journalctl -u celery查看日志确认任务处理正常

额外提示

  • 如需HTTPS,可通过Let's Encrypt配置SSL证书,并修改Apache配置将ws替换为wss
  • 定期查看各服务日志排查问题:Apache日志在/var/log/apache2/,Celery/Gunicorn日志通过journalctl查看

内容的提问来源于stack exchange,提问作者Muktadirul Islam Mahi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 02:01:17