案例需求

通过shell脚本安装nginx

应用场景

web服务器业务初始化

案例思路

1、软件获得
2、源码安装

案例代码

  1. #!/bin/bash
  2. #Description: nginx install script from zutuanxue(http://www.zutuanxue.com)
  3. #Release: 1.0
  4. #Auther: www.zutuanxue.com
  5. #Email:
  6. #OS: Centos 8.X
  7. cat <<EOF
  8. #Description: nginx install script from zutuanxue(http://www.zutuanxue.com)
  9. #Release: 1.0
  10. #Auther: www.zutuanxue.com
  11. #Email:
  12. #OS: Centos 8.X
  13. EOF
  14. #nginx源码包下载路径
  15. nginx_pkg_url=http://nginx.org/download/nginx-1.17.8.tar.gz
  16. #nginx安装路径,安装路径为$nginx_install_doc/nginx
  17. nginx_install_doc=/usr/local
  18. #nginx服务管理用户
  19. nginx_manage_user=www
  20. #统计本机CPU核数
  21. cpu_count=`grep -c "flags" /proc/cpuinfo`
  22. check () {
  23. #安装nginx需要管理员权限
  24. [ $UID -ne 0 ] && echo "need to be root so that" && exit 1
  25. #安装前的依赖包解决
  26. #wget 命令
  27. #gcc 编译命令
  28. #pcre-devel URL重写功能
  29. #zlib-devel 压缩支持
  30. #make 编译命令
  31. if ! (yum -y install wget gcc pcre-devel zlib-devel make &>/dev/null);then
  32. echo "yum install soft package fail"
  33. exit 1
  34. fi
  35. if ! (egrep "^www" /etc/passwd &>/dev/null);then
  36. useradd -s /sbin/nologin -r -M www
  37. fi
  38. }
  39. nginx_install () {
  40. #1、下载软件包
  41. #if wget $nginx_pkg_url &>/dev/null;then
  42. #2、解压软件包
  43. echo $nginx_pkg_url|awk -F "/" '{print $5}'|xargs tar xf
  44. nginx_source_doc=`echo $nginx_pkg_url|awk -F "/" '{print $5}'|cut -d "." -f 1-3`
  45. #3、进入软件包
  46. if [ ! -d $nginx_source_doc ];then
  47. echo "unzip `echo $nginx_pkg_url|awk -F "/" '{print $5}'` fail"
  48. exit 1
  49. fi
  50. cd $nginx_source_doc
  51. #4、configure nginx
  52. ./configure --prefix=$nginx_install_doc/nginx --user=$nginx_manage_user --group=$nginx_manage_user 1>/dev/null
  53. [ $? -ne 0 ]&&echo "nginx configure fail"&&exit 1
  54. #5、make nginx
  55. make -j $cpu_count 1>/dev/null
  56. [ $? -ne 0 ]&&echo "nginx make fail"&&exit 1
  57. #6、install nginx
  58. make install 1>/dev/null
  59. [ $? -ne 0 ]&&echo "nginx install fail"&&exit 1||echo "`clear`nginx install success..."
  60. #7、delete nginx soft package
  61. cd ..
  62. rm -rf ${nginx_source_doc}*
  63. #else
  64. # echo "$nginx_pkg_url download fail"
  65. # exit 1
  66. #fi
  67. }
  68. #####callable function
  69. check;nginx_install