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-18
  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名称和版本
  11. # vim src/core/nginx.h
  12. # #define NGINX_VERSION "1.66.88"
  13. # #define NGINX_VER "websrv" NGINX_VERSION
  14. #
  15. # vim src/http/ngx_http_header_filter_module.c
  16. # static char ngx_http_server_string[] = "Server: websrv" CRLF
  17. #**********************************************************************
  18. NGINX_VER=1.26.3
  19. CUSTOM_VER=66.88.0
  20. SERVER_NAME=Websrv
  21. NGINX_FILE=nginx-${NGINX_VER}.tar.gz
  22. NGINX_URL=http://nginx.org/download/
  23. HOST_IP=$(hostname -I | awk '{print $1}')
  24. SRC_DIR=/tools
  25. NGINX_INSTALL_DIR=/apps/nginx
  26. NGINX_TMP_DIR=/var/tmp/nginx
  27. CONFILE=/etc/nginx/nginx.conf
  28. NGINX_COMMAND_FILE=/etc/profile.d/nginx.sh
  29. USER=nginx
  30. GROUP=nginx
  31. CPUS=$(lscpu |awk '/^CPU\(s\)/{print $2}')
  32. . /etc/os-release
  33. color() {
  34. local text="$1"
  35. local status="$2"
  36. local RES_COL=60
  37. local GREEN="\033[1;32m"
  38. local RED="\033[1;31m"
  39. local YELLOW="\033[1;33m"
  40. local NORMAL="\033[0m"
  41. # 左侧文本 + 对齐
  42. printf "%-${RES_COL}s" "$text"
  43. # 移动光标到第 RES_COL 列
  44. echo -en "\033[${RES_COL}G"
  45. echo -n "["
  46. case "$status" in
  47. success|0)
  48. echo -en "${GREEN} OK ${NORMAL}"
  49. ;;
  50. failure|1)
  51. echo -en "${RED}FAILED${NORMAL}"
  52. ;;
  53. *)
  54. echo -en "${YELLOW}WARNING${NORMAL}"
  55. ;;
  56. esac
  57. echo "]"
  58. }
  59. check () {
  60. [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
  61. [ ! -d ${NGINX_TMP_DIR} ] && mkdir -p ${NGINX_TMP_DIR}/{client,proxy,fcgi,uwsgi,scgi}
  62. color "检查操作系统和系统版本" 0
  63. # Detect OS
  64. # $os_version variables aren't always in use, but are kept here for convenience
  65. if grep -qs "ubuntu" /etc/os-release; then
  66. os="ubuntu"
  67. os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
  68. elif [[ -e /etc/debian_version ]]; then
  69. os="debian"
  70. os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
  71. elif egrep -i "openeuler|rocky|almalinux|Anolis|centos|kylin" /etc/os-release &> /dev/null; then
  72. os="centos"
  73. os_version=$(grep "VERSION_ID" /etc/os-release | awk -F'"' '{print $2}' | cut -d'.' -f1)
  74. elif [[ -e /etc/fedora-release ]]; then
  75. os="fedora"
  76. os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
  77. else
  78. color "This installer seems to be running on an unsupported distribution.Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora." 1
  79. exit
  80. fi
  81. if [[ "$os" == "ubuntu" && "$os_version" -lt 2204 ]]; then
  82. color "Ubuntu 22.04 or higher is required to use this installer.This version of Ubuntu is too old and unsupported." 1
  83. exit
  84. fi
  85. if [[ "$os" == "debian" ]]; then
  86. if grep -q '/sid' /etc/debian_version; then
  87. color "Debian Testing and Debian Unstable are unsupported by this installer." 1
  88. exit
  89. fi
  90. if [[ "$os_version" -lt 11 ]]; then
  91. color "Debian 11 or higher is required to use this installer.This version of Debian is too old and unsupported." 1
  92. exit
  93. fi
  94. fi
  95. if [[ "$os" == "centos" && "$os_version" -lt 9 ]]; then
  96. os_name=$(grep "^NAME" /etc/os-release | awk -F'"' '{print $2}')
  97. echo "$os_name 9 or higher is required to use this installer.This version of $os_name is too old and unsupported." 0
  98. exit
  99. fi
  100. color "安装工具包,检查防火墙" 0
  101. if ! hash ufw 2>/dev/null && ! hash firewall-cmd 2>/dev/null && ! hash iptables 2>/dev/null; then
  102. if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
  103. apt -y update && apt install -y ufw
  104. [ $? -ne 0 ] && { color "Install UFW Failed" 1 ; exit; }
  105. ufw disable
  106. color 'ufw is closed' 0
  107. else
  108. dnf install -y firewalld
  109. [ $? -ne 0 ] && { color "Install Firewalld Failed" 1; exit; }
  110. setenforce 0
  111. sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
  112. color 'Selinux has been disabled, and Permanent After Restarting' 0
  113. systemctl disable --now firewalld.service &>/dev/null
  114. color 'firewalld is complete closed' 0
  115. fi
  116. fi
  117. if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
  118. apt -y update && apt install -y tar wget vim bash-completion gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib-dev
  119. [ $? -ne 0 ] && { color "Install Tools Failed" 1 ; exit; }
  120. else
  121. dnf install -y wget tar vim-enhanced bash-completion gcc make openssl-devel pcre-devel zlib-devel perl-ExtUtils-Embed
  122. [ $? -ne 0 ] && { color "Install Tools Failed" 1; exit; }
  123. fi
  124. [ ! -d ${SRC_DIR} ] && mkdir ${SRC_DIR}
  125. cd ${SRC_DIR}
  126. if [ -e ${SRC_DIR}/${NGINX_FILE} ];then
  127. color "相关文件已准备好" 0
  128. else
  129. color '开始下载 nginx 源码包' 0
  130. wget ${NGINX_URL}${NGINX_FILE}
  131. [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}文件失败" 1; exit; }
  132. fi
  133. }
  134. install () {
  135. color "开始安装 nginx" 0
  136. if id $USER &>/dev/null; then
  137. color "$USER 用户已存在" 1
  138. else
  139. groupadd $GROUP
  140. useradd -r -g $GROUP -s /sbin/nologin $USER
  141. color "创建 $USER 用户" 0
  142. fi
  143. color "开始安装 nginx 依赖包" 0
  144. cd $SRC_DIR
  145. tar xf ${NGINX_FILE}
  146. NGINX_DIR=`echo ${NGINX_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p'`
  147. cd ${NGINX_DIR}
  148. sed -i "s|${NGINX_VER}|${CUSTOM_VER}|" src/core/nginx.h
  149. sed -i "s|nginx/|${SERVER_NAME}/|" src/core/nginx.h
  150. sed -i "s| nginx| ${SERVER_NAME}|" src/http/ngx_http_header_filter_module.c
  151. [ $? -eq 0 ] && color "nginx 修改源码版本成功" 0 || { color "nginx 修改源码版本失败,退出!" 1 ; exit; }
  152. ./configure \
  153. --prefix=${NGINX_INSTALL_DIR} \
  154. --sbin-path=${NGINX_INSTALL_DIR}/sbin/nginx \
  155. --conf-path=$CONFILE \
  156. --pid-path=/var/run/nginx.pid \
  157. --lock-path=/var/lock/nginx.lock \
  158. --user=$USER \
  159. --group=$GROUP \
  160. --with-pcre \
  161. --with-stream \
  162. --with-http_ssl_module \
  163. --with-http_flv_module \
  164. --with-http_stub_status_module \
  165. --with-http_gzip_static_module \
  166. --with-http_realip_module \
  167. --with-http_v2_module \
  168. --with-http_dav_module \
  169. --http-client-body-temp-path=/var/tmp/nginx/client \
  170. --http-proxy-temp-path=/var/tmp/nginx/proxy \
  171. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
  172. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  173. --http-scgi-temp-path=/var/tmp/nginx/scgi \
  174. --without-mail_pop3_module \
  175. --without-mail_imap_module \
  176. --without-mail_smtp_module
  177. # --with-openssl=/root/openssl-1.1.1w
  178. make -j $CPUS && make install
  179. [ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; }
  180. chown -R $USER:$GROUP ${NGINX_INSTALL_DIR}
  181. echo "export PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > ${NGINX_COMMAND_FILE}
  182. cat > /etc/security/limits.d/nginx.conf <<EOF
  183. * soft nofile 65535
  184. * hard nofile 65535
  185. EOF
  186. cat > /usr/lib/systemd/system/nginx.service <<EOF
  187. [Unit]
  188. Description=The nginx HTTP and reverse proxy server
  189. After=network.target remote-fs.target nss-lookup.target
  190. [Service]
  191. Type=forking
  192. ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx -c $CONFILE
  193. ExecReload=${NGINX_INSTALL_DIR}/sbin/nginx -s reload
  194. ExecStop=${NGINX_INSTALL_DIR}/sbin/nginx -s stop
  195. KillSignal=SIGQUIT
  196. TimeoutStopSec=5
  197. KillMode=process
  198. #PrivateTmp=true
  199. LimitNOFILE=100000
  200. [Install]
  201. WantedBy=multi-user.target
  202. EOF
  203. systemctl daemon-reload
  204. systemctl enable --now nginx.service &> /dev/null
  205. systemctl is-active nginx &> /dev/null || { color "nginx 启动失败,退出!" 1 ; exit; }
  206. color "nginx 安装完成" 0
  207. echo
  208. echo "请先运行: source ${NGINX_COMMAND_FILE}"
  209. echo "访问链接: http://${HOST_IP}"
  210. echo
  211. }
  212. check
  213. install

运行脚本:

  1. # bash nginx_install.sh

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

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