04-centos7.4一键安装nginx-1.16.1【固定端口82】

一键安装nginx-1.16.1【固定端口82】

nginx版本:nginx-1.16.1

nginx安装路径:/etc/nginx/

用户名称:www

用户UID:1200

  • 配置文件
[nameke@web01 ~]$ ll conf/
total 48
-rw-r--r--. 1 nameke nameke  494 Oct 30  2020 50x.html
-rw-r--r--. 1 nameke nameke   52 Dec 14 07:43 index.html
-rw-r--r--. 1 nameke nameke   62 Sep 28  2020 index.php
-rw-r--r--. 1 nameke nameke  139 Nov  3  2020 mobile.conf
-rw-r--r--. 1 nameke nameke 1697 Sep 28  2020 my.cnf
-rw-r--r--. 1 nameke nameke 1245 Dec 14 07:34 nginx.conf
-rw-r--r--. 1 nameke nameke  225 Nov  3  2020 pc.conf
-rw-r--r--. 1 nameke nameke   20 Sep 28  2020 test_info.php
-rw-r--r--. 1 nameke nameke  404 Sep 28  2020 test_mysql.php
-rw-r--r--. 1 nameke nameke   21 Sep 28  2020 test_php.php
-rw-r--r--. 1 nameke nameke 1044 Oct 29  2020 webdemo.conf
-rw-r--r--. 1 nameke nameke  889 Dec 14 07:48 www.shinebook.org.conf
  • 脚本内容
#!/bin/bash
#Author:chenwei
#Time:2020-07-28 11:30:44
#Name:auto_install_nginx_v1.sh
#Version:V1.0
#Description:This script is used for automatically

#1.0 检查nginx安装情况
function usage_nginx(){
    echo -e "\033[31m Input Error,Try agian! \033[0m"
    echo -e "\033[33m Tip: Nginx port defaults to 82! \033[0m"
    exit 2;
}

#2.0 定义环境变量
function define_env(){
    #read -p "Please input Nginx port number: " ngx_port
    echo -e "\033[32m Tip: Nginx port defaults to 82! \033[0m" && sleep 3
    Nginx_PORT="82"
    Nginx_Domain="www.shinebook.org"
    TOOLS_PATH="/home/nameke/tools"
    NGX_CONF_PATH="/home/nameke/conf"
    #WEBDEMO_CONF_PATH="/etc/nginx/vhost"
    WEBCONF_DEDIR="/etc/nginx/sites-available"
    WEBCONF_SODIR="/etc/nginx/sites-enabled"
}
#2.1 检查/etc/hosts配置解析情况
function check_hosts(){
    grep_num=$(grep "nginx" /etc/rc.local|egrep -v grep|wc -l)
    grep_hosts=$(grep "$Nginx_Domain" /etc/hosts|egrep -v grep|wc -l)
    #判断/etc/hosts配置DNS解析情况
    if [ $grep_hosts -eq 0 ]
    then
        echo -e  "$(hostname -I|awk '{print $1}') $Nginx_Domain" >>/etc/hosts && echo -e "\033[32m config /etc/hosts succ! \033[0m"
    else
        echo -e  "$(hostname -I|awk '{print $1}') $Nginx_Domain had exists in /etc/hosts"
    fi
    #2.2 判断是否访问成功
    curl -i "http://$Nginx_Domain:$Nginx_PORT/index.html"
    if [ $? -eq 0 ]
    then
        echo -e "\033[32m visit nginx service succ! \033[0m"
    else
        echo -e "\033[31m visit nginx service fail!please check! \033[0m"
    fi
}

#3.0 定义nginx检查函数
function check_nginx(){
    echo -e "\033[32m ------>>> Checking Nginx Service <<<----" && sleep 5
    ngx_port=$(netstat -antlp|grep "$Nginx_PORT"|egrep -v grep|wc -l)
    ngx_process=$(ps aux|grep nginx|egrep -v grep|wc -l)
    if [ "$ngx_port" -gt "0" -a "$ngx_process" -gt "0" ]
    then
        echo "\033[32m Nginx had installed! \033[0m" 
        check_hosts;
        exit 2
    else
        echo -e "\033[31m Nginx doesn't installed! \033[0m"
    fi
}

