Skip to content

Instantly share code, notes, and snippets.

@mikedevita
Last active March 29, 2024 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikedevita/ad7b747d8aa327af3ed80421ad61e0cd to your computer and use it in GitHub Desktop.
Save mikedevita/ad7b747d8aa327af3ed80421ad61e0cd to your computer and use it in GitHub Desktop.
Enable or disable NGINX Upstream & vhost shell script

These scripts will manage the sym link's for a given nginx vhost app and it's upstreams based on the arguments provided.

How to use

  1. To enable, run script ./enable.sh app_name [app_domain_name]
  2. To disable, run script ./disable.sh app_name [app_domain_name]

Arguments

ARGUMENT DESCRIPTION
app_name the name of the app, it should match the sub domain and before the _upstream for the upstream.
app_domain_name optional and defaults to domain.com

Running ./enable.sh img will create two symlinks

  1. /etc/nginx/upstreams-enabled/img_upstream.conf
  2. /etc/nginx/sites-enabled/img.domain.com

Running ./disable.sh img will remove two symlinks

  1. /etc/nginx/upstreams-enabled/img_upstream.conf
  2. /etc/nginx/sites-enabled/img.domain.com
#!/bin/bash
# Get the first argument (APP_NAME)
APP_NAME="$1"
# Get the second argument (DOMAIN_NAME) or default to "domain.com"
DOMAIN_NAME="${2:-domain.com}"
# Define paths for upstream and site configuration files
UPSTREAM_ENABLED="/etc/nginx/upstreams-enabled/${APP_NAME}_upstream.conf"
SITE_ENABLED="/etc/nginx/sites-enabled/${APP_NAME}.${DOMAIN_NAME}"
# Check if the upstream symlink exists
if [ -L "$UPSTREAM_ENABLED" ]; then
# Remove the upstream symlink
rm "$UPSTREAM_ENABLED"
echo "Upstream symlink removed for $APP_NAME.$DOMAIN_NAME."
else
echo "Upstream symlink does not exist for $APP_NAME.$DOMAIN_NAME."
fi
# Check if the site symlink exists
if [ -L "$SITE_ENABLED" ]; then
# Remove the site symlink
rm "$SITE_ENABLED"
echo "Site symlink removed for $APP_NAME.$DOMAIN_NAME"
else
echo "Site symlink does not exist for $APP_NAME.$DOMAIN_NAME."
fi
#!/bin/bash
# Get the first argument (APP_NAME)
APP_NAME="$1"
# Get the second argument (DOMAIN_NAME) or default to "domain.com"
DOMAIN_NAME="${2:-domain.com}"
# Define paths for upstream and site configuration files
UPSTREAM_AVAILABLE="/etc/nginx/upstreams-available/${APP_NAME}_upstream.conf"
UPSTREAM_ENABLED="/etc/nginx/upstreams-enabled/${APP_NAME}_upstream.conf"
SITE_AVAILABLE="/etc/nginx/sites-available/${APP_NAME}.${DOMAIN_NAME}.conf"
SITE_ENABLED="/etc/nginx/sites-enabled/${APP_NAME}.${DOMAIN_NAME}"
# Check if the upstream configuration file doesn't exist and the upstream available configuration file does exist
if [ ! -f "$UPSTREAM_ENABLED" ] && [ -f "$UPSTREAM_AVAILABLE" ]; then
# Create symlink for upstream
ln -s "$UPSTREAM_AVAILABLE" "$UPSTREAM_ENABLED"
echo "Upstream symlink created for $APP_NAME"
elif [ ! -f "$UPSTREAM_AVAILABLE" ]; then
echo "Skipping symlink creation. Upstream configuration file does not exist for $APP_NAME.$DOMAIN_NAME."
else
echo "Skipping upstream symlink creation. Upstream configuration file already exists for $APP_NAME.$DOMAIN_NAME."
fi
# Check if the site configuration file exists
if [ ! -f "$SITE_ENABLED" ] && [ -f "$SITE_AVAILABLE" ]; then
# Create symlink for site configuration
ln -s "$SITE_AVAILABLE" "$SITE_ENABLED"
echo "Site symlink created for $APP_NAME.$DOMAIN_NAME"
elif [ ! -f "$SITE_AVAILABLE" ]; then
echo "Skipping symlink creation, Site configuration file does not exist for $APP_NAME."
else
echo "Skipping symlink creation, Site symlink already exists for $APP_NAME.$DOMAIN_NAME."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment