如何实现DigitalOcean Ubuntu服务器重启后自动重启Django应用
Hey there! I get it—server reboots taking your app offline is a real pain. Let’s get your setup configured so everything fires back up automatically after those monthly restarts. Here are the most reliable methods, starting with the industry standard for Ubuntu systems:
Method 1: Use Systemd Services (Recommended)
Systemd is Ubuntu’s default service manager, and it’s perfect for ensuring your processes start on boot and stay running (even if they crash unexpectedly).
Step 1: Create a Gunicorn Systemd Service File
Create a new service file for Gunicorn using your favorite editor (nano or vim):
sudo nano /etc/systemd/system/gunicorn.service
Paste in this configuration, adjusting paths to match your setup:
[Unit] Description=gunicorn daemon for Django app After=network.target postgresql.service [Service] User=your-username # Replace with your actual server username Group=www-data WorkingDirectory=/path/to/your/django/project # Folder containing your manage.py ExecStart=/home/wrangler_env/bin/gunicorn --workers 3 --bind unix:/path/to/your/project/wrangler.sock wrangler.wsgi:application # Auto-restart if Gunicorn crashes or server reboots Restart=always RestartSec=5 [Install] WantedBy=multi-user.target
- Update
your-usernameto the user who owns your Django project - Double-check the
WorkingDirectoryandExecStartpaths match your virtual environment and project location - The
After=postgresql.serviceline ensures Gunicorn waits for PostgreSQL to fully start before launching
Step 2: Enable and Start the Service
Save the file, then run these commands to activate the service:
# Reload systemd to recognize the new service sudo systemctl daemon-reload # Set the service to start automatically on boot sudo systemctl enable gunicorn # Start the service immediately sudo systemctl start gunicorn
Step 3: Verify PostgreSQL Auto-Starts
Chances are PostgreSQL already has a systemd service enabled, but let’s confirm:
sudo systemctl is-enabled postgresql
If it returns disabled, run this to enable auto-start for PostgreSQL:
sudo systemctl enable postgresql
Method 2: Use Your Existing Script with Crontab
If you prefer sticking with your existing bash script, you can set it to run on reboot using crontab.
Step 1: Polish Your Startup Script
First, complete the Gunicorn command and make the script executable:
#!/bin/bash # Activate virtual env echo "Activating virtualenv." source /home/wrangler_env/bin/activate # Optional: Uncomment only if you need to restart PostgreSQL on boot # echo "Restarting database." # sudo service postgresql restart # Start Gunicorn processes echo "Starting Gunicorn." gunicorn --workers 3 --bind unix:/path/to/your/project/wrangler.sock wrangler.wsgi:application
Add execution permissions:
chmod +x /path/to/your/startup-script.sh
Step 2: Add to Crontab
Open your user’s crontab editor:
crontab -e
Add this line at the bottom (replace the path with your script’s actual location):
@reboot /path/to/your/startup-script.sh >> /var/log/django-startup.log 2>&1
This runs the script on reboot and logs all output to /var/log/django-startup.log—handy for debugging if something goes wrong.
Quick Note: Why Systemd is Better
While the crontab method works, systemd is more robust:
- It monitors Gunicorn and automatically restarts it if it crashes mid-run (not just on reboot)
- It handles dependencies properly (ensuring PostgreSQL starts before Gunicorn)
- It lets you easily check status with
sudo systemctl status gunicorn
Test it out by manually rebooting your server:
sudo reboot
After it comes back up, verify Gunicorn is running with sudo systemctl status gunicorn and visit your app to confirm it’s online.
内容的提问来源于stack exchange,提问作者Sidhartha

