Skip to content

Instantly share code, notes, and snippets.

@xram64
Created March 12, 2024 21:31
Show Gist options
  • Select an option

  • Save xram64/027015c91122189cdfe7fbeb8c3be409 to your computer and use it in GitHub Desktop.

Select an option

Save xram64/027015c91122189cdfe7fbeb8c3be409 to your computer and use it in GitHub Desktop.
Nginx script to automatically create links in `sites-enabled` to mirror all files in `sites-available`.
#!/bin/bash
## Nginx script to automatically create links in `sites-enabled` to mirror all files in `sites-available`.
## xram | 3/12/24
# TODO:
# - Add an option to automatically test and/or reload Nginx config.
# ============================================================================ #
# Define paths for Nginx directories
SITES_AVAILABLE="/etc/nginx/sites-available"
SITES_ENABLED="/etc/nginx/sites-enabled"
# Ensure script is run as root to avoid permission issues
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root." 1>&2
exit 1
fi
# Remove any existing links in `sites-enabled`
echo "Removing existing links in $SITES_ENABLED..."
find "$SITES_ENABLED" -type l -exec rm {} \;
# Create a new link in `sites-enabled` for each file in `sites-available`
for site in "$SITES_AVAILABLE"/*; do
site_name=$(basename "$site")
ln -s "$SITES_AVAILABLE/$site_name" "$SITES_ENABLED/$site_name"
echo "Added link to $site"
done
echo "Testing new configuration..."
# Test Nginx configuration
nginx -t
# Reload Nginx
#systemctl reload nginx
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment