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

如何使Django Channels服务脱离runserver持续运行?

Keep Django Channels WebSocket Service Running Continuously

Got it, let's get your WebSocket service up and running 24/7, even after you close your terminal! Below are practical solutions tailored for both development and production scenarios:

Development Environment (Quick Fixes)

If you're just testing things out and need a simple way to keep the service going without switching to production tools:

1. Use nohup to Run in the Background

nohup lets you run commands that keep executing even after you log out. Here's how to use it:

nohup python manage.py runserver 0.0.0.0:8080 &
  • The & at the end sends the process to the background.
  • All logs will be saved to a file named nohup.out in your current directory.
  • To stop the service later, find the process ID first with ps aux | grep runserver, then kill it with kill <process_id>.

2. Use Screen/Tmux (More Flexible)

These tools let you create persistent terminal sessions that you can detach from and reattach to later—perfect if you need to check logs or interact with the service again.

Using Screen:

  1. Install screen (if not already installed):
    # Debian/Ubuntu
    sudo apt install screen
    # RHEL/CentOS
    sudo yum install screen
    
  2. Start a new screen session:
    screen
    
  3. Run your Django Channels command inside the session:
    python manage.py runserver 0.0.0.0:8080
    
  4. Detach from the session without stopping the service: press Ctrl + A, then D.
  5. To reattach later:
    screen -r
    
  6. To stop the service, reattach to the session and press Ctrl + C, or kill the process as before.

Using Tmux (Modern Alternative to Screen):

  1. Install tmux:
    # Debian/Ubuntu
    sudo apt install tmux
    # RHEL/CentOS
    sudo yum install tmux
    
  2. Create a new named session:
    tmux new -s channels-session
    
  3. Run your service command inside the session.
  4. Detach: press Ctrl + B, then D.
  5. Reattach later:
    tmux attach -t channels-session
    

The runserver command is meant for development only—it's not secure or efficient enough for production. For a stable, production-ready setup, use Daphne (the official ASGI server for Django Channels) with a system service manager like systemd.

Step 1: Install Daphne

If you haven't already, install Daphne in your virtual environment:

pip install daphne

Step 2: Run Daphne Directly

Replace the runserver command with Daphne (point to your project's ASGI application):

daphne -b 0.0.0.0 -p 8080 your_project_name.asgi:application

(Replace your_project_name with the actual name of your Django project.)

Step 3: Configure Systemd to Manage the Service

To make sure the service starts on boot and restarts automatically if it crashes, create a systemd service file:

  1. Create the service file:
    sudo nano /etc/systemd/system/channels.service
    
  2. Paste the following content (update paths and user/group to match your setup):
    [Unit]
    Description=Django Channels WebSocket Service
    After=network.target
    
    [Service]
    User=your_username  # Replace with your server username
    Group=www-data      # Common group for web apps
    WorkingDirectory=/path/to/your/django/project  # Full path to your project root
    ExecStart=/path/to/your/virtualenv/bin/daphne -b 0.0.0.0 -p 8080 your_project_name.asgi:application
    Restart=always      # Restart service if it crashes
    RestartSec=10       # Wait 10 seconds before restarting
    Environment="DJANGO_SETTINGS_MODULE=your_project_name.settings"  # Ensure settings are loaded
    
    [Install]
    WantedBy=multi-user.target
    
  3. Save and exit the editor (in nano: Ctrl + O, then Enter, then Ctrl + X).

Step 4: Start and Enable the Service

# Reload systemd to recognize the new service
sudo systemctl daemon-reload

# Start the service
sudo systemctl start channels

# Enable it to start on boot
sudo systemctl enable channels

Check Service Status:

sudo systemctl status channels

You'll see if the service is running, and view recent logs directly here.

Stop/Restart the Service:

# Stop
sudo systemctl stop channels

# Restart (e.g., after updating code)
sudo systemctl restart channels

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:34:41