导航
导航

CentOS 从源码安装 Nginx

Nginx 安装过程比较简单,详见自动安装脚本

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

CUR_DIR=$(pwd)
if [ $(id -u) != "0" ]; then
  printf "Error: You must be root to run this script!"
  exit 1
fi

SSL_VER=openssl-1.0.2l
PCRE_VER=pcre-8.40
NGINX_VER=nginx-1.12.0

DownloadOpenSSL()
{
    [[ ! -f "$SSL_VER.tar.gz" ]] && wget https://www.openssl.org/source/$SSL_VER.tar.gz
    [[ -d "${SSL_VER}" ]] && rm -rf ${SSL_VER}
    tar -zxf $SSL_VER.tar.gz
}

DownloadPcre()
{

    [[ ! -f "${PCRE_VER}.tar.gz" ]] && wget https://ftp.pcre.org/pub/pcre/${PCRE_VER}.tar.gz
    [[ -d "${PCRE_VER}" ]] && rm -rf ${PCRE_VER}
    tar -zxf ${PCRE_VER}.tar.gz
}

InstallNginx()
{
    uninstall

    groupadd www
    useradd -g www -M -s /sbin/nologin www

    echo "---------- Download OpenSSL ----------"

    DownloadOpenSSL

    echo "---------- Download Pcre ----------"

    DownloadPcre

    echo "---------- Nginx Install ----------"

    [[ ! -f "$NGINX_VER.tar.gz" ]] &&  wget http://nginx.org/download/$NGINX_VER.tar.gz
    tar -zxf $NGINX_VER.tar.gz
    cd $NGINX_VER
    ./configure \
        --prefix=/usr/local/nginx \
        --user=www  \
        --group=www  \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-http_realip_module \
        --with-http_sub_module \
        --with-http_gzip_static_module \
        --with-http_stub_status_module  \
        --with-openssl=${CUR_DIR}/${SSL_VER} \
        --with-pcre=${CUR_DIR}/${PCRE_VER} \
        --http-client-body-temp-path=/usr/local/nginx/temp/client_body \
        --http-proxy-temp-path=/usr/local/nginx/temp/proxy \
        --http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi \
        --http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi \
        --http-scgi-temp-path=/usr/local/nginx/temp/scgi

    make && make install
    ln -sf /usr/local/nginx/sbin/nginx /usr/sbin/nginx
    mkdir -p /usr/local/nginx/temp/{client_body,proxy,fastcgi,uwsgi,scgi}

    echo "---------- Nginx Config ----------"

    mkdir -p /usr/local/nginx/conf/sites-enabled/
    mkdir -p /usr/local/nginx/conf/sites-available/
    chmod 711 /usr/local/nginx/conf/sites-*

    mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
    \cp -r $CUR_DIR/conf/* /usr/local/nginx/conf/
    chmod 644 /usr/local/nginx/conf/*.conf

    cp $CUR_DIR/html/* /usr/local/nginx/html
    chmod 644 /usr/local/nginx/conf/sites-enabled/* /usr/local/nginx/html/*

    mkdir /usr/local/nginx/conf/ssl
    chmod 711 /usr/local/nginx/conf/ssl

cat >/usr/lib/systemd/system/nginx.service<<'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

    chmod +x /usr/lib/systemd/system/nginx.service
    systemctl enable nginx.service
    systemctl start nginx.service
}

uninstall()
{
    groupdel www
    userdel -f www

    [[ ! -d "/usr/local/nginx_backup" ]] && mkdir -p /usr/local/nginx_backup
    mv /usr/local/nginx/html /usr/local/nginx/conf /usr/local/nginx_backup
    rm -rf /usr/local/nginx /usr/sbin/nginx

    systemctl disable nginx.service
    rm -f /usr/lib/systemd/system/nginx.service

    rm -f /tmp/nginx_install.log
}

read -p "Are you sure install nginx[y/n]?" ANSWER
case $ANSWER in
y)
   echo "Fine,continue!!!"
   InstallNginx 2>&1 | tee -a /tmp/nginx_install.log ;;
n)
   echo "It is dangerous,bye!!!";;
u)
   uninstall 2>&1 ;;
*)
   echo "Error choice";;
esac
支持一下
扫一扫,支持一下