Shell脚本实现Nginx一键自动化部署与优化
1. Shell脚本与Nginx一键部署概述
在Linux系统管理中,Shell脚本和Nginx的组合堪称黄金搭档。Shell脚本作为系统管理员的瑞士军刀,能够将复杂的操作流程自动化;而Nginx作为高性能的Web服务器,在互联网服务中占据着重要地位。将二者结合实现一键部署,不仅能提升工作效率,还能确保部署过程的标准化和可重复性。
我曾在多个生产环境中使用Shell脚本部署Nginx,从最初的简单安装到现在的完整配置管理,积累了不少实战经验。本文将分享一个经过实战检验的Nginx一键部署脚本,包含版本选择、依赖检查、编译安装、服务管理等完整流程,同时会详细解释每个关键步骤的设计考量。
2. 环境准备与前置检查
2.1 系统环境要求
Nginx对Linux系统环境有一定要求,我们的脚本需要先进行环境检查:
#!/bin/bash # 检查系统版本 OS_INFO=$(cat /etc/os-release | grep "^ID=" | cut -d= -f2) OS_VERSION=$(cat /etc/os-release | grep "VERSION_ID" | cut -d= -f2 | tr -d '"') # 检查是否为root用户 if [ "$(id -u)" != "0" ]; then echo "错误:此脚本必须以root用户运行" >&2 exit 1 fi # 检查内存是否足够(至少1GB) TOTAL_MEM=$(free -m | awk '/Mem:/ {print $2}') if [ "$TOTAL_MEM" -lt 1024 ]; then echo "警告:系统内存低于1GB,可能影响Nginx性能" >&2 fi注意:不同Linux发行版的包管理命令不同,我们的脚本需要兼容主流的CentOS/RHEL、Ubuntu/Debian等系统。
2.2 依赖包安装
Nginx编译安装需要一些基础开发工具和库文件:
# 根据系统类型安装依赖 case "$OS_INFO" in "centos"|"rhel") yum install -y gcc make pcre-devel zlib-devel openssl-devel wget ;; "ubuntu"|"debian") apt-get update apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev wget ;; *) echo "不支持的Linux发行版:$OS_INFO" >&2 exit 1 ;; esac # 检查依赖是否安装成功 for cmd in gcc make wget; do if ! command -v $cmd >/dev/null 2>&1; then echo "错误:$cmd 安装失败" >&2 exit 1 fi done这个环节有几个关键点需要注意:
- 不同发行版的包名可能不同(如pcre-devel vs libpcre3-dev)
- 需要处理网络连接失败的情况
- 某些系统可能需要额外的epel仓库
3. Nginx安装与配置
3.1 版本选择与下载
我们建议使用稳定版Nginx,脚本中可以实现自动获取最新稳定版本:
# 获取最新稳定版版本号 NGINX_STABLE_VERSION=$(wget -qO- http://nginx.org/en/CHANGES | awk '/Changes with nginx/ {print $4; exit}') # 下载源码包 NGINX_SRC_URL="http://nginx.org/download/nginx-${NGINX_STABLE_VERSION}.tar.gz" wget --no-check-certificate $NGINX_SRC_URL -P /tmp/ if [ ! -f "/tmp/nginx-${NGINX_STABLE_VERSION}.tar.gz" ]; then echo "Nginx源码包下载失败" >&2 exit 1 fi # 解压源码 tar zxf /tmp/nginx-${NGINX_STABLE_VERSION}.tar.gz -C /usr/local/src/提示:生产环境中建议将版本号固定,避免自动升级带来的不可控因素。
3.2 编译安装配置
Nginx的编译选项直接影响其性能和功能,以下是一个经过优化的配置:
cd /usr/local/src/nginx-${NGINX_STABLE_VERSION} ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-threads make -j$(nproc) && make install if [ $? -ne 0 ]; then echo "Nginx编译安装失败" >&2 exit 1 fi关键编译选项说明:
--with-http_ssl_module:启用HTTPS支持--with-http_stub_status_module:启用状态监控--with-threads:提高性能-j$(nproc):使用所有CPU核心加速编译
3.3 系统服务集成
为了方便管理,我们需要将Nginx设置为系统服务:
# 创建nginx用户 useradd -r -s /sbin/nologin 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=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID ExecStop=/bin/kill -s QUIT \$MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 重新加载systemd并启用服务 systemctl daemon-reload systemctl enable nginx4. 安全加固与优化配置
4.1 基础安全配置
安装完成后需要对Nginx进行安全加固:
# 备份原始配置文件 cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak # 应用安全配置 sed -i 's/#server_tokens off;/server_tokens off;/' /usr/local/nginx/conf/nginx.conf sed -i 's/worker_connections 1024;/worker_connections 4096;/' /usr/local/nginx/conf/nginx.conf # 创建专用目录存放网站内容 mkdir -p /var/www/html chown -R nginx:nginx /var/www chmod -R 755 /var/www # 配置日志轮转 cat > /etc/logrotate.d/nginx <<EOF /usr/local/nginx/logs/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate [ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \$(cat /usr/local/nginx/logs/nginx.pid) endscript } EOF4.2 性能优化建议
根据服务器配置调整Nginx性能参数:
# 根据CPU核心数设置worker进程数 CPU_CORES=$(grep -c ^processor /proc/cpuinfo) sed -i "s/worker_processes 1;/worker_processes $CPU_CORES;/" /usr/local/nginx/conf/nginx.conf # 调整连接相关参数 cat >> /usr/local/nginx/conf/nginx.conf <<EOF events { worker_connections 4096; multi_accept on; use epoll; } http { client_header_timeout 10; client_body_timeout 10; keepalive_timeout 30; send_timeout 10; client_max_body_size 20m; client_body_buffer_size 128k; client_header_buffer_size 1k; large_client_header_buffers 4 4k; sendfile on; tcp_nopush on; tcp_nodelay on; } EOF5. 脚本完整实现与使用说明
5.1 完整一键部署脚本
将上述所有步骤整合为一个完整的脚本:
#!/bin/bash # Nginx一键部署脚本 # 功能:自动安装最新稳定版Nginx并完成基础配置 # 作者:Your Name # 日期:$(date +%Y-%m-%d) set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # 日志函数 log() { echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}" } error() { echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] 错误:$1${NC}" >&2 exit 1 } warn() { echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] 警告:$1${NC}" >&2 } # 检查系统环境 check_environment() { log "开始检查系统环境..." # 检查root权限 [ "$(id -u)" != "0" ] && error "此脚本必须以root用户运行" # 检查系统类型 OS_INFO=$(cat /etc/os-release | grep "^ID=" | cut -d= -f2) case "$OS_INFO" in "centos"|"rhel"|"ubuntu"|"debian") ;; *) error "不支持的Linux发行版:$OS_INFO" ;; esac # 检查内存 TOTAL_MEM=$(free -m | awk '/Mem:/ {print $2}') [ "$TOTAL_MEM" -lt 1024 ] && warn "系统内存低于1GB,可能影响Nginx性能" log "系统环境检查完成" } # 安装依赖 install_dependencies() { log "开始安装依赖包..." case "$OS_INFO" in "centos"|"rhel") yum install -y gcc make pcre-devel zlib-devel openssl-devel wget ;; "ubuntu"|"debian") apt-get update apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev wget ;; esac # 验证依赖安装 for cmd in gcc make wget; do command -v $cmd >/dev/null 2>&1 || error "$cmd 安装失败" done log "依赖包安装完成" } # 下载并编译Nginx install_nginx() { log "开始安装Nginx..." # 获取最新稳定版 NGINX_STABLE_VERSION=$(wget -qO- http://nginx.org/en/CHANGES | awk '/Changes with nginx/ {print $4; exit}') NGINX_SRC_URL="http://nginx.org/download/nginx-${NGINX_STABLE_VERSION}.tar.gz" # 下载源码 log "正在下载Nginx ${NGINX_STABLE_VERSION}..." wget --no-check-certificate $NGINX_SRC_URL -P /tmp/ || error "Nginx源码包下载失败" # 解压源码 tar zxf /tmp/nginx-${NGINX_STABLE_VERSION}.tar.gz -C /usr/local/src/ || error "Nginx源码解压失败" # 编译安装 cd /usr/local/src/nginx-${NGINX_STABLE_VERSION} log "开始编译Nginx..." ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-threads make -j$(nproc) && make install || error "Nginx编译安装失败" log "Nginx安装完成,版本:${NGINX_STABLE_VERSION}" } # 配置系统服务 configure_service() { log "开始配置系统服务..." # 创建nginx用户 id nginx &>/dev/null || useradd -r -s /sbin/nologin 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=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID ExecStop=/bin/kill -s QUIT \$MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 重新加载systemd systemctl daemon-reload systemctl enable nginx log "系统服务配置完成" } # 安全加固 secure_nginx() { log "开始安全加固..." # 备份原始配置 cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak # 应用安全配置 sed -i 's/#server_tokens off;/server_tokens off;/' /usr/local/nginx/conf/nginx.conf sed -i 's/worker_connections 1024;/worker_connections 4096;/' /usr/local/nginx/conf/nginx.conf # 创建网站目录 mkdir -p /var/www/html chown -R nginx:nginx /var/www chmod -R 755 /var/www # 配置日志轮转 cat > /etc/logrotate.d/nginx <<EOF /usr/local/nginx/logs/*.log { daily missingok rotate 30 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate [ -f /usr/local/nginx/logs/nginx.pid ] && kill -USR1 \$(cat /usr/local/nginx/logs/nginx.pid) endscript } EOF log "安全加固完成" } # 性能优化 optimize_nginx() { log "开始性能优化..." # 根据CPU核心数设置worker进程数 CPU_CORES=$(grep -c ^processor /proc/cpuinfo) sed -i "s/worker_processes 1;/worker_processes $CPU_CORES;/" /usr/local/nginx/conf/nginx.conf # 添加性能优化配置 if ! grep -q "events {" /usr/local/nginx/conf/nginx.conf; then cat >> /usr/local/nginx/conf/nginx.conf <<EOF events { worker_connections 4096; multi_accept on; use epoll; } http { client_header_timeout 10; client_body_timeout 10; keepalive_timeout 30; send_timeout 10; client_max_body_size 20m; client_body_buffer_size 128k; client_header_buffer_size 1k; large_client_header_buffers 4 4k; sendfile on; tcp_nopush on; tcp_nodelay on; } EOF fi log "性能优化完成" } # 启动Nginx start_nginx() { log "正在启动Nginx服务..." systemctl start nginx if systemctl is-active --quiet nginx; then log "Nginx启动成功" log "访问地址:http://$(hostname -I | awk '{print $1}')" else error "Nginx启动失败,请检查日志:journalctl -xe" fi } # 主函数 main() { check_environment install_dependencies install_nginx configure_service secure_nginx optimize_nginx start_nginx log "Nginx一键部署完成!" } main "$@"5.2 脚本使用说明
- 保存脚本:将上述内容保存为
install_nginx.sh - 赋予执行权限:
chmod +x install_nginx.sh - 执行脚本:
./install_nginx.sh - 验证安装:
- 检查服务状态:
systemctl status nginx - 测试访问:
curl http://localhost
- 检查服务状态:
- 后续配置:
- 主配置文件:
/usr/local/nginx/conf/nginx.conf - 网站目录:
/var/www/html - 日志文件:
/usr/local/nginx/logs/
- 主配置文件:
6. 常见问题与解决方案
6.1 编译错误排查
问题1:make: *** No rule to make target 'build'
原因:通常是因为缺少依赖包或configure失败
解决方案:
- 检查是否安装了所有依赖包
- 查看
config.log文件中的错误信息 - 重新运行
./configure并检查输出
问题2:src/os/unix/ngx_user.c: error: 'struct crypt_data' has no member named 'current_salt'
原因:glibc版本与Nginx源码不兼容
解决方案:
- 使用较新版本的Nginx
- 或添加编译选项:
--with-ld-opt="-lcrypt"
6.2 服务管理问题
问题:Failed to start nginx.service: Unit not found
原因:systemd服务文件未正确安装
解决方案:
- 检查
/etc/systemd/system/nginx.service是否存在 - 重新加载systemd:
systemctl daemon-reload - 再次尝试启动:
systemctl start nginx
6.3 性能调优建议
- worker_processes:设置为CPU核心数
- worker_connections:根据内存调整,每个连接约占用256KB内存
- 启用gzip:减少传输数据量
- 启用缓存:对于静态资源设置expires头
- 调整缓冲区大小:根据平均请求大小调整
7. 脚本扩展与定制
7.1 添加虚拟主机支持
可以在脚本中添加自动创建虚拟主机的功能:
add_vhost() { local domain=$1 local root_dir="/var/www/${domain}" # 创建网站目录 mkdir -p "${root_dir}" chown nginx:nginx "${root_dir}" # 创建虚拟主机配置 cat > "/usr/local/nginx/conf/vhost/${domain}.conf" <<EOF server { listen 80; server_name ${domain}; root ${root_dir}; index index.html index.htm; access_log /var/log/nginx/${domain}.access.log; error_log /var/log/nginx/${domain}.error.log; location / { try_files \$uri \$uri/ =404; } } EOF # 测试并重载配置 /usr/local/nginx/sbin/nginx -t && systemctl reload nginx }7.2 添加SSL证书支持
使用Let's Encrypt自动获取SSL证书:
install_ssl() { local domain=$1 # 安装certbot case "$OS_INFO" in "centos"|"rhel") yum install -y epel-release yum install -y certbot python2-certbot-nginx ;; "ubuntu"|"debian") apt-get install -y certbot python3-certbot-nginx ;; esac # 获取证书 certbot --nginx -d ${domain} --non-interactive --agree-tos -m admin@${domain} # 设置自动续期 (crontab -l 2>/dev/null; echo "0 3 * * * /usr/bin/certbot renew --quiet") | crontab - }7.3 多版本管理
对于需要测试不同Nginx版本的场景,可以扩展脚本支持多版本切换:
switch_nginx_version() { local version=$1 # 下载指定版本 wget "http://nginx.org/download/nginx-${version}.tar.gz" -P /tmp/ tar zxf "/tmp/nginx-${version}.tar.gz" -C /usr/local/src/ # 编译安装 cd "/usr/local/src/nginx-${version}" ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module make && make install # 重启服务 systemctl restart nginx }8. 生产环境建议
在实际生产环境中部署Nginx时,除了基本的安装配置外,还需要考虑以下方面:
- 高可用架构:使用Keepalived实现VIP漂移,或使用负载均衡器
- 日志分析:配置ELK或Graylog收集分析Nginx日志
- 监控告警:使用Prometheus+Grafana监控Nginx指标
- WAF防护:集成ModSecurity等Web应用防火墙
- 配置管理:使用Ansible等工具实现批量部署和配置管理
对于大规模部署,建议将上述脚本改造成Ansible Playbook或Terraform模块,实现基础设施即代码。
