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:
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 folderssudo du -sh /var/*: Zero in on/varfirst—it’s almost always the culprit (logs, databases, package caches live here)- For a more interactive, easy-to-navigate view, install
ncduwithsudo apt install ncdu, then runsudo ncdu /to browse disk usage visually
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)
- Clean systemd logs with
- Stale Packages & Caches:
- Clear unused dependencies:
sudo apt autoremove -y - Delete downloaded package archives:
sudo apt clean - Remove outdated package caches:
sudo apt autoclean
- Clear unused dependencies:
- 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 withOPTIMIZE TABLE your_table_name;(run during low traffic!) - For PostgreSQL: Run
VACUUM ANALYZE;to clean up dead data tuples
- For MySQL/MariaDB: Purge old binary logs with
- 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/withsudo rm -rf /tmp/*(files here are deleted on reboot, but manual cleanup won’t hurt)
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%:
Make it executable with#!/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 fichmod +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/vda1with your actual disk device—find it withdf -h)
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% usagesudo du -sh /var/log: Ensure logs aren’t already creeping back upsudo 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