#4.0 编译安装基础环境
function install_yum(){
    yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-cc+ glibc glibc-devel pcre \
    pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils \
    automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
}
#5.0 添加nginx用户www
function useradd_www(){
    id www
    if [ $? -ne 0 ]
    then
        echo -e "\033[31m user www doesn't exists! \033[0m" && useradd -u 1200 -s /sbin/nologin -M www
        grep -w www /etc/passwd && echo -e "\033[32m useradd www succ! \033[0m"
    else
        echo -e "\033[33m user www had existed! \033[0m"
    fi
}
#6.0 编译安装nginx
function install_nginx(){
    cd $TOOLS_PATH && wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
    tar zxf nginx-1.16.1.tar.gz
    cd $TOOLS_PATH/nginx-1.16.1/ && ./configure --prefix=/etc/nginx-1.16.1 \
    --user=www \
    --group=www \
    --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

    if [ $? -eq 0 ]
    then
        echo -e "\033[32m configure nginx succ! \033[0m"
        make && make install ##make,根据Makefile文件生成相应的模块;make install创建目录并将生成的模块和文件复制到相应的目录
        if [ $? -eq 0 ]
        then
            echo -e "\033[32m make and make install succ! \033[0m" && ln -s /etc/nginx-1.16.1 /etc/nginx && cd ../
        else
            echo -e "\033[32m make and make install fail!,please check! \033[0m"
            exit 2;
        fi
    else
        echo -e "\033[32m configure nginx fail!please check! \033[0m"
        exit 2;
    fi
}
#6.0 配置nginx文件
function config_nginx(){
    if [ ! -f /usr/lib/systemd/system/nginx.service ]
    then
    cat >>/usr/lib/systemd/system/nginx.service<<EOF
[Unit]

Description=The NGINX HTTP and reverse proxy-server
#After=syslog.target network-online.target remote-fs.target nss-lookup.target
#Wants=network-online.target
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/etc/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /etc/nginx/logs/nginx.pid
ExecStartPre=/etc/nginx/sbin/nginx -t
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/etc/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s HUP $MAINPID
#KillSignal=SIGQUIT
#TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    fi
    if [ -f $NGX_CONF_PATH/nginx.conf -a -f $NGX_CONF_PATH/${Nginx_Domain}.conf -a -f $NGX_CONF_PATH/index.html ]
    then
        echo -e "\033[32m $NGX_CONF_PATH/{nginx.conf,${Nginx_Domain}.conf,index.html} are exists! \033[0m"
        \cp -f $NGX_CONF_PATH/nginx.conf /etc/nginx/conf/ && echo -e "\033[32m copy nginx.conf succ! \033[0m"
        #拷贝$Nginx_Domain.conf配置文件
        if [ ! -d $WEBCONF_DEDIR ]
        then
            cd /etc/nginx/ && mkdir $WEBCONF_DEDIR $WEBCONF_SODIR && echo -e "\033[32m mkdir $WEBCONF_DEDIR,$WEBCONF_SODIR succ! \033[0m" && \cp -f $NGX_CONF_PATH/${Nginx_Domain}.conf $WEBCONF_DEDIR/${Nginx_Domain}.conf
            if [ -f $WEBCONF_DEDIR/$Nginx_Domain.conf ]
            then
                ln -sf $WEBCONF_DEDIR/${Nginx_Domain}.conf $WEBCONF_SODIR/${Nginx_Domain}.conf
                if [ $? -eq 0 ]
                then
                    echo -e "\033[32m ln -s nginx config succ! \033[0m"
                else
                    echo -e "\033[31m ln -s nginx config fail!please check! \033[0m"
                fi
            else
                echo -e "\033[31m $WEBCONF_DEDIR/${Nginx_Domain}.conf doesn't found!please check! \033[0m"
            fi
            #sed -i "s#82#$Nginx_PORT#g" $WEBCONF_DEDIR/$Nginx_Domain.conf && sed -i "s#www.shinebook.org#$Nginx_Domain#g" $WEBCONF_DEDIR/$Nginx_Domain.conf
            echo -e "\033[32m Replace $Nginx_PORT and $Nginx_Domain succ! \033[0m" && sleep 3
            #rep_port=$(grep "$Nginx_PORT" $WEBCONF_SODIR/${Nginx_Domain}.conf|egrep -v grep|wc -l)
            #if [ "$rep_port" -gt "0" ]
            #then
            #    echo -e "\033[32m $Nginx_PORT found in $WEBCONF_SODIR/${Nginx_Domain}.conf! \033[0m" 
            #else
            #    echo -e "\033[31m $Nginx_PORT doesn't found in $WEBCONF_SODIR/${Nginx_Domain}.conf,please check! \033[0m"
            #    exit 2;
            #fi
        fi
        #拷贝index.html首页文件
        echo "Welcome to visit $Nginx_Domain $(date +%Y%m%d-%H-%M%S)" >$NGX_CONF_PATH/index.html
        if [ ! -d /webroot/$Nginx_Domain ]
        then
            echo -e "\033[33m /webroot/$Nginx_Domain doesn't exists! \033[0m" && mkdir -p /webroot/$Nginx_Domain
            \cp -f $NGX_CONF_PATH/index.html /webroot/$Nginx_Domain/index.html && chown -R www:www /webroot/$Nginx_Domain/
            echo "\033[32m config /webroot/$Nginx_Domain/index.html succ! \033[0m"
        fi
    else
        echo -e "\033[31m $NGX_CONF_PATH/{nginx.conf,${Nginx_Domain}.conf,index.html} doesn't exist! \033[0m"
        exit 2;
    fi
}

#7.0 检查和启动nginx
function start_ngxservice(){
    chown -R www:www /etc/nginx/
    if [ -f /usr/lib/systemd/system/nginx.service ]
    then
        systemctl daemon-reload && systemctl start nginx.service && systemctl enable nginx.service
        check_nginx
    else
        echo -e "\033[31m /usr/lib/systemd/system/nginx.service doesn't exist! \033[0m"
        echo -e "\033[31m Start Nginx fail! \033[0m"
    fi
}
#8.0 多频道启动nginx
function start_nginx(){
    chown -R www:www /etc/nginx/ && chown -R www:www /etc/nginx/*
    /etc/nginx/sbin/nginx -t && /etc/nginx/sbin/nginx && /etc/nginx/sbin/nginx -s reload 
}

function main(){
    if [ $UID -ne 0 ]
    then
        echo -e "\033[31m You must be root to run this script! \033[0m"
        usage_nginx
    else
        define_env
        echo -e "\033[32m Start Installing Nginx,Please wait....\033[0m"
        check_nginx;
        install_yum;
        useradd_www;
        install_nginx;
        config_nginx;
        ##start_nginx;
        start_ngxservice;
        check_nginx
        echo -e "\033[32m End of Install Nginx \033[0m"
        exit 2;
    fi
}
main

 

posted @ 2020-11-27 19:14  西瓜的春天  阅读(75)  评论(0)    收藏  举报