#!/bin/bash DOMAIN="$1" WEBROOT="/var/www/html/$DOMAIN" APACHE_CONF="/etc/apache2/sites-available/$DOMAIN.conf" if [ -z "$DOMAIN" ]; then echo "Usage: $0 example.com" exit 1 fi # Count dots in domain DOT_COUNT=$(grep -o "\." <<< "$DOMAIN" | wc -l) # Only add www. if domain has exactly 1 dot (example.com) if [ "$DOT_COUNT" -eq 1 ]; then WWW_DOMAIN="www.$DOMAIN" else WWW_DOMAIN="" fi echo ">>> Creating web root at $WEBROOT" sudo mkdir -p "$WEBROOT" sudo chown -R $USER:$USER "$WEBROOT" echo ">>> Creating sample index page" echo "

$DOMAIN is live!

" | sudo tee "$WEBROOT/index.html" echo ">>> Creating Apache config at $APACHE_CONF" sudo bash -c "cat > $APACHE_CONF" < ServerName $DOMAIN EOF # Only add ServerAlias if needed if [ -n "$WWW_DOMAIN" ]; then sudo bash -c "cat >> $APACHE_CONF" <> $APACHE_CONF" < Options Indexes FollowSymLinks AllowOverride All Require all granted RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-error.log CustomLog \${APACHE_LOG_DIR}/$DOMAIN-access.log combined EOF echo ">>> Enabling modules" sudo a2enmod rewrite sudo a2enmod ssl echo ">>> Enabling site" sudo a2ensite "$DOMAIN.conf" echo ">>> Reloading Apache" sudo systemctl reload apache2 echo ">>> Running Certbot" if [ -n "$WWW_DOMAIN" ]; then sudo certbot --apache -d "$DOMAIN" -d "$WWW_DOMAIN" --non-interactive --agree-tos -m admin@$DOMAIN else sudo certbot --apache -d "$DOMAIN" --non-interactive --agree-tos -m admin@$DOMAIN fi echo ">>> Done!" echo "Site available at: https://$DOMAIN"