Web Server-Specific Update Procedures

Web Server-Specific Update Procedures

Implement safe update procedures for Apache and Nginx:

#!/bin/bash
# /usr/local/bin/webserver-update.sh

# Function to safely update Apache
update_apache() {
    echo "Updating Apache..."
    
    # Check configuration before update
    apache2ctl configtest || {
        echo "Apache configuration error, aborting update"
        return 1
    }
    
    # Backup current configuration
    cp -a /etc/apache2 /etc/apache2.backup.$(date +%Y%m%d)
    
    # Update Apache
    apt-get update
    apt-get install --only-upgrade apache2
    
    # Test configuration after update
    apache2ctl configtest || {
        echo "Post-update configuration error, rolling back"
        rm -rf /etc/apache2
        mv /etc/apache2.backup.$(date +%Y%m%d) /etc/apache2
        return 1
    }
    
    # Graceful restart
    systemctl reload apache2
    
    echo "Apache updated successfully"
}

# Function to safely update Nginx
update_nginx() {
    echo "Updating Nginx..."
    
    # Check configuration before update
    nginx -t || {
        echo "Nginx configuration error, aborting update"
        return 1
    }
    
    # Backup current configuration
    cp -a /etc/nginx /etc/nginx.backup.$(date +%Y%m%d)
    
    # Update Nginx
    apt-get update
    apt-get install --only-upgrade nginx
    
    # Test configuration after update
    nginx -t || {
        echo "Post-update configuration error, rolling back"
        rm -rf /etc/nginx
        mv /etc/nginx.backup.$(date +%Y%m%d) /etc/nginx
        return 1
    }
    
    # Graceful restart
    systemctl reload nginx
    
    echo "Nginx updated successfully"
}