#!/bin/bash
# Check for root privileges
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root (use sudo)"
exit 1
fi
create_vhost() {
echo ""
read -p "Enter the domain name to CREATE (e.g., example.com): " DOMAIN
if [[ -z "$DOMAIN" ]]; then
echo "Domain cannot be empty."
return
fi
# Debian/Ubuntu conventions use www-data as the web user
SITE_DIR="/var/www/$DOMAIN/public_html"
CONF_FILE="/etc/apache2/sites-available/$DOMAIN.conf"
if [ -f "$CONF_FILE" ]; then
echo "Warning: A virtual host for $DOMAIN already exists!"
return
fi
echo "Creating Virtual Host directories for $DOMAIN..."
mkdir -p "$SITE_DIR"
# Create a basic index.html for testing
echo "
$DOMAINSuccess! $DOMAIN is live on Debian-based Linux.
" > "$SITE_DIR/index.html"
# Set Permissions (Debian uses www-data user/group)
chown -R www-data:www-data "/var/www/$DOMAIN"
# Create the Apache Configuration File
cat < "$CONF_FILE"
ServerName $DOMAIN
ServerAlias www.$DOMAIN
DocumentRoot $SITE_DIR
ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-error.log
CustomLog \${APACHE_LOG_DIR}/$DOMAIN-access.log combined
AllowOverride All
Require all granted
EOF
# Enable the site using Debian's native tool
echo "Enabling site configuration..."
a2ensite "$DOMAIN.conf" > /dev/null
# Test and Reload Apache
echo "Testing Apache configuration..."
apache2ctl configtest
if [ $? -eq 0 ]; then
systemctl reload apache2
echo "----------------------------------------------------"
echo "Virtual Host Created Successfully!"
echo "Site Root: $SITE_DIR"
echo "Config File: $CONF_FILE"
echo "----------------------------------------------------"
else
echo "Configuration error detected. Rolling back changes..."
a2dissite "$DOMAIN.conf" > /dev/null
rm -f "$CONF_FILE"
fi
}
delete_vhost() {
echo ""
read -p "Enter the domain name to DELETE (e.g., example.com): " DOMAIN
if [[ -z "$DOMAIN" ]]; then
echo "Domain cannot be empty."
return
fi
SITE_DIR="/var/www/$DOMAIN"
CONF_FILE="/etc/apache2/sites-available/$DOMAIN.conf"
if [ ! -f "$CONF_FILE" ] && [ ! -d "$SITE_DIR" ]; then
echo "Error: No Virtual Host or directory configuration found for $DOMAIN."
return
fi
echo "CRITICAL WARNING: This will permanently delete:"
echo " - Configuration: $CONF_FILE"
echo " - All web files in: $SITE_DIR"
read -p "Are you absolutely sure you want to proceed? (y/n): " CONFIRM
if [[ "$CONFIRM" =~ ^[yY]([eE][sS])?$ ]]; then
echo "Disabling site and removing configurations..."
# Disable the site first so Apache stops looking for it
a2dissite "$DOMAIN.conf" > /dev/null 2>&1
rm -f "$CONF_FILE"
rm -rf "$SITE_DIR"
echo "Testing configuration and reloading Apache..."
apache2ctl configtest && systemctl reload apache2
echo "----------------------------------------------------"
echo "Virtual Host $DOMAIN has been completely removed."
echo "----------------------------------------------------"
else
echo "Deletion canceled."
fi
}
# --- Main Menu Loop ---
while true; do
echo ""
echo "================================="
echo " Debian Apache Site Manager "
echo "================================="
echo "1) Create a New Virtual Host"
echo "2) Delete an Existing Virtual Host"
echo "3) Exit"
echo "================================="
read -p "Choose an option (1-3): " MENU_CHOICE
case "$MENU_CHOICE" in
1)
create_vhost
;;
2)
delete_vhost
;;
3)
echo "Exiting Site Manager. Goodbye!"
break
;;
*)
echo "Invalid choice. Please select 1, 2, or 3."
;;
esac
done