Nginx site loading functions

|
While digging through some old files, I found these two functions I had written during my fiddlings with the Nginx web server.  At the time at least, there weren't Nginx equivalents of the a2ensite and a2dissite commands used to enable and disable sites in Apache. 

Since I can't remember if I posted them already (and I'm a bit to busy to check right now) I thought I'd post them again just to be sure.  Usage of these functions blah blah I take no responsibility blah at your own risk blah blah created in a factory that processes tree nuts.

# Enables a site used by nginx
function nginable
(
  if [ $# -lt 4 ]; then
    echo "Usage: nginable -s|--source source_config_file -n|--name site_name [-r|--restart]"
    echo "-s|--source The config file defining the site"
    echo "-n|--name The name to appear in the sites-enabled directory"
    echo "-r|--restart Include to restart nginx after the site has been enabled"
   return 0
  fi

  if [ $1 = "--help" ]; then
    echo "Enables a site used by nginx"  
    echo 'Usage: nginable -s|--source source_config_file -n|--name site_name [-r|--restart]'
    echo 'Ex: nginable --source ./site_config --name nginx_tutorial --restart'
    echo "  If no pathing information is given, the config file"
    echo "  is assumed to exist in /etc/nginx/sites-available."
    return 0
  fi

  args=`getopt :s:n:r $*`

  for i
    do
      case "$i" in
        -s|--source) shift;SOURCE=$1;shift;;
        -n|--name) shift;NAME=$1;shift;;
        -r|--restart) RESTART=1;;
      esac
    done

  SITESENABLED=/etc/nginx/sites-enabled
  SITESAVAIL=/etc/nginx/sites-available

  # See if nginx is running
  PROC=`pgrep -c nginx`
 
  if [ $PROC -gt 1 ]; then
    NGINXON=1
  else
    echo 'Nginx does not appear to be running...'
  fi
 
  # Check for target directory
  if [ ! -d $SITESENABLED ]; then
    echo "[ERROR] Can't find sites-enabled directory"
    return 1
  fi

  # Check for config file
  if [ -f $SITESAVAIL/$SOURCE ]; then
    SOURCE=$SITESAVAIL/$SOURCE
    echo "Site detected - $SOURCE"
  elif [ ! -f $SOURCE ]; then
    echo "[ERROR] Can't find config file $SOURCE"
    return 2
  fi

  # See if a site by that name already exists
  if [ -f $SITESENABLED/$NAME ]; then
    echo "A site called $NAME already exists in $SITESENABLED"
    return 3
  fi

  # Enable site
  ln --target-directory=$SITESENABLED --symbolic $SOURCE

  # See if restart was called for and check syntax
  if [ $RESTART ]; then
    if [ $NGINXON ]; then
      /etc/init.d/nginx stop
    fi

    /etc/nginx -t
    
    if [ ! $? -eq 0 ]; then
        echo "[ERROR] Errors found in config files"
        echo "        Disabling new site..."
        rm $SITESENABLED/$NAME

        if [ ${NGINXON} ]; then
          /etc/init.d/nginx start
          echo "Restarting nginx..."
        fi

        return 4
    fi
    
    if [ ${NGINXON} ]; then
      /etc/init.d/nginx start
      return 0
    fi

    echo "Site $NAME enabled"
    return 0
  fi
)

# Disables a site used by nginx
function nginoff
(
  if [ $1 = '--help' || $1 = '-h' || $# -lt 2 ]; then
    echo "Disables a site used by nginx"
    echo "Usage: nginoff -n|--name site_name [-r|--restart]"
    echo "Ex: nginoff --name nginx_tutorial --restart"
    return 0
  fi
 
  SITESENABLED=/etc/nginx/sites-enabled

  # See if nginx is running
  PROC=`pgrep -c nginx`
 
  if [ $PROC -gt 1 ]; then
    NGINXON=1
  else
    echo 'Nginx does not appear to be running...'
  fi

  args=`getopt :n:r $*`

  for i
    do
      case "$i" in
        -n|--name)shift;NAME=$1;shift;;
        -r|--restart)shift;RESTART=1;;
      esac
    done

  if [ ${NGINXON} && ! ${RESTART} ]; then
    echo "[WARNING] If nginx is not restarted, site errors may occur"
  fi
 
  if [ ! -f $SITESENABLED/$NAME ]; then
    echo "[ERROR] Can't find an enabled site called $NAME"
    return 1
  fi

  rm $SITESENABLED/$NAME

  echo "Site $NAME disabled"

  if [ ${RESTART} ]; then
    if [ ${NGINXON} ]; then
      /etc/init.d/nginx stop
      /etc/init.d/nginx start
    fi
  fi

  return 0
)

About this Entry

This page contains a single entry by Philip Ratzsch published on April 6, 2009 10:14 AM.

A break from the norm was the previous entry in this blog.

Cisco Alert is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.