RAAF Vampire trainer being maintained
Semi-Auto Nginx Install
Tuesday, 02 February 2016 10:42 Hrs
❝ Emulate apt-get: create your own Super Cow Powers.

I'm not lazy. I can type. I just have better things to do with my time than type in trivial commands at the keyboard. That's where automation comes in. Well, not quite full automation. I'll settle for semi—automation.

I can go a number of ways here. There are many languages to choose that abstract the work suitable for multi-platforms, I can use existing open-source tools or write up the command line script I type in and use a operating / shell specific (a.k.a) throw away script. This is the path I choose.

Why semi-automate, why not do the lot? Well the extra effort to write the specific details to the nginx configuration file is not worth the pain of extra code.

What do we need to automate?

Bash away

My trusty toolset today is making use of Bash shell scripting. [0] The task, setup of a nginx server. Here's what I want to do:

  • install nginx using apt-get

  • start the server

  • display the hostname

  • stop the server

  • IF first time install then

    • write out filepath of file to edit

    • write out nginx config file details:

      • http->server

      • http->server->servername

      • http->server->location->try_files

      • http->server->location->root

      • http->server->index files

      • http->server->location path for images

    • make directory given path and name

    • change ownership of directory to owner:group

  • start the server

  • reload the server

  • display server status

Once we have the requirements, turn them into a usable script. The way to think of this is, "automate the repeatable commands" and supply any of the changing arguments as variables. This statement is obvious, though how many times have you repeated simple tasks like this? Once, twice... ten?

Sample code

The code below is basic. In fact its what I like to call, "toy code". The simple point I am trying to illustrate? A tiny bit of extra effort means you can do these routine tasks quicker and more accurately the next time.

It also saves on typing.

#====== start ======

   # name: si_nginx.sh
   # desc: semi installation of nginx with 
   #       minor editing if required


# ------ setup, install ------

   echo "start install"
   apt-get install nginx
   echo "server at http://",`hostname -I`
   echo "stop server"
   /etc/init.d/nginx stop 

#------- config ------

   if [ $IS_FIRST_TIME = 0 ]; then
      echo "edit time"
      echo "$ vim $DEFAULT_CONFIG_PATH"
      echo "====== multiple lines of config file here ====="
      echo "make directory $WWW"
      mkdir $WWW
      echo "set ownership of $WWW to $OWNER:$GROUP"
      chown $OWNER:$GROUP $WWW
      echo ">>>>>> warning: NOT FIRST TIME SETUP? done this before? >>>>>>"

#------ status ------

   echo "start, reload server"
   echo "status report"
   systemctl status nginx.service
   echo "ready at http://" `hostname -I`

#===== end ======

You may notice the script runs without stopping, the first time through. A stop could be inserted. Running the script multiple times I didn't see it worth the extra effort. Also notice there is no mention of sudo. I run the sudo command on the actual script.


[0] Bash Intro HOWTO #11

[Last accessed: Tuesday 2nd February, 2016]


bio Another Scrappy Startup ☮ ♥ ♬ ⌨

contact Peter Renshaw