#!/bin/bash # Fix Apache Configuration Script # This script fixes common Apache issues after the main setup set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color error() { echo -e "${RED}ERROR: $1${NC}" >&2 exit 1 } info() { echo -e "${BLUE}INFO: $1${NC}" } success() { echo -e "${GREEN}SUCCESS: $1${NC}" } warning() { echo -e "${YELLOW}WARNING: $1${NC}" } # Check if running as root if [[ $EUID -ne 0 ]]; then error "This script must be run as root" fi info "Fixing Apache configuration..." # Stop any conflicting services info "Stopping conflicting services..." if systemctl is-active --quiet nginx 2>/dev/null; then systemctl stop nginx systemctl disable nginx success "Nginx stopped and disabled" fi if pgrep nginx > /dev/null; then pkill nginx success "Killed remaining nginx processes" fi # Stop Apache systemctl stop apache2 2>/dev/null || true # Check what's listening on ports 80 and 443 info "Checking port usage..." if ss -tlnp | grep -q ":80 "; then warning "Something is listening on port 80:" ss -tlnp | grep ":80 " fi if ss -tlnp | grep -q ":443 "; then warning "Something is listening on port 443:" ss -tlnp | grep ":443 " fi # Enable required Apache modules info "Enabling Apache modules..." a2enmod ssl a2enmod rewrite a2enmod proxy a2enmod proxy_fcgi a2enmod setenvif # Enable PHP-FPM info "Configuring PHP-FPM..." a2enconf php*-fpm systemctl enable php*-fpm systemctl restart php*-fpm # Check if PostfixAdmin site exists and enable it if [[ -f "/etc/apache2/sites-available/postfixadmin.conf" ]]; then a2ensite postfixadmin success "PostfixAdmin site enabled" else warning "PostfixAdmin site configuration not found" fi # Disable default site a2dissite 000-default 2>/dev/null || true # Test Apache configuration info "Testing Apache configuration..." if apachectl configtest; then success "Apache configuration is valid" else error "Apache configuration has errors" fi # Start Apache info "Starting Apache..." systemctl enable apache2 systemctl start apache2 # Wait for Apache to start sleep 3 # Check if Apache is running if systemctl is-active --quiet apache2; then success "Apache is running" else error "Apache failed to start" fi # Check listening ports info "Checking Apache listening ports..." if ss -tlnp | grep -q ":80.*apache"; then success "Apache is listening on port 80" else warning "Apache is not listening on port 80" fi if ss -tlnp | grep -q ":443.*apache"; then success "Apache is listening on port 443" else warning "Apache is not listening on port 443" fi # Show Apache status info "Apache status:" systemctl status apache2 --no-pager # Show enabled sites info "Enabled Apache sites:" a2ensite 2>&1 | grep "already enabled" || echo "No sites explicitly enabled" info "Apache configuration fix completed!" info "You can check Apache error logs with: journalctl -u apache2 -f" info "You can also check: /var/log/apache2/error.log"