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

如何实现DigitalOcean Ubuntu服务器重启后自动重启Django应用

Fixing Auto-Restart for Your Django App (Gunicorn+Nginx+PostgreSQL) on Ubuntu

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:

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-username to the user who owns your Django project
  • Double-check the WorkingDirectory and ExecStart paths match your virtual environment and project location
  • The After=postgresql.service line 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:40:51