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

DigitalOcean Ubuntu服务器磁盘耗尽致500错误,求助解决避免复发

Hey there, glad you got your site back up and running with the backup! Let’s make sure that annoying "No space left on device" issue doesn’t crash your site again. Here’s a practical, step-by-step plan to diagnose hidden disk hogs and set up safeguards:

1. First: Map Out Where Your Disk Space Is Going

Start by running these commands to identify exactly what’s eating up space:

  • df -h: Shows overall disk usage for all mounted partitions (focus on the / partition’s usage percentage)
  • du -sh /*: Lists the size of every top-level directory to narrow down big, space-hungry folders
  • sudo du -sh /var/*: Zero in on /var first—it’s almost always the culprit (logs, databases, package caches live here)
  • For a more interactive, easy-to-navigate view, install ncdu with sudo apt install ncdu, then run sudo ncdu / to browse disk usage visually
2. Fix Common Disk Bloat Culprits

Once you’ve found the big folders, tackle these most likely offenders:

  • Uncontrolled Log Files: System logs (/var/log/) or app logs (Nginx/Apache, your framework’s logs) can grow indefinitely.
    • Clean systemd logs with sudo journalctl --vacuum-size=100M (adjust the size to your needs)
    • Check /etc/logrotate.d/ to ensure log rotation is enabled for your apps (most default Ubuntu configs have this, but verify it’s set to compress old logs and delete them after 7-14 days)
  • Stale Packages & Caches:
    • Clear unused dependencies: sudo apt autoremove -y
    • Delete downloaded package archives: sudo apt clean
    • Remove outdated package caches: sudo apt autoclean
  • Database Junk:
    • For MySQL/MariaDB: Purge old binary logs with PURGE BINARY LOGS BEFORE 'YYYY-MM-DD 00:00:00'; (replace with a date 2-3 weeks ago) and optimize bloated tables with OPTIMIZE TABLE your_table_name; (run during low traffic!)
    • For PostgreSQL: Run VACUUM ANALYZE; to clean up dead data tuples
  • Old Backups: Double-check if leftover backup files are lingering in /root/, /home/, or your backup directory—delete any that aren’t needed
  • Temporary Files: Clear /tmp/ with sudo rm -rf /tmp/* (files here are deleted on reboot, but manual cleanup won’t hurt)
3. Set Up Preventive Safeguards

Don’t wait for the disk to fill up again—set these up now:

  • Disk Usage Alerts: Create a simple bash script to email you when disk usage hits 80%:
    #!/bin/bash
    THRESHOLD=80
    DISK_USAGE=$(df -h / | grep / | awk '{print $5}' | sed 's/%//g')
    if [ $DISK_USAGE -ge $THRESHOLD ]; then
        echo "Disk usage on $(hostname) is at ${DISK_USAGE}% - take action!" | mail -s "URGENT: Disk Space Alert" your-email@example.com
    fi
    
    Make it executable with chmod +x /path/to/disk-alert.sh, then add it to crontab (crontab -e) to run daily:
    0 9 * * * /path/to/disk-alert.sh
  • Automated Maintenance: Add a weekly cleanup job to crontab to keep things tidy:
    0 10 * * 0 sudo apt autoremove -y && sudo apt clean && sudo journalctl --vacuum-size=100M
  • Resize the Disk (If Needed): If your app is legitimately outgrowing the current disk, DigitalOcean lets you resize volumes online. After resizing, expand the file system with sudo resize2fs /dev/vda1 (replace /dev/vda1 with your actual disk device—find it with df -h)
4. Quick Current Status Check

Before wrapping up, run these to confirm you’re not sitting on a ticking time bomb:

  • df -h: Confirm your root partition is under 80% usage
  • sudo du -sh /var/log: Ensure logs aren’t already creeping back up
  • sudo ls -la /var/lib/mysql | grep -E "binlog|ib_logfile": Check for oversized database log files

By following these steps, you’ll eliminate hidden disk hogs and set up safeguards to keep your site running smoothly. If you hit any snags while checking specific folders or configuring alerts, feel free to share the output of the commands and I’ll help you troubleshoot further!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:54:38