diff --git a/add_vhost.sh b/add_vhost.sh new file mode 100644 index 0000000..f3961ff --- /dev/null +++ b/add_vhost.sh @@ -0,0 +1,79 @@ +#!/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() { + # 1. Prompt for the domain name + read -p "Enter the domain name (e.g., example.com): " DOMAIN + + # Define paths + SITE_DIR="/var/www/$DOMAIN/public_html" + CONF_FILE="/etc/httpd/conf.d/$DOMAIN.conf" + + echo "Creating Virtual Host for $DOMAIN..." + + # 2. Create directory structure + mkdir -p "$SITE_DIR" + + # 3. Create a basic index.html for testing + echo "$DOMAIN

Success! $DOMAIN is live on Fedora.

" > "$SITE_DIR/index.html" + + # 4. Set Permissions and SELinux + # Change ownership to Apache user + chown -R apache:apache "/var/www/$DOMAIN" + # Set SELinux context so Apache can read the files + chcon -R -t httpd_sys_content_t "/var/www/$DOMAIN" + + # 5. Create the Apache Configuration File + cat < "$CONF_FILE" + + ServerName $DOMAIN + ServerAlias www.$DOMAIN + DocumentRoot $SITE_DIR + + ErrorLog /var/log/httpd/$DOMAIN-error.log + CustomLog /var/log/httpd/$DOMAIN-access.log combined + + + AllowOverride All + Require all granted + + +EOF + + # 6. Test and Restart Apache + echo "Testing configuration..." + apachectl configtest + + if [ $? -eq 0 ]; then + echo "Syntax OK. Restarting Apache..." + 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. Please check $CONF_FILE" + fi +} + +# --- Main Execution Loop --- +while true; do + create_vhost + + read -p "Would you like to create another virtual host? (y/n): " CHOICE + case "$CHOICE" in + [yY][eE][sS]|[yY]) + echo "Starting next setup..." + ;; + *) + echo "Exiting Virtual Host creator." + break + ;; + esac +done