80 lines
2.1 KiB
Bash
80 lines
2.1 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
|
|
|
|
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 "<html><head><title>$DOMAIN</title></head><body><h1>Success! $DOMAIN is live on Fedora.</h1></body></html>" > "$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 <<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
|
|
|
|
# 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
|