一、脚本概览¶
1.1 目录中的脚本分类¶
rocky_nginx_binary_offline_install.sh:Rocky 系列离线二进制安装。rocky_nginx_binary_online_install.sh:Rocky 系列在线二进制安装。rocky_nginx_source_offline_install.sh:Rocky 系列离线源码编译安装。rocky_nginx_source_online_install.sh:Rocky 系列在线源码编译安装。ubuntu_nginx_binary_offline_install.sh:Ubuntu 离线二进制安装。ubuntu_nginx_binary_online_install.sh:Ubuntu 在线二进制安装。ubuntu_nginx_source_offline_install.sh:Ubuntu 离线源码编译安装。ubuntu_nginx_source_online_install.sh:Ubuntu 在线源码编译安装。
1.2 适用场景拆分¶
- 需要快速上线,优先使用二进制安装脚本。
- 需要自定义编译参数,优先使用源码安装脚本。
- 生产环境无法联网,优先使用离线脚本。
- 同时维护 Rocky 与 Ubuntu 环境时,可以把同一套流程按发行版拆成两篇独立文章继续扩写。
二、公共安装流程¶
2.1 二进制安装脚本的典型步骤¶
NGINX_VERSION="1.20.1"
SOFTS_DIR="/opt"
NGINX_USER="nginx"
cd "$SOFTS_DIR"
tar -zxvf nginx-${NGINX_VERSION}-deps-<os><os_version>.tar.gz
yum localinstall *.rpm -y
yum localinstall nginx-${NGINX_VERSION}-*.rpm -y
useradd -r -s /usr/sbin/nologin "$NGINX_USER"
systemctl disable --now firewalld.service
setenforce 0
systemctl enable nginx --now
2.2 源码安装脚本的典型步骤¶
INSTALL_DIR="/data/server/nginx"
NGINX_USER="nginx"
./configure --prefix="$INSTALL_DIR" \
--user="$NGINX_USER" \
--group="$NGINX_USER" \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module
make
make install
三、整理后的阅读顺序¶
3.1 Rocky 系列可以单独成文¶
- 二进制在线安装:适合基础环境统一、仓库可用的场景。
- 二进制离线安装:适合内网、等保或封网环境。
- 源码在线安装:适合实验环境和需要快速验证模块参数的场景。
- 源码离线安装:适合既不能联网、又需要自定义安装目录和模块的环境。
3.2 Ubuntu 系列可以平行复用 Rocky 结构¶
Ubuntu 系列脚本与 Rocky 系列在总体步骤上基本一致,主要差异集中在包管理器、依赖包命名和服务文件所在路径,因此后续整理为博客时可以直接复用“环境准备、安装流程、服务启动、验证”的结构。
四、发布前建议¶
4.1 建议补齐的说明项¶
- 离线包命名规则建议单独写成一节,避免读者把依赖包和源码包混放后无法复现。
- 建议说明禁用防火墙和 SELinux 的前提,避免读者在生产环境直接照搬。
- 建议增加
nginx -V、systemctl status nginx、curl 127.0.0.1三个验收步骤。
五、完整脚本¶
以下为本文对应的完整脚本,便于直接复制复用。
5.1 rocky_nginx_binary_offline_install.sh¶
#!/bin/bash
##############################################################
# File Name:rocky_nginx_binary_offline_install.sh
# Version:V1.1
# Author:zq
# Organization:www.zhang-qing.com
# Desc:离线二进制安装Nginx并配置
##############################################################
##############################################################
# 执行脚本说明:
# 1. 请先将 Nginx 相关版本的依赖包和源码包提前放好在 /opt 目录下。
# 2. 依赖包和源码包的命名规则如下:
# - 依赖包:nginx-<version>-deps-<os>-<os_version>.tar.gz
# - 源码包:nginx-<version>.tar.gz
#
# 例如,对于 Nginx 1.20.1 版本,适用于 Rocky Linux 9.4 操作系统的依赖包应命名为:
# nginx-1.20.1-deps-rocky9.4.tar.gz
# nginx-1.20.1.tar.gz
#
# 完成这些步骤后,将依赖包拷贝到 /opt 目录下,供脚本后续安装使用。
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.20.1"
SOFTS_DIR="/opt"
NGINX_USER="nginx"
# 定义操作系统和版本号变量
OS="rocky"
OS_VERSION="9.4"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 离线安装依赖
print_line
print_step "离线安装依赖包..."
# 解压并安装 Nginx 依赖包
cd $SOFTS_DIR
tar -zxvf nginx-$NGINX_VERSION-deps-$OS$OS_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION-deps-$OS$OS_VERSION
# 使用 yum localinstall 安装 rpm 包
yum localinstall *.rpm -y &>/dev/null
print_step "依赖包离线安装完成."
# 安装 Nginx 二进制包
print_line
print_step "安装 Nginx 二进制包..."
tar -zxvf nginx-$NGINX_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION
yum localinstall nginx-$NGINX_VERSION-*.rpm -y &>/dev/null
print_step "Nginx 二进制包安装完成."
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
print_step "Nginx 用户创建完成."
# 禁用防火墙
print_line
print_step "禁用防火墙..."
systemctl disable --now firewalld.service &>/dev/null
print_step "防火墙已禁用."
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
print_step "SELinux 已禁用."
# 启动 Nginx
print_line
print_step "启动 Nginx..."
systemctl enable nginx --now &>/dev/null
print_step "Nginx 已启动并设置为开机自启."
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.2 rocky_nginx_binary_online_install.sh¶
#!/bin/bash
##############################################################
# File Name:rocky_nginx_binary_online_install.sh
# Version:V1.1
# Author:zq
# Organization:www.zhang-qing.com
# Desc:Rocky系列二进制安装Nginx并配置
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.23.0"
NGINX_USER="nginx"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 检查 Nginx 是否已经安装
if rpm -q nginx &>/dev/null; then
print_step "Nginx 已经安装,跳过安装步骤."
else
# 安装 Nginx 二进制包
print_line
print_step "安装 Nginx 二进制包..."
yum install -y nginx nginx-core &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 安装完成."
else
echo -e "${RED}[ERROR] Nginx 安装失败.${RESET}"
exit 1
fi
fi
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 用户创建完成."
else
print_step "Nginx 用户已存在."
fi
# 禁用防火墙
print_line
print_step "禁用防火墙..."
systemctl disable --now firewalld.service &>/dev/null
if [ $? -eq 0 ]; then
print_step "防火墙已禁用."
else
echo -e "${RED}[ERROR] 禁用防火墙失败.${RESET}"
exit 1
fi
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
if [ $? -eq 0 ]; then
print_step "SELinux 已禁用."
else
echo -e "${RED}[ERROR] 禁用 SELinux 失败.${RESET}"
exit 1
fi
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "启动并设置开机自启 Nginx..."
systemctl enable nginx --now &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 已启动并设置为开机自启."
else
echo -e "${RED}[ERROR] 启动 Nginx 失败.${RESET}"
exit 1
fi
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.3 rocky_nginx_source_offline_install.sh¶
#!/bin/bash
##############################################################
# File Name:rocky_nginx_source_offline_install.sh
# Version:V1.0
# Author:zq
# Organization:www.zhang-qing.com
# Desc:离线源码编译安装nginx
##############################################################
##############################################################
# 执行脚本说明:
# 1. 请先将 Nginx 相关版本的依赖包和源码包提前放好在 /opt 目录下。
# 2. 依赖包和源码包的命名规则如下:
# - 依赖包:nginx-<version>-deps-<os>-<os_version>.tar.gz
# - 源码包:nginx-<version>.tar.gz
#
# 例如,对于 Nginx 1.23.0 版本,适用于 Rocky Linux 9.4 操作系统的依赖包应命名为:
# nginx-1.23.0-deps-rocky9.4.tar.gz
# nginx-1.23.0.tar.gz
#
# 联网下载依赖包命令示例(以 Nginx 1.23.0 版本为例):
# 1. 使用以下命令下载所有需要的依赖包到指定目录:
# yum install -y gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel libxml2 libxml2-devel libxslt libxslt-devel php-gd gd-devel --downloadonly --downloaddir=/root/nginx-1.23.0-deps-rocky9.4
#
# 2. 使用以下命令将下载的依赖包打包成 tar.gz 格式:
# tar zcvf nginx-1.23.0-deps-rocky9.4.tar.gz -C /root nginx-1.23.0-deps-rocky9.4
#
# 完成这些步骤后,将依赖包和源码包拷贝到 /opt 目录下,供脚本后续安装使用。
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.23.0"
INSTALL_DIR="/data/server/nginx"
SOFTS_DIR="/opt"
NGINX_USER="nginx"
# 定义操作系统和版本号变量
OS="rocky"
OS_VERSION="9.4"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 离线安装依赖
print_line
print_step "离线安装依赖包..."
# 解压并安装依赖包
cd $SOFTS_DIR
tar -zxvf nginx-$NGINX_VERSION-deps-$OS$OS_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION-deps-$OS$OS_VERSION
# 使用 yum localinstall 安装 rpm 包
yum localinstall *.rpm -y &>/dev/null
print_step "依赖包离线安装完成."
# 创建目录
print_line
print_step "创建目录 $INSTALL_DIR ..."
mkdir -p $INSTALL_DIR &>/dev/null
print_step "目录创建完成."
# 下载并解压 Nginx 源码包
print_line
print_step "解压 Nginx 源码包..."
tar -zxvf $SOFTS_DIR/nginx-$NGINX_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION
print_step "Nginx 源码解压完成."
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
print_step "Nginx 用户创建完成."
# 配置 Nginx 编译选项
print_line
print_step "配置 Nginx 编译选项..."
./configure --prefix=$INSTALL_DIR \
--user=$NGINX_USER \
--group=$NGINX_USER \
--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
print_step "Nginx 配置完成."
# 编译并安装 Nginx
print_line
print_step "编译并安装 Nginx..."
make &>/dev/null
make install &>/dev/null
print_step "Nginx 编译和安装完成."
# 设置权限
print_line
print_step "设置文件权限..."
chown -R $NGINX_USER:$NGINX_USER $INSTALL_DIR
print_step "文件权限设置完成."
# 配置环境变量
print_line
print_step "配置 Nginx 环境变量..."
echo "export PATH=$INSTALL_DIR/sbin:$PATH" >> /etc/bashrc
print_step "环境变量配置完成."
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
print_step "SELinux 已禁用."
# 创建 Nginx systemd 服务文件
print_line
print_step "创建 Nginx systemd 服务文件..."
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=$INSTALL_DIR/logs/nginx.pid
ExecStartPre=$INSTALL_DIR/sbin/nginx -t
ExecStart=$INSTALL_DIR/sbin/nginx
ExecReload=$INSTALL_DIR/sbin/nginx -s reload
ExecStop=$INSTALL_DIR/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
print_step "Nginx systemd 服务文件创建完成."
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "重新加载 systemd 配置并启动 Nginx..."
systemctl daemon-reload &>/dev/null
systemctl enable nginx --now &>/dev/null
print_step "Nginx 已启动并设置为开机自启."
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.4 rocky_nginx_source_online_install.sh¶
#!/bin/bash
##############################################################
# File Name:rocky_nginx_source_online_install.sh
# Version:V1.0
# Author:zq
# Organization:www.zhang-qing.com
# Desc:在线源码编译安装nginx
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.23.0"
INSTALL_DIR="/data/server/nginx"
SOFTS_DIR="/softs"
NGINX_USER="nginx"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 安装依赖
print_line
print_step "安装依赖包..."
yum install -y gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel libxml2 libxml2-devel libxslt libxslt-devel php-gd gd-devel &>/dev/null
print_step "依赖包安装完成."
# 创建目录
print_line
print_step "创建目录 /softs ..."
mkdir -p $SOFTS_DIR &>/dev/null
cd $SOFTS_DIR
print_step "目录创建完成."
# 下载 Nginx 源码包
print_line
print_step "下载 Nginx 源码..."
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz &>/dev/null
tar xf nginx-$NGINX_VERSION.tar.gz &>/dev/null
print_step "Nginx 源码下载并解压完成."
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
print_step "Nginx 用户创建完成."
# 进入 Nginx 源码目录
cd nginx-$NGINX_VERSION
# 配置 Nginx 编译选项
print_line
print_step "配置 Nginx 编译选项..."
./configure --prefix=$INSTALL_DIR \
--user=$NGINX_USER \
--group=$NGINX_USER \
--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
print_step "Nginx 配置完成."
# 编译并安装 Nginx
print_line
print_step "编译并安装 Nginx..."
make &>/dev/null
make install &>/dev/null
print_step "Nginx 编译和安装完成."
# 设置权限
print_line
print_step "设置文件权限..."
chown -R $NGINX_USER:$NGINX_USER $INSTALL_DIR
print_step "文件权限设置完成."
# 配置环境变量
print_line
print_step "配置 Nginx 环境变量..."
echo "export PATH=$INSTALL_DIR/sbin:$PATH" >> /etc/bashrc
source /etc/bashrc
print_step "环境变量配置完成."
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
print_step "SELinux 已禁用."
# 创建 Nginx systemd 服务文件
print_line
print_step "创建 Nginx systemd 服务文件..."
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=$INSTALL_DIR/logs/nginx.pid
ExecStartPre=$INSTALL_DIR/sbin/nginx -t
ExecStart=$INSTALL_DIR/sbin/nginx
ExecReload=$INSTALL_DIR/sbin/nginx -s reload
ExecStop=$INSTALL_DIR/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
print_step "Nginx systemd 服务文件创建完成."
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "重新加载 systemd 配置并启动 Nginx..."
systemctl daemon-reload &>/dev/null
systemctl enable nginx --now &>/dev/null
print_step "Nginx 已启动并设置为开机自启."
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.5 ubuntu_nginx_binary_offline_install.sh¶
#!/bin/bash
##############################################################
# File Name:ubuntu_nginx_binary_offline_install.sh
# Version:V1.1
# Author:zq
# Organization:www.zhang-qing.com
# Desc:离线二进制安装Nginx并配置(适用于Ubuntu)
##############################################################
##############################################################
# 执行脚本说明:
# 1. 请先将 Nginx 相关版本的依赖包和二进制包提前放好在 /opt 目录下。
# 2. 依赖包和二进制包的命名规则如下:
# - 依赖包:nginx-<version>-deps-<os>-<os_version>.tar.gz
# - 二进制包:nginx-<version>.tar.gz
#
# 例如,对于 Nginx 1.24.0 版本,适用于 Ubuntu 24.04 操作系统的依赖包应命名为:
# nginx-1.24.0-deps-ubuntu24.04.tar.gz
# nginx-1.24.0.tar.gz
#
# 联网下载依赖包命令示例(以 Nginx 1.24.0 版本为例):
# 1. 使用以下命令下载所有需要的依赖包到指定目录:
# mkdir /root/nginx-1.24.0-deps-ubuntu24.04 && cd /root/nginx-1.24.0-deps-ubuntu24.04
# apt download nginx nginx-core fcgiwrap nginx-doc
#
# 2. 使用以下命令将下载的依赖包打包成 tar.gz 格式:
# tar zcvf nginx-1.24.0-deps-ubuntu24.04.tar.gz -C /root nginx-1.24.0-deps-ubuntu24.04
#
# 完成这些步骤后,将依赖包拷贝到 /opt 目录下,供脚本后续安装使用。
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.24.0"
SOFTS_DIR="/opt"
NGINX_USER="nginx"
# 定义操作系统和版本号变量
OS="ubuntu"
OS_VERSION="24.04"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 安装 Nginx 二进制包
print_line
print_step "离线安装二进制包..."
# 解压并安装 Nginx 依赖包
cd $SOFTS_DIR
tar -zxvf nginx-$NGINX_VERSION-deps-$OS$OS_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION-deps-$OS$OS_VERSION
# 使用 dpkg 安装 deb 包
sudo dpkg -i *.deb &>/dev/null
if [ $? -eq 0 ]; then
print_step "二进制包离线安装完成."
else
echo -e "${RED}[ERROR] 二进制包安装失败.${RESET}"
exit 1
fi
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
sudo useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 用户创建完成."
else
print_step "Nginx 用户已存在."
fi
# 禁用防火墙
print_line
print_step "禁用防火墙..."
sudo systemctl disable --now ufw &>/dev/null
if [ $? -eq 0 ]; then
print_step "防火墙已禁用."
else
echo -e "${RED}[ERROR] 禁用防火墙失败.${RESET}"
exit 1
fi
# 启动 Nginx
print_line
print_step "启动 Nginx..."
sudo systemctl enable nginx --now &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 已启动并设置为开机自启."
else
echo -e "${RED}[ERROR] 启动 Nginx 失败.${RESET}"
exit 1
fi
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.6 ubuntu_nginx_binary_online_install.sh¶
#!/bin/bash
##############################################################
# File Name:ubuntu_nginx_binary_online_install.sh
# Version:V1.1
# Author:zq
# Organization:www.zhang-qing.com
# Desc:Ubuntu系统二进制安装 Nginx 并配置
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_USER="nginx"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 检查 Nginx 是否已经安装
if dpkg -l | grep -q nginx; then
print_step "Nginx 已经安装,跳过安装步骤."
else
# 安装 Nginx 二进制包
print_line
print_step "安装 Nginx 二进制包..."
apt-get update -y &>/dev/null
apt-get install -y nginx nginx-core fcgiwrap nginx-doc &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 安装完成."
else
echo -e "${RED}[ERROR] Nginx 安装失败.${RESET}"
exit 1
fi
fi
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
if id "$NGINX_USER" &>/dev/null; then
print_step "Nginx 用户已存在."
else
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 用户创建完成."
else
echo -e "${RED}[ERROR] 创建 Nginx 用户失败.${RESET}"
exit 1
fi
fi
# 禁用防火墙
print_line
print_step "禁用防火墙..."
systemctl disable --now ufw.service &>/dev/null
if [ $? -eq 0 ]; then
print_step "防火墙已禁用."
else
echo -e "${RED}[ERROR] 禁用防火墙失败.${RESET}"
exit 1
fi
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
if [ $? -eq 0 ]; then
print_step "SELinux 已禁用."
else
echo -e "${RED}[ERROR] 禁用 SELinux 失败.${RESET}"
exit 1
fi
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "启动并设置开机自启 Nginx..."
systemctl enable nginx --now &>/dev/null
if [ $? -eq 0 ]; then
print_step "Nginx 已启动并设置为开机自启."
else
echo -e "${RED}[ERROR] 启动 Nginx 失败.${RESET}"
exit 1
fi
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.7 ubuntu_nginx_source_offline_install.sh¶
#!/bin/bash
##############################################################
# File Name:rocky_nginx_source_offline_install.sh
# Version:V1.0
# Author:zq
# Organization:www.zhang-qing.com
# Desc:离线源码编译安装nginx
##############################################################
##############################################################
# 执行脚本说明:
# 1. 请先将 Nginx 相关版本的依赖包和源码包提前放好在 /opt 目录下。
# 2. 依赖包和源码包的命名规则如下:
# - 依赖包:nginx-<version>-deps-<os>-<os_version>.tar.gz
# - 源码包:nginx-<version>.tar.gz
#
# 例如,对于 Nginx 1.23.0 版本,适用于 Rocky Linux 9.4 操作系统的依赖包应命名为:
# nginx-1.23.0-deps-rocky9.4.tar.gz
# nginx-1.23.0.tar.gz
#
# 联网下载依赖包命令示例(以 Nginx 1.23.0 版本为例):
# 1. 使用以下命令下载所有需要的依赖包到指定目录:
# yum install -y gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel libxml2 libxml2-devel libxslt libxslt-devel php-gd gd-devel --downloadonly --downloaddir=/root/nginx-1.23.0-deps-rocky9.4
#
# 2. 使用以下命令将下载的依赖包打包成 tar.gz 格式:
# tar zcvf nginx-1.23.0-deps-rocky9.4.tar.gz -C /root nginx-1.23.0-deps-rocky9.4
#
# 完成这些步骤后,将依赖包和源码包拷贝到 /opt 目录下,供脚本后续安装使用。
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.23.0"
INSTALL_DIR="/data/server/nginx"
SOFTS_DIR="/opt"
NGINX_USER="nginx"
# 定义操作系统和版本号变量
OS="rocky"
OS_VERSION="9.4"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 离线安装依赖
print_line
print_step "离线安装依赖包..."
# 解压并安装依赖包
cd $SOFTS_DIR
tar -zxvf nginx-$NGINX_VERSION-deps-$OS$OS_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION-deps-$OS$OS_VERSION
# 使用 yum localinstall 安装 rpm 包
yum localinstall *.rpm -y &>/dev/null
print_step "依赖包离线安装完成."
# 创建目录
print_line
print_step "创建目录 $INSTALL_DIR ..."
mkdir -p $INSTALL_DIR &>/dev/null
print_step "目录创建完成."
# 下载并解压 Nginx 源码包
print_line
print_step "解压 Nginx 源码包..."
tar -zxvf $SOFTS_DIR/nginx-$NGINX_VERSION.tar.gz &>/dev/null
cd nginx-$NGINX_VERSION
print_step "Nginx 源码解压完成."
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
print_step "Nginx 用户创建完成."
# 配置 Nginx 编译选项
print_line
print_step "配置 Nginx 编译选项..."
./configure --prefix=$INSTALL_DIR \
--user=$NGINX_USER \
--group=$NGINX_USER \
--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
print_step "Nginx 配置完成."
# 编译并安装 Nginx
print_line
print_step "编译并安装 Nginx..."
make &>/dev/null
make install &>/dev/null
print_step "Nginx 编译和安装完成."
# 设置权限
print_line
print_step "设置文件权限..."
chown -R $NGINX_USER:$NGINX_USER $INSTALL_DIR
print_step "文件权限设置完成."
# 配置环境变量
print_line
print_step "配置 Nginx 环境变量..."
echo "export PATH=$INSTALL_DIR/sbin:$PATH" >> /etc/bashrc
print_step "环境变量配置完成."
# 禁用 SELinux
print_line
print_step "禁用 SELinux..."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
print_step "SELinux 已禁用."
# 创建 Nginx systemd 服务文件
print_line
print_step "创建 Nginx systemd 服务文件..."
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=$INSTALL_DIR/logs/nginx.pid
ExecStartPre=$INSTALL_DIR/sbin/nginx -t
ExecStart=$INSTALL_DIR/sbin/nginx
ExecReload=$INSTALL_DIR/sbin/nginx -s reload
ExecStop=$INSTALL_DIR/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
print_step "Nginx systemd 服务文件创建完成."
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "重新加载 systemd 配置并启动 Nginx..."
systemctl daemon-reload &>/dev/null
systemctl enable nginx --now &>/dev/null
print_step "Nginx 已启动并设置为开机自启."
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"
5.8 ubuntu_nginx_source_online_install.sh¶
#!/bin/bash
##############################################################
# File Name:ubuntu_nginx_source_online_install.sh
# Version:V1.0
# Author:zq
# Organization:www.zhang-qing.com
# Desc:Ubuntu在线源码编译安装nginx
##############################################################
# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# 设置安装路径
NGINX_VERSION="1.22.1"
INSTALL_DIR="/data/server/nginx"
SOFTS_DIR="/softs"
NGINX_USER="nginx"
# 打印分隔线函数
print_line() {
echo -e "${BLUE}########################################################${RESET}"
}
# 打印步骤函数
print_step() {
echo -e "${GREEN}[INFO] ${YELLOW}$1${RESET}"
}
# 安装依赖
print_line
print_step "安装依赖包..."
apt install -y build-essential gcc g++ libc6 libc6-dev libpcre3 libpcre3-dev libssl-dev libsystemd-dev zlib1g-dev libxml2 libxml2-dev libxslt1-dev php-gd libgd-dev geoip-database libgeoip-dev &>/dev/null
print_step "依赖包安装完成."
# 创建目录
print_line
print_step "创建目录 /softs ..."
mkdir -p $SOFTS_DIR &>/dev/null
cd $SOFTS_DIR
print_step "目录创建完成."
# 下载 Nginx 源码包
print_line
print_step "下载 Nginx 源码..."
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz &>/dev/null
tar xf nginx-$NGINX_VERSION.tar.gz &>/dev/null
print_step "Nginx 源码下载并解压完成."
# 创建 Nginx 用户
print_line
print_step "创建 Nginx 用户..."
useradd -r -s /usr/sbin/nologin $NGINX_USER &>/dev/null
print_step "Nginx 用户创建完成."
# 进入 Nginx 源码目录
cd nginx-$NGINX_VERSION
# 配置 Nginx 编译选项
print_line
print_step "配置 Nginx 编译选项..."
./configure --prefix=$INSTALL_DIR \
--user=$NGINX_USER \
--group=$NGINX_USER \
--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
print_step "Nginx 配置完成."
# 编译并安装 Nginx
print_line
print_step "编译并安装 Nginx..."
make &>/dev/null
make install &>/dev/null
print_step "Nginx 编译和安装完成."
# 设置权限
print_line
print_step "设置文件权限..."
chown -R $NGINX_USER:$NGINX_USER $INSTALL_DIR
print_step "文件权限设置完成."
# 配置环境变量
print_line
print_step "配置 Nginx 环境变量..."
export PATH=$INSTALL_DIR/sbin:$PATH
echo 'export PATH=$INSTALL_DIR/sbin:$PATH' >> /etc/bashrc
source /etc/bashrc
print_step "环境变量配置完成."
# 创建 Nginx systemd 服务文件
print_line
print_step "创建 Nginx systemd 服务文件..."
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=$INSTALL_DIR/logs/nginx.pid
ExecStartPre=$INSTALL_DIR/sbin/nginx -t
ExecStart=$INSTALL_DIR/sbin/nginx
ExecReload=$INSTALL_DIR/sbin/nginx -s reload
ExecStop=$INSTALL_DIR/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
print_step "Nginx systemd 服务文件创建完成."
# 重新加载 systemd 配置并启动 Nginx
print_line
print_step "重新加载 systemd 配置并启动 Nginx..."
systemctl daemon-reload &>/dev/null
systemctl enable nginx --now &>/dev/null
print_step "Nginx 已启动并设置为开机自启."
# 打印安装完成信息
print_line
echo -e "${GREEN}[SUCCESS] Nginx 安装完成,并已启动.${RESET}"