安装nignx脚本
#!/bin/bash
#
#******************************************************************************
#Author: dange
#QQ: XXXXXXXXX
#Date: 2025-04-25
#FileName: install_nginx.sh
#Description: Ubuntu 22.04/24.04 & Rocky 8/9 & openEuler24系列
#Copyright (C): 2025 All rights reserved
#******************************************************************************
SRC_DIR=/data/server
NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.26.3
TAR=.tar.gz
NGINX_INSTALL_DIR=/data/server/nginx
CPUS=$(nproc)
# 颜色脚本,通用
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
# 查看属于Rocky、Ubuntu、openEuler系列
os_type () {
awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}
# Linux版本信息
os_version () {
awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}
check () {
# 检查文件夹是否存在
[ -d ${SRC_DIR} ] || { mkdir /data/codes /data/server /data/softs -p; }
# 检查是否存在nginx执行文件
[ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
cd ${SRC_DIR}
if [ -e ${NGINX_FILE}${TAR} ];then
color "相关文件已准备好" 0
else
color '开始下载 nginx 源码包' 0
wget ${NGINX_URL}${NGINX_FILE}${TAR} > /dev/null 2>&1
[ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; }
fi
}
install () {
color "开始安装 nginx" 0
# 创建用户
if id nginx &> /dev/null;then
color "nginx 用户已存在" 1
else
useradd -s /sbin/nologin -r nginx
color "创建 nginx 用户" 0
fi
# 临时关闭setenforce,防止启动不起来nginx,报错
if [[ $(os_type) == "CentOS" || $(os_type) == "Rocky" || $(os_type) == "openEuler" ]] ;then
if [[ $(getenforce) == "Enforcing" ]];then
setenforce 0
color "SELinux 已临时关闭" 0
fi
fi
# 安装需要依赖的软件包
color "开始安装 nginx 依赖包" 0
if [[ $(os_type) == "Rocky" || $(os_type) == "openEuler" ]] ;then
yum -y -q install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel > /dev/null 2>&1
yum -y -q install libxml2 libxml2-devel libxslt libxslt-devel php-gd gd-devel > /dev/null 2>&1
elif [ $(os_type) == "Ubuntu" ] ;then
apt update > /dev/null 2>&1
apt -y install build-essential gcc g++ libc6 libc6-dev libpcre3 libpcre3-dev libssl-dev libsystemd-dev zlib1g-dev > /dev/null 2>&1
apt -y install libxml2 libxml2-dev libxslt1-dev php-gd libgd-dev geoip-database libgeoip-dev > /dev/null 2>&1
else
apt update > /dev/null 2>&1
apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev > /dev/null 2>&1
fi
color "开始安装 nginx 配置" 0
cd $SRC_DIR
# 解压缩
tar xf ${NGINX_FILE}${TAR}
cd ${NGINX_FILE}
./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module >/dev/null 2>&1
# 多线程编译
color "开始安装 nginx 多线程编译" 0
make -j $CPUS > /dev/null 2>&1 && make install > /dev/null 2>&1
[ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; }
# 定制环境变量
echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
# 修改目录属主属组并查看
chown -R nginx:nginx /data/server/nginx/
# 创建软链接
ln -sv /data/server/nginx/sbin/nginx /usr/sbin/nginx >/dev/null 2>&1
# 生成pid
mkdir -p ${NGINX_INSTALL_DIR}/run
# touch ${NGINX_INSTALL_DIR}/run/nginx.pid
# 修改配置文件设置pid路径
sed -i.bak '/pid/a pid /data/server/nginx/run/nginx.pid;' /data/server/nginx/conf/nginx.conf
# 创建日志文件夹
# mkdir ${NGINX_INSTALL_DIR}/logs/
color "定制nginx服务管理" 0
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/run/nginx.pid
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx -c ${NGINX_INSTALL_DIR}/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
systemctl is-active nginx &> /dev/null || { color "nginx 启动失败,退出!" 1 ; exit; }
color "nginx 安装完成" 0
}
check
install
endl
评论