Skip to content

Instantly share code, notes, and snippets.

@djrobby
Forked from kshep92/.bashrc
Created April 13, 2021 17:10
Show Gist options
  • Save djrobby/ad261673d46c22d45de4dbe8291d36cc to your computer and use it in GitHub Desktop.
Save djrobby/ad261673d46c22d45de4dbe8291d36cc to your computer and use it in GitHub Desktop.
Some handy functions for managing NginX
alias nx='/bin/bash ~/.scripts/nx.sh' # NginX scripts
# File stored in ~/.scripts/nx.sh
# Add a vhost
add() {
local vhost=$1
local conf="/etc/nginx/sites-available/$vhost.conf"
local template="$(dirname $0)/vhost-template.conf"
if [ -z $vhost ]; then
echo "Please enter a vhost name. Use the form 'nx -a [vhost]'"
return 1
fi
sudo cp $template $conf
echo "Created vhost $vhost"
sudo nano $conf
enable $vhost
}
# Enable a vhost
enable() {
local vhost=$1
local conf="/etc/nginx/sites-available/$vhost.conf"
if [ -z $vhost ]; then
echo "Please enter a vhost name. Use the form 'nx enable [vhost]'."
return 1
fi
if ! [ -e $conf ]; then # If the config file doesn't exist, exit.
echo "$conf does not exist."
return 1
fi
if ! [ -e "/etc/nginx/sites-enabled/$vhost.conf" ]; then # If the symbolic link doesn't exist, create it.
sudo ln -s $conf /etc/nginx/sites-enabled/
fi
if test ; then #If the test passes, enable the vhost file.
# @nx-reload # Not sure if I need to reload at this point.
echo "$vhost is now enabled."
else
echo "Test failed: $vhost not enabled. Run nx edit $vhost to amend, and then run nx enable $vhost again."
fi
}
# Disable a vhost
disable() {
local vhost=$1
if [ -z $vhost ]; then
echo "Please enter a vhost name. Use the form 'nx disable [vhost]'."
return 1
fi
local link="/etc/nginx/sites-enabled/$vhost.conf"
if [ -e $link ]; then
sudo rm $link
echo "Disabled vhost $vhost"
fi
}
# Edit a vhost configuration file
edit() {
local vhost=$1
if [ -z $vhost ]; then
echo "Please enter a vhost name. Use the form 'nx edit [vhost]'."
return 1
fi
sudo nano "/etc/nginx/sites-enabled/$vhost.conf"
test
}
# Delete disable a vhost and delete its configuration
delete() {
local vhost=$1
local conf="/etc/nginx/sites-available/$vhost.conf"
local symlink="/etc/nginx/sites-enabled/$vhost.conf"
if [ -z $vhost ]; then
echo "Please enter a vhost name. Use the form 'nx delete [vhost]'"
return 1
fi
if ! [ -e $conf ]; then
echo "$conf doesn't exist."
return 1
fi
sudo rm -i $conf # Confirm if the user wants to delete the file.
if ! [ -e $conf ]; then
sudo rm -f $symlink
echo "$vhost disabled and deleted."
reload
fi
}
# Show all enabled and available vhosts
list() {
if [ ! -z $1 ] && [[ $1 == "--all" ]]; then
ls /etc/nginx/sites-available/
else
ls /etc/nginx/sites-enabled/
fi
}
# NginX Service Functions
# Reload NginX silently
reload() {
if sudo nginx -s reload ; then
echo "NginX configuration reloaded."
fi
}
# Test the configuration
test() {
sudo nginx -t
}
# Print the readme file
help() {
local cd=$(dirname $0)
cat "$cd/readme-nx"
return 0
}
if [ $# -ge 3 ] ; then
help
return 1
fi
case $1 in
a | add)
add $2
;;
d | delete)
delete $2
;;
disable)
disable $2
;;
e | edit)
edit $2
;;
enable)
enable $2
;;
l | list)
list $2
;;
r | reload)
reload
;;
t | test)
test
;;
-h | h | help | *)
help
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment