Nginx 源码编译安装
Nginx 源码编译安装

Nginx 源码编译安装

#!/bin/bash
#-----------------------------------------------
# 脚本名称:       install_nginx.sh
# 作者:             whao
# 创建时间:     2025-04-26
# 版本:                         v.0.1
# 描述:  Nginx源码编译安装脚本
#------------------------------------------------

NGINX_VERSION="1.28.0"
INSTALL_DIR="/data/server/nginx"
SOFTS_DIR="/data/softs"
SYSTEMD_FILE="/etc/systemd/system/nginx.service"


# 颜色输出
color() {
    local msg="$1"
    local status="$2"
    local label color

    case "$status" in
        success|0) label="  OK  "; color="1;32" ;;
        failure|1) label="FAILED"; color="1;31" ;;
        *)         label=" WARN "; color="1;33" ;;
    esac

    printf "%-40s \033[60G[\033[%sm%s\033[0m]\n" "$msg" "$color" "$label"
}

# 安装依赖
install_dependencies() {
    . /etc/os-release
    if [[ "$ID" == "centos" || "$ID" == "rocky" ]]; then
        yum install -y gcc make  curl wget 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 || {
            color "依赖安装失败" 1
            exit 1
        }
        setenforce 0 && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g " /etc/selinux/config

    elif [[ "$ID" == "ubuntu" ]]; then
        apt update
        apt install -y libxml2 libxml2-dev libxslt1-dev php-gd libgd-dev geoip-database libgeoip-dev \
        build-essential make curl wget gcc g++ libc6 libc6-dev libpcre3 libpcre3-dev \
        libssl-dev libsystemd-dev || {
            color "依赖安装失败" 1
            exit 1
        }
    else
        color "不支持此操作系统" 2
        exit 1
    fi
    color "Nginx编译环境安装" $?
}

# 创建必要目录
mkdir_dir() {
    mkdir -p "$INSTALL_DIR" "$SOFTS_DIR" 
    color "Nginx软件目录创建" $?
}

#创建用户及设置环境变量
useradd_nginx() {
        export PATH="$PATH:/usr/sbin/nologin"
        useradd -r -s /usr/sbin/nologin nginx
        echo "export PATH="$INSTALL_DIR"sbin:$PATH" >> /etc/bashrc
        source /etc/bashrc
}

#下载Nginx
download_nginx() {
    cd ${SOFTS_DIR}
    wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz >/dev/null 2>&1
    tar xf nginx-${NGINX_VERSION}.tar.gz
    color "Nginx软件包下载并解压" $?
}

# 编译安装Nginx
make_nginx(){
    cd "${SOFTS_DIR}/nginx-${NGINX_VERSION}"
    ./configure --prefix="$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
    make >/dev/null && make install >/dev/null 2>&1
    color "Nginx编译安装" $?
    chown -R nginx:nginx "$INSTALL_DIR" && ln -sv "$INSTALL_DIR sbin/nginx" /usr/sbin/nginx >/dev/null 2>&1
    cp "${SOFTS_DIR}/nginx-${NGINX_VERSION}/man/nginx.8" /usr/share/man/man8/

}

#设置服务文件
service_file() {
   cat >> "$SYSTEMD_FILE" <<- eof
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=${INSTALL_DIR}/sbin/nginx -c ${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
   color "Nginx服务文件已设置" $?
}

start_nginx_service(){
    systemctl daemon-reload
    systemctl enable nginx &>/dev/null
    systemctl restart nginx 
    color "Nginx服务启动" $?
}

main(){
    install_dependencies
    mkdir_dir
    useradd_nginx
    download_nginx
    make_nginx
    service_file
    start_nginx_service
}

main

Nginx编译脚本

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注