skip to Main Content

I want to automate the installation of nginx web service in such a way that if nginx is already present it will get installed at other location ex: /usr/local/nginx.

#!/bin/bash
echo "Beginning installation of nginx web server"
if ! which nginx > /dev/null 2>&1; then
    echo "Nginx is not installed"
else
    echo "Installing nginx"
    yum install nginx -y
    
    #check if nginx services are up
    service nginx start
fi

I am new to shell script and tried above approach, however I am unable to execute command in else block.
Help will be really appreciated.

2

Answers


  1. I think i understand what you wanted to do but logic in you code is not the best, so here is snip from one of my first bash scripts where i did something similar:

      read INP
      if [ $INP -eq 1 ]
      then
        if command -v nginx
        then
          systemctl status nginx
          back
        else
          error
          read INP
          if [ $INP == y ]
          then
            sudo apt install nginx
          else
            back
          fi
        fi
      fi
    

    Btw this is for Debian based distros, so you can change apt for yum, pacman, etc. package manager. And just to be clear, back and error are functions i created.

    Login or Signup to reply.
  2. Check this out

    #!/usr/bin/env bash
    # Install nginx on your web-01 server
    # Nginx should be listening on port 80
    # When querying Nginx at its root / with a GET request
    # (requesting a page) using curl, it must return a page
    # that contains the string Hello World!
    
    sudo apt-get -y update
    sudo apt-get -y install nginx
    echo 'Hello World' | sudo tee /var/www/html/index.html
    sudo service nginx start
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search