Skip to content

Instantly share code, notes, and snippets.

@azer
Created October 27, 2024 07:16
Show Gist options
  • Save azer/2d75e021f1966563299e93862e0f60c5 to your computer and use it in GitHub Desktop.
Save azer/2d75e021f1966563299e93862e0f60c5 to your computer and use it in GitHub Desktop.
nginx_manage.sh
#!/bin/bash
command=$1
domain=$2
show_help() {
echo "Usage: $0 <command> <domain>"
echo ""
echo "Commands:"
echo " create <domain> Create a new website configuration"
echo " remove <domain> Remove website configuration (preserves content)"
echo " list List all configured domains"
echo " help Show this help message"
echo ""
echo "Example:"
echo " $0 create example.com"
echo " $0 remove example.com"
}
if [ "$1" = "help" ] || [ -z "$1" ]; then
show_help
exit 0
fi
if [ "$command" = "list" ]; then
echo "Configured domains:"
ls -1 /etc/nginx/sites-enabled/ | grep -v "default"
exit 0
fi
if [ -z "$domain" ] && [ "$command" != "list" ]; then
echo "Error: Domain name is required"
show_help
exit 1
fi
case $command in
"create")
# Create domain directory
mkdir -p "$HOME/dev/www/$domain"
# Create default index.html
cat > "$HOME/dev/www/$domain/index.html" << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to $domain</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f5f5f5;
}
.container {
background: white;
padding: 2rem 3rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
text-align: center;
max-width: 600px;
}
h1 {
color: #2d3748;
margin-bottom: 1rem;
}
p {
color: #4a5568;
margin-bottom: 2rem;
}
.domain {
font-family: monospace;
background: #edf2f7;
padding: 0.2rem 0.5rem;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to <span class="domain">$domain</span></h1>
<p>Your website is ready to be built!</p>
</div>
</body>
</html>
EOF
# Create nginx config
sudo tee "/etc/nginx/sites-available/$domain" > /dev/null << EOF
server {
listen 80;
listen [::]:80;
root $HOME/dev/www/$domain;
index index.html index.htm;
server_name $domain www.$domain;
location / {
try_files \$uri \$uri/ =404;
}
}
EOF
# Enable site
sudo ln -sf "/etc/nginx/sites-available/$domain" "/etc/nginx/sites-enabled/$domain"
sudo nginx -t && sudo systemctl reload nginx
echo "✅ Site $domain has been created and configured!"
echo "📂 Content directory: ~/dev/www/$domain"
echo "🔧 Config file: /etc/nginx/sites-available/$domain"
;;
"remove")
# Remove nginx config
sudo rm -f "/etc/nginx/sites-enabled/$domain"
sudo rm -f "/etc/nginx/sites-available/$domain"
sudo nginx -t && sudo systemctl reload nginx
echo "🗑️ Configuration for $domain has been removed"
echo "📂 Content remains at ~/dev/www/$domain"
;;
*)
echo "❌ Unknown command: $command"
show_help
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment