
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 21; 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_URLhttp://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 \ --usernginx \ --groupnginx \ --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] DescriptionThe nginx HTTP and reverse proxy server Afternetwork.target [Service] Typeforking 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 PrivateTmptrue [Install] WantedBymulti-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 21 || 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_URLhttp://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 \ --usernginx \ --groupnginx \ --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] DescriptionThe nginx HTTP and reverse proxy server Afternetwork.target [Service] Typeforking 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 PrivateTmptrue [Install] WantedBymulti-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 编译错误排查问题1make: *** No rule to make target build原因通常是因为缺少依赖包或configure失败解决方案检查是否安装了所有依赖包查看config.log文件中的错误信息重新运行./configure并检查输出问题2src/os/unix/ngx_user.c: error: struct crypt_data has no member named current_salt原因glibc版本与Nginx源码不兼容解决方案使用较新版本的Nginx或添加编译选项--with-ld-opt-lcrypt6.2 服务管理问题问题Failed to start nginx.service: Unit not found原因systemd服务文件未正确安装解决方案检查/etc/systemd/system/nginx.service是否存在重新加载systemdsystemctl daemon-reload再次尝试启动systemctl start nginx6.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证书支持使用Lets 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 \ --usernginx \ --groupnginx \ --with-http_ssl_module \ --with-http_stub_status_module make make install # 重启服务 systemctl restart nginx }8. 生产环境建议在实际生产环境中部署Nginx时除了基本的安装配置外还需要考虑以下方面高可用架构使用Keepalived实现VIP漂移或使用负载均衡器日志分析配置ELK或Graylog收集分析Nginx日志监控告警使用PrometheusGrafana监控Nginx指标WAF防护集成ModSecurity等Web应用防火墙配置管理使用Ansible等工具实现批量部署和配置管理对于大规模部署建议将上述脚本改造成Ansible Playbook或Terraform模块实现基础设施即代码。