WEB 服务器之 Nginx服务脚本安装

/

Nginx 介绍:

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。因些许多企业都在使用。

目的:

1、一键安装
2、修改源码改变nginx的名称和版本【安全】

脚本内容:

  1. #!/bin/bash
  2. #
  3. #********************************************************************
  4. #Author: wwtou
  5. #Date: 2025-04-23
  6. #FileName: nginx_install.sh
  7. #Description: The test script for OS Version greater than 8.0
  8. #Copyright (C): 2020 All rights reserved
  9. #********************************************************************
  10. NGINX_VER=1.26.3
  11. CUSTOM_VER=66.88.0
  12. SERVER_NAME=Websrv
  13. NGINX_FILE=nginx-${NGINX_VER}.tar.gz
  14. NGINX_URL=http://nginx.org/download/
  15. HOST_IP=$(hostname -I | awk '{print $1}')
  16. SRC_DIR=/tools
  17. NGINX_INSTALL_DIR=/apps/nginx
  18. NGINX_TMP_DIR=/var/tmp/nginx
  19. CONFILE=/etc/nginx/nginx.conf
  20. NGINX_COMMAND_FILE=/etc/profile.d/nginx.sh
  21. USER=nginx
  22. GROUP=nginx
  23. CPUS=$(lscpu |awk '/^CPU\(s\)/{print $2}')
  24. . /etc/os-release
  25. color () {
  26. RES_COL=60
  27. MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  28. SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  29. SETCOLOR_FAILURE="echo -en \\033[1;31m"
  30. SETCOLOR_WARNING="echo -en \\033[1;33m"
  31. SETCOLOR_NORMAL="echo -en \E[0m"
  32. echo -n "$1" && $MOVE_TO_COL
  33. echo -n "["
  34. if [ $2 = "success" -o $2 = "0" ] ;then
  35. ${SETCOLOR_SUCCESS}
  36. echo -n $" OK "
  37. elif [ $2 = "failure" -o $2 = "1" ] ;then
  38. ${SETCOLOR_FAILURE}
  39. echo -n $"FAILED"
  40. else
  41. ${SETCOLOR_WARNING}
  42. echo -n $"WARNING"
  43. fi
  44. ${SETCOLOR_NORMAL}
  45. echo -n "]"
  46. echo
  47. }
  48. check () {
  49. [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
  50. [ ! -d ${NGINX_TMP_DIR} ] && mkdir -p ${NGINX_TMP_DIR}/{client,proxy,fcgi,uwsgi,scgi}
  51. selinux_code=$(getenforce)
  52. if [ ${selinux_code} = 'Disabled' ]; then
  53. color 'Selinux 已经关闭' 0
  54. else
  55. setenforce 0
  56. sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
  57. color 'Selinux 已经临时关闭,重启后彻底生效' 0
  58. fi
  59. if firewall-cmd --state &>/dev/null; then
  60. systemctl disable --now firewalld.service &>/dev/null
  61. #firewall-cmd --add-service=http --permanent
  62. #firewall-cmd --add-service=https --permanent
  63. #firewall-cmd --add-port=8012/tcp --permanent
  64. #firewall-cmd --add-port=8013/tcp --permanent
  65. #firewall-cmd --reload
  66. color '已关闭防火墙' 0
  67. else
  68. color '防火墙已关闭' 0
  69. fi
  70. color "安装工具包" 0
  71. if egrep -i 'centos|rocky|anolis|rhel' /etc/os-release &>/dev/null; then
  72. dnf -y install tar bash-completion vim-enhanced wget
  73. else
  74. apt update
  75. apt -y install tar bash-completion vim-enhanced wget
  76. fi
  77. [ ! -d ${SRC_DIR} ] && mkdir ${SRC_DIR}
  78. cd ${SRC_DIR}
  79. if [ -e ${SRC_DIR}/${NGINX_FILE} ];then
  80. color "相关文件已准备好" 0
  81. else
  82. color '开始下载 nginx 源码包' 0
  83. wget ${NGINX_URL}${NGINX_FILE}
  84. [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}文件失败" 1; exit; }
  85. fi
  86. }
  87. install () {
  88. color "开始安装 nginx" 0
  89. if id $USER &> /dev/null;then
  90. color "$USER 用户已存在" 1
  91. else
  92. groupadd $GROUP
  93. useradd -r -g $GROUP -s /sbin/nologin $USER
  94. color "创建 $USER 用户" 0
  95. fi
  96. color "开始安装 nginx 依赖包" 0
  97. if egrep -i 'centos|rocky|anolis|rhel' /etc/os-release &>/dev/null; then
  98. dnf -y install gcc make openssl-devel pcre-devel zlib-devel perl-ExtUtils-Embed
  99. else
  100. apt update
  101. apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
  102. fi
  103. cd $SRC_DIR
  104. tar xf ${NGINX_FILE}
  105. NGINX_DIR=`echo ${NGINX_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p'`
  106. cd ${NGINX_DIR}
  107. #
  108. sed -i "s|${NGINX_VER}|${CUSTOM_VER}|" src/core/nginx.h
  109. sed -i "s|nginx/|${SERVER_NAME}/|" src/core/nginx.h
  110. sed -i "s| nginx| ${SERVER_NAME}|" src/http/ngx_http_header_filter_module.c
  111. [ $? -eq 0 ] && color "nginx 修改源码版本成功" 0 || { color "nginx 修改源码版本失败,退出!" 1 ; exit; }
  112. ./configure \
  113. --prefix=${NGINX_INSTALL_DIR} \
  114. --sbin-path=${NGINX_INSTALL_DIR}/sbin/nginx \
  115. --conf-path=$CONFILE \
  116. --pid-path=/var/run/nginx.pid \
  117. --lock-path=/var/lock/nginx.lock \
  118. --user=$USER \
  119. --group=$GROUP \
  120. --with-pcre \
  121. --with-stream \
  122. --with-http_ssl_module \
  123. --with-http_flv_module \
  124. --with-http_stub_status_module \
  125. --with-http_gzip_static_module \
  126. --with-http_realip_module \
  127. --with-http_v2_module \
  128. --http-client-body-temp-path=/var/tmp/nginx/client \
  129. --http-proxy-temp-path=/var/tmp/nginx/proxy \
  130. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
  131. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  132. --http-scgi-temp-path=/var/tmp/nginx/scgi \
  133. --without-mail_pop3_module \
  134. --without-mail_imap_module \
  135. --without-mail_smtp_module
  136. make -j $CPUS && make install
  137. [ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; }
  138. chown -R $USER:$GROUP ${NGINX_INSTALL_DIR}
  139. echo "export PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > ${NGINX_COMMAND_FILE}
  140. cat > /etc/security/limits.d/nginx.conf <<EOF
  141. * soft nofile 65535
  142. * hard nofile 65535
  143. EOF
  144. cat > /usr/lib/systemd/system/nginx.service <<EOF
  145. [Unit]
  146. Description=The nginx HTTP and reverse proxy server
  147. After=network.target remote-fs.target nss-lookup.target
  148. [Service]
  149. Type=forking
  150. ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx -c $CONFILE
  151. ExecReload=${NGINX_INSTALL_DIR}/sbin/nginx -s reload
  152. ExecStop=${NGINX_INSTALL_DIR}/sbin/nginx -s stop
  153. KillSignal=SIGQUIT
  154. TimeoutStopSec=5
  155. KillMode=process
  156. #PrivateTmp=true
  157. LimitNOFILE=100000
  158. [Install]
  159. WantedBy=multi-user.target
  160. EOF
  161. systemctl daemon-reload
  162. systemctl enable --now nginx.service &> /dev/null
  163. systemctl is-active nginx &> /dev/null || { color "nginx 启动失败,退出!" 1 ; exit; }
  164. color "nginx 安装完成" 0
  165. echo
  166. echo "请先运行: source ${NGINX_COMMAND_FILE}"
  167. echo "访问链接: http://${HOST_IP}"
  168. echo
  169. }
  170. check
  171. install

运行脚本:

  1. # sh nginx_install.sh

通过执行脚本实现一键安装 ~~

转载请注明作者和出处,并添加本页链接。
原文链接: //www.wwtou.com/pawx8DY.html