#!/bin/bash # Nginx 一键编译安装脚本 # 基于 buildnginx.mdx 文档 # 适用于 Debian/Ubuntu 系统 # # 使用方法: # 1. 普通用户安装(推荐): bash install-nginx.sh # 2. 自定义安装路径: NGINX_ROOT_PATH=/opt/nginx bash install-nginx.sh # 3. Root用户安装: sudo bash install-nginx.sh # # 默认安装路径: /usr/local/nginx set -e # 遇到错误立即退出 # 配置变量 FANCYINDEX_VERSION="0.5.2" NGINX_ROOT_PATH="${NGINX_ROOT_PATH:-/usr/local/nginx}" # 默认路径,可通过环境变量覆盖 CURRENT_USER=$(whoami) CURRENT_GROUP=$(id -gn) # 获取最新的nginx版本号 get_latest_nginx_version() { print_info "正在获取最新的 nginx 版本号..." # 从nginx官网获取最新稳定版本号 NGINX_VERSION=$(curl -s https://nginx.org/en/download.html | grep -oP 'nginx-\K[0-9]+\.[0-9]+\.[0-9]+(?=\.tar\.gz">nginx)' | head -1) if [ -z "$NGINX_VERSION" ]; then print_warning "无法自动获取最新版本,使用默认版本 1.29.3" NGINX_VERSION="1.29.3" else print_info "检测到最新稳定版本: nginx-${NGINX_VERSION}" fi } # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # 打印带颜色的信息 print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # 检查权限并提示 check_permission() { if [ "$EUID" -eq 0 ]; then print_warning "检测到使用 root 用户运行" USE_SUDO="" else print_info "检测到使用普通用户 ${CURRENT_USER} 运行" print_warning "部分操作可能需要 sudo 权限" USE_SUDO="sudo" fi } # 检查系统类型 check_system() { if [ -f /etc/debian_version ]; then print_info "检测到 Debian/Ubuntu 系统" else print_error "此脚本仅支持 Debian/Ubuntu 系统" exit 1 fi } # 安装编译环境 install_dependencies() { print_info "步骤 1/8: 安装编译环境和依赖..." $USE_SUDO apt update $USE_SUDO apt install -y build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev wget curl print_info "编译环境安装完成" } # 下载并解压 nginx 源码 download_nginx() { print_info "步骤 2/8: 下载并解压 nginx-${NGINX_VERSION}..." cd /tmp wget -q --show-progress https://1ms.cc/https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz tar -xf nginx-${NGINX_VERSION}.tar.gz print_info "nginx 源码解压完成" } # 下载并解压 fancyindex 模块 download_fancyindex() { print_info "步骤 3/8: 下载并解压 ngx-fancyindex 模块..." cd /tmp/nginx-${NGINX_VERSION} wget -q --show-progress https://1ms.cc/https://github.com/aperezdc/ngx-fancyindex/releases/download/v${FANCYINDEX_VERSION}/ngx-fancyindex-${FANCYINDEX_VERSION}.tar.xz tar -xf ngx-fancyindex-${FANCYINDEX_VERSION}.tar.xz print_info "fancyindex 模块解压完成" } # 创建必要目录 create_directories() { print_info "步骤 4/8: 创建必要目录..." $USE_SUDO mkdir -p /var/cache/nginx $USE_SUDO mkdir -p /var/log/nginx # 设置目录权限 if [ -n "$USE_SUDO" ]; then $USE_SUDO chown -R ${CURRENT_USER}:${CURRENT_GROUP} /var/cache/nginx $USE_SUDO chown -R ${CURRENT_USER}:${CURRENT_GROUP} /var/log/nginx fi print_info "目录创建完成" } # 配置编译选项 configure_nginx() { print_info "步骤 5/8: 配置编译选项..." cd /tmp/nginx-${NGINX_VERSION} ./configure --add-module=./ngx-fancyindex-${FANCYINDEX_VERSION} \ --prefix=${NGINX_ROOT_PATH} \ --user=${CURRENT_USER} \ --group=${CURRENT_GROUP} \ --sbin-path=${NGINX_ROOT_PATH}/sbin/nginx \ --conf-path=${NGINX_ROOT_PATH}/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-http_v3_module print_info "配置完成" } # 编译安装 compile_nginx() { print_info "步骤 6/8: 开始编译和安装(这可能需要几分钟)..." cd /tmp/nginx-${NGINX_VERSION} make $USE_SUDO make install # 如果使用普通用户,设置安装目录权限 if [ -n "$USE_SUDO" ]; then $USE_SUDO chown -R ${CURRENT_USER}:${CURRENT_GROUP} ${NGINX_ROOT_PATH} fi print_info "编译安装完成" } # 创建 systemd 服务 create_systemd_service() { print_info "步骤 7/8: 创建 systemd 服务..." $USE_SUDO tee /usr/lib/systemd/system/nginx.service > /dev/null <