144 lines
4.2 KiB
Bash
144 lines
4.2 KiB
Bash
#!/bin/bash
|
|
|
|
# Check for root privileges
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "This script must be run as root (use sudo)"
|
|
exit 1
|
|
fi
|
|
|
|
# Ensure SELinux management tools are present for permanent contexts
|
|
if ! command -v semanage &> /dev/null; then
|
|
echo "Installing policycoreutils for SELinux management..."
|
|
dnf install -y policycoreutils-python-utils
|
|
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
|
|
|
|
SITE_DIR="/var/www/$DOMAIN/public_html"
|
|
CONF_FILE="/etc/httpd/conf.d/$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 "<html><head><title>$DOMAIN</title></head><body><h1>Success! $DOMAIN is live on Fedora.</h1></body></html>" > "$SITE_DIR/index.html"
|
|
|
|
# Set Permissions and PERMANENT SELinux Contexts
|
|
chown -R apache:apache "/var/www/$DOMAIN"
|
|
semanage fcontext -a -t httpd_sys_content_t "/var/www/$DOMAIN(/.*)?" 2>/dev/null
|
|
restorecon -Rv "/var/www/$DOMAIN" >/dev/null
|
|
|
|
# Create the Apache Configuration File
|
|
cat <<EOF > "$CONF_FILE"
|
|
<VirtualHost *:80>
|
|
ServerName $DOMAIN
|
|
ServerAlias www.$DOMAIN
|
|
DocumentRoot $SITE_DIR
|
|
|
|
ErrorLog /var/log/httpd/$DOMAIN-error.log
|
|
CustomLog /var/log/httpd/$DOMAIN-access.log combined
|
|
|
|
<Directory $SITE_DIR>
|
|
AllowOverride All
|
|
Require all granted
|
|
</Directory>
|
|
</VirtualHost>
|
|
EOF
|
|
|
|
# Test and Restart Apache
|
|
echo "Testing Apache configuration..."
|
|
apachectl configtest
|
|
|
|
if [ $? -eq 0 ]; then
|
|
systemctl restart httpd
|
|
echo "----------------------------------------------------"
|
|
echo "Virtual Host Created Successfully!"
|
|
echo "Site Root: $SITE_DIR"
|
|
echo "Config File: $CONF_FILE"
|
|
echo "----------------------------------------------------"
|
|
else
|
|
echo "Configuration error detected. Rolling back config file..."
|
|
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/httpd/conf.d/$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 "Removing configurations and files..."
|
|
rm -f "$CONF_FILE"
|
|
rm -rf "$SITE_DIR"
|
|
|
|
# Clean up SELinux database rules for this directory
|
|
semanage fcontext -d -t httpd_sys_content_t "$SITE_DIR(/.*)?" 2>/dev/null
|
|
|
|
echo "Testing configuration and reloading Apache..."
|
|
apachectl configtest && systemctl restart httpd
|
|
echo "----------------------------------------------------"
|
|
echo "Virtual Host $DOMAIN has been completely removed."
|
|
echo "----------------------------------------------------"
|
|
else
|
|
echo "Deletion canceled."
|
|
fi
|
|
}
|
|
|
|
# --- Main Menu Loop ---
|
|
while true; do
|
|
echo ""
|
|
echo "================================="
|
|
echo " Fedora 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 |