Skip to content

Instantly share code, notes, and snippets.

@ilovefreesw
Last active September 2, 2025 07:36
Show Gist options
  • Save ilovefreesw/5c803afd9a4822766adfa9a79af09837 to your computer and use it in GitHub Desktop.
Save ilovefreesw/5c803afd9a4822766adfa9a79af09837 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ==============================================================================
# Apache HTTPS Reverse Proxy Setup Script (Enhanced)
#
# This script automates the setup of an Apache2 reverse proxy with a free
# Let's Encrypt SSL certificate. It avoids SSL config issues by letting
# Certbot create the HTTPS vhost, then injecting proxy settings.
#
# Usage: sudo ./setup_domain.sh <your_domain> <local_port> <your_email>
# Example: sudo ./setup_domain.sh myapp.example.com 8000 [email protected]
# ==============================================================================
set -e # exit on error
DOMAIN=$1
PORT=$2
EMAIL=$3
APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
CONFIG_FILE="$APACHE_SITES_AVAILABLE/$DOMAIN.conf"
SSL_CONFIG_FILE="$APACHE_SITES_AVAILABLE/$DOMAIN-le-ssl.conf"
# --- Pre-flight Checks ---
if [ "$(id -u)" -ne 0 ]; then
echo "❌ This script must be run as root. Please use sudo." >&2
exit 1
fi
if [ "$#" -ne 3 ]; then
echo "❌ Usage: $0 <domain> <port> <email>"
exit 1
fi
if [ -f "$CONFIG_FILE" ]; then
echo "⚠️ Config file $CONFIG_FILE already exists."
read -p "Do you want to overwrite it? (y/n) " -n 1 -r
echo
[[ ! $REPLY =~ ^[Yy]$ ]] && echo "Aborting." && exit 1
fi
# --- Installation and Setup ---
echo "▶️ Installing Apache and Certbot if missing..."
apt-get update -qq
apt-get install -y apache2 certbot python3-certbot-apache > /dev/null
echo "✅ Apache and Certbot ready."
echo "▶️ Enabling Apache modules..."
a2enmod proxy proxy_http ssl rewrite headers socache_shmcb > /dev/null
echo "✅ Modules enabled."
echo "▶️ Creating initial Apache config at $CONFIG_FILE..."
cat <<EOF > "$CONFIG_FILE"
<VirtualHost *:80>
ServerName $DOMAIN
DocumentRoot /var/www/html
ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-error.log
CustomLog \${APACHE_LOG_DIR}/$DOMAIN-access.log combined
</VirtualHost>
EOF
echo "✅ Config file created."
echo "▶️ Enabling site..."
a2ensite "$DOMAIN.conf" > /dev/null
systemctl reload apache2
echo "✅ Site enabled and Apache reloaded."
echo "▶️ Requesting SSL certificate from Let's Encrypt..."
certbot --apache --non-interactive --agree-tos --redirect --email "$EMAIL" -d "$DOMAIN"
echo "✅ SSL certificate installed."
# --- Add Proxy Configuration ---
if [ -f "$SSL_CONFIG_FILE" ]; then
echo "▶️ Adding reverse proxy settings to $SSL_CONFIG_FILE..."
sed -i "/<\/VirtualHost>/i \
ProxyPreserveHost On\n \
ProxyPass / http://127.0.0.1:$PORT/ retry=0\n \
ProxyPassReverse / http://127.0.0.1:$PORT/\n \
" "$SSL_CONFIG_FILE"
else
echo "❌ Could not find SSL config file $SSL_CONFIG_FILE."
echo "You may need to manually add proxy settings."
exit 1
fi
echo "▶️ Checking Apache config..."
apache2ctl configtest
echo "▶️ Reloading Apache..."
systemctl reload apache2
# --- Completion ---
echo ""
echo "🎉 All done! Your site is live."
echo "👉 URL: https://$DOMAIN"
echo ""
@ilovefreesw
Copy link
Author

Create Renewal Cron Job every 2 months : 0 3 */75 * * certbot renew --quiet --post-hook "systemctl reload apache2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment