如何使Django Channels服务脱离runserver持续运行?
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.outin your current directory. - To stop the service later, find the process ID first with
ps aux | grep runserver, then kill it withkill <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:
- Install screen (if not already installed):
# Debian/Ubuntu sudo apt install screen # RHEL/CentOS sudo yum install screen - Start a new screen session:
screen - Run your Django Channels command inside the session:
python manage.py runserver 0.0.0.0:8080 - Detach from the session without stopping the service: press
Ctrl + A, thenD. - To reattach later:
screen -r - To stop the service, reattach to the session and press
Ctrl + C, or kill the process as before.
Using Tmux (Modern Alternative to Screen):
- Install tmux:
# Debian/Ubuntu sudo apt install tmux # RHEL/CentOS sudo yum install tmux - Create a new named session:
tmux new -s channels-session - Run your service command inside the session.
- Detach: press
Ctrl + B, thenD. - Reattach later:
tmux attach -t channels-session
Production Environment (Recommended)
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:
- Create the service file:
sudo nano /etc/systemd/system/channels.service - 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 - Save and exit the editor (in nano:
Ctrl + O, thenEnter, thenCtrl + 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

