简体   繁体   English

使用shell脚本安装nginx web服务器

[英]Install nginx web server using shell script

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.我想自动安装 nginx web 服务,如果 nginx 已经存在,它将安装在其他位置,例如:/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.我是 shell 脚本的新手并尝试了上述方法,但是我无法在 else 块中执行命令。 Help will be really appreciated.帮助将不胜感激。

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:我想我明白你想做什么,但你代码中的逻辑不是最好的,所以这里是我的第一个 bash 脚本中的一个片段,我做了类似的事情:

  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.顺便说一句,这是基于 Debian 的发行版,因此您可以将 apt 更改为 yum、pacman 等 package 管理器。 And just to be clear, back and error are functions i created.需要说明的是,back 和 error 是我创建的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM