Skip to content

Instantly share code, notes, and snippets.

@seifsallam
Forked from joequery/gist:1607063
Last active December 19, 2015 10:29
Show Gist options
  • Save seifsallam/5941104 to your computer and use it in GitHub Desktop.
Save seifsallam/5941104 to your computer and use it in GitHub Desktop.
Enable disable a Nginx configuration from `/sites-available`
##
## NGINX helpers
##
# Kill and restart nginx
function restart_nginx(){
pids=$(pidof nginx)
if [[ -n $pids ]];
then
sudo kill -9 $pids
sudo service nginx restart
fi
}
# Create a symbolic link to a config file in the sites-enabled dir
# arg1: basename of config file in sites-available dir
# Ex: 'nginx_enable railsapp1.com'
function nginx_enable(){
confFile=$1
fullFilePath=/etc/nginx/sites-available/$confFile
symLinkPath=/etc/nginx/sites-enabled/$confFile
# First test to see that the file exists
if [ ! -e $fullFilePath ]
then
printf "%s not found..." "$fullFilePath"
printf "Aborted!\n"
else
# If symlink already exists, delete it so the new configuration
# will take effect.
if [ -e $symLinkPath ]
then
printf "Old symbolic link removed...\n"
sudo rm $symLinkPath
fi
sudo ln -s $fullFilePath $symLinkPath
# Confirm the symlink was created
if [ -e $symLinkPath ]
then
printf "$confFile enabled.\n"
fi
fi
}
# Destroy a symbolic link in the sites-enabled directory.
# arg1: basename of symbolic link to
# Ex: 'nginx_disable railsapp1.com'
function nginx_disable(){
symLink=$1
symLinkPath=/etc/nginx/sites-enabled/$symLink
# First test to see that the link exists
if [ ! -e $symLinkPath ]
then
printf "$symLink is not currently enabled. Exiting.\n"
else
# If the link does exist, delete it.
sudo rm $symLinkPath
# Confirm the symlink was destroyed
if [ ! -e $symLinkPath ]
then
printf "$symLink disabled.\n"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment