Skip to content

Instantly share code, notes, and snippets.

@amanjuman
Last active February 16, 2020 18:11
Show Gist options
  • Save amanjuman/8fc600356fccb8057abde61d4df42cba to your computer and use it in GitHub Desktop.
Save amanjuman/8fc600356fccb8057abde61d4df42cba to your computer and use it in GitHub Desktop.
Nginx vhost control scripts
#!/bin/bash
# Name: n2dissite / n2ensite
# Desc: Nginx vhost control scripts
# Ver: 1.0
CONFDIR="/etc/nginx/"
EN="$CONFDIR/sites-enabled/"
AV="$CONFDIR/sites-available/"
if [[ `basename $0` == "n2ensite" ]] ; then
mode="en"
elif [[ `basename $0` == "n2dissite" ]] ; then
mode="dis"
else
echo "Unknown command"
exit 1
fi
if [[ "$#" -lt 1 ]] ; then
echo "Usage: $0 <domain>"
if [[ "$mode" == "dis" ]] ; then
echo "List of enabled domains:"
/bin/ls $EN
else
echo "List of available domains:"
/bin/ls $AV
fi
exit 0
elif [[ $mode == "en" ]] && [ -f $AV$1 ] && ( [ ! -f $EN$1 ] ) ; then
if nginx -t &> /dev/null ; then
[[ $2 == "-f" ]] && rm $EN$1 &> /dev/null
ln -s $AV$1 $EN$1
if nginx -t &> /dev/null || [[ $2 == "-f" ]] ; then
echo "Reload nginx with 'service nginx reload' for the changes to take effect."
exit 0
else
rm $EN$1
echo "Error in the virtualhost config, disabled again."
exit 1
fi
else
echo "Error in the Nginx configuration, not enabling the virtualhost."
exit 2
fi
elif [ -f $EN$1 ] && [[ $mode = "dis" ]] ; then
rm $EN$1
echo "$1 is disabled. Reload nginx with 'service nginx reload' for the changes to take effect."
exit 0
elif [ -f $EN$1 ] ; then
echo "$1 exists, cannot enable."
exit 1
else
echo "Virtualhost config not found."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment