Fixing Digital Ocean's Out of memory issues and set up auto-recovery

Bash scriptswordpresssshapache

Digital Ocean droplets, specially Wordpress sites are low-memory VPS instances. As result, they frequently crash due to Out of Memory (OOM) errors. This guide explains how to diagnose OOM crashes, configure Apache for limited resources, and implement automated recovery.

Diagnosing OOM Issues

Check system logs for OOM killer activity:

        
        
          
            
          
          
            
          
        

        sudo journalctl --since "7 days ago" | grep -i "out of memory\|oom\|killed"

# Look for entries like:
Sep 23 06:52:13 server systemd[1]: system.slice: A process of this unit has been killed by the OOM killer.
      

Check Apache service status:

        
        
          
            
          
          
            
          
        

        systemctl status apache2`
      

If the output shows Result: oom-kill, Apache was terminated by the OOM killer.

Check current memory usage

        
        
          
            
          
          
            
          
        

        free -h
df -h
      

How to Configure Apache for Low-Memory Servers

The default Apache configuration often allows too many concurrent processes for budget VPS instances.

Modify MPM Prefork Settings

        
        
          
            
          
          
            
          
        

        sudo nano /etc/apache2/mods-available/mpm_prefork.conf
      

Replace the content with optimized settings for 1GB RAM:

        
        
          
            
          
          
            
          
        

        <IfModule mpm_prefork_module>
    StartServers            2
    MinSpareServers         2
    MaxSpareServers         5
    MaxRequestWorkers       15
    MaxConnectionsPerChild  1000
</IfModule>
      

Apply changes:

        
        
          
            
          
          
            
          
        

        sudo systemctl reload apache2
      

How to Add Swap Space

Swap space acts as emergency memory when physical RAM is exhausted. Without swap, the OOM killer immediately terminates processes when memory runs out. With swap configured, the system can temporarily use disk space as memory, preventing sudden process termination and allowing time for automatic recovery.

Create and enable a 2GB swap file:

        
        
          
            
          
          
            
          
        

        sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
      

Make swap permanent:

        
        
          
            
          
          
            
          
        

        echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
      

Verify swap is active:

        
        
          
            
          
          
            
          
        

        free -h
      

How to Implement Automated Recovery

The monitoring script provides:

  • Service health monitoring
  • Automatic service restart
  • Memory usage alerts
  • Email notifications via Resend API
  • Website response testing

Installation Steps

Download and save the script:

        
        
          
            
          
          
            
          
        

        sudo nano /usr/local/bin/service-monitor.sh
      

Configure email settings in the script:

        
        
          
            
          
          
            
          
        

        RESEND_API_KEY="your_api_key_here"
FROM_EMAIL="[email protected]"
TO_EMAIL="[email protected]"
SERVER_NAME="production-server"
      

Make it executable:

        
        
          
            
          
          
            
          
        

        sudo chmod +x /usr/local/bin/service-monitor.sh
      

Test the script:

        
        
          
            
          
          
            
          
        

        sudo /usr/local/bin/service-monitor.sh
cat /var/log/service-monitor.log
      

Schedule with cron (runs every 5 minutes):

        
        
          
            
          
          
            
          
        

        sudo crontab -e
      

Add this line:

        
        
          
            
          
          
            
          
        

        */5 * * * * /usr/local/bin/service-monitor.sh
      

Email Alert Configuration

The script uses Resend API for email notifications. Get an API key from resend.com and update the configuration variables.

The script sends alerts for:

  • Apache service failures
  • Website unresponsive
  • High memory usage (>85%)
  • Service recovery confirmations

Results

This configuration typically reduces memory usage from 90%+ to 35-60% and eliminates OOM crashes. The monitoring script provides automatic recovery within 30 seconds of detecting issues.

The approach prevents extended downtime by addressing both the root cause (excessive Apache processes) and implementing proactive monitoring with automated remediation.