shell版的nginx安裝

#!/bin/bash
# Name:Centos 6.4 安裝nginx1.8.1
# Date:2016-05-27
# Author:qifei@meizu.com

Home=$(cd `dirname $0`;pwd) #這個命令寫在腳本文件裏面纔有用,它返回這個腳本文件放置的目錄,並能夠根據這個目錄來定位所要運行程序的相對位置
Nginx=nginx-1.12.1.tar.gz
Pcre=pcre-8.33.tar.gz
Jvm_Route=nginx-upstream-jvm-route-master.zip
NGX_Cache=ngx_cache_purge-master.zip
NGX_Check=nginx_upstream_check_module-master.zip
NGX_Sticky=nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
NGX_Upload=nginx-upload-module-2.255.zip
NGX_Openssl=openssl-1.0.1s.tar.gz
TmpSoft=/tmp/software
LogFile=/tmp/`basename $0`.log #`basename $0`值顯示當前腳本或命令的名字,$0顯示會包括當前腳本或命令的路徑,`basename $1`和$1相同

# ================================= System Basic setting

. $Home/funcations.sh

#此代碼內容在其餘頁面
#1、定義控制檯輸出字符顏色
#2、肯定安裝包是否存在,以及不存在時的網絡下載方法

Usage() {

echo ""
echo " ==================================================================="
echo " || Deploy Nginx 1.12.1 ||"
echo " ==================================================================="
echo ""

}


Basic_Set() {

Log "System basic setting --------------->"
if id nginx &> /dev/null;then #id nginx的執行結果爲真,則nginx用戶存在
GreenChar "User:nginx exist:" 2>&1|tee -a $LogFile #會以綠色字體輸出「User:nginx exist:---------------[OK]」在控制檯, 
else
useradd nginx -u 505 -s /sbin/nologin -d /dev/null &>> $LogFile #若是沒有nginx用戶,則添加不登陸方式的nginx用戶,並指定用戶id,-d指定登陸目錄
GreenChar "User:Add nginx:" 2>&1|tee -a $LogFile 
fi

yum -y install libcurl-devel
yum -y groupinstall "Development tools" "Server Platform Libraries" &>> $LogFile
yum -y install gd gd-devel pcre-devel &>> $LogFile

Log "System basic setting <---------------"

[ $? -eq 0 ] && GreenChar "<< Basic >> Setting-prepare:" 2>&1|tee -a $LogFile || RedChar "<< Basic >> Setting-prepare:" 2>&1|tee -a $LogFile
#$0爲最後一次命令的執行狀態,成功爲0,失敗通常爲1,上一步無命令則返回127;若是上一步執行成功,則綠色標記「<< Basic >> Setting-prepare:-----------[OK]」 ;
#不然紅色標記「<< Basic >> Setting-prepare:-----------[Failed]」
}

# ================================= Install Pcre

Pcre_Install() {

Software_Pre ${Pcre}

if [ -d $TmpSoft/pcre-8.33 ];then
cd $TmpSoft/pcre-8.33
else
RedBGChar "PcreDir not exist"
exit 3
fi

make clean all &> /dev/null

Log "${Pcre} Install --------------->"
./configure &>> $LogFile
make &>> $LogFile
make install &>> $LogFile

Log "${Pcre} Install <---------------"

[ $? -eq 0 ] && GreenChar "<< $Pcre >> install is ..." 2>&1|tee -a $LogFile || RedChar "<< $Pcre >> install..." 2>&1|tee -a $LogFile
}


# ================================= ngx_cache_purge Plugin

# ================================= upstream-jvm-route Patch

# ================================= nginx_upstream_check Patch

Nginx_Install() {

Software_Pre ${Nginx}
Software_Pre ${Jvm_Route}
Software_Pre ${NGX_Cache}
Software_Pre ${NGX_Check}
Software_Pre ${NGX_Upload}
Software_Pre ${NGX_Sticky}
Software_Pre ${NGX_Openssl}

[ ! -d /data/tmp/nginx ] && mkdir -p /data/tmp/nginx #目錄不存在建立愛
[ ! -d /data/log/nginx ] && mkdir -p /data/log/nginx
[ ! -d /data/nginx/tmp/client ] && mkdir -p /data/nginx/tmp/client
[ ! -d /data/nginx/tmp/proxy ] && mkdir -p /data/nginx/tmp/proxy
[ ! -d /data/nginx/tmp/fcgi ] && mkdir -p /data/nginx/tmp/fcgi
[ ! -d /data/nginx/tmp/uwsgi ] && mkdir -p /data/nginx/tmp/uwsgi
[ ! -d /data/nginx/tmp/scgi ] && mkdir -p /data/nginx/tmp/scgi

 

if [ -d $TmpSoft/nginx-1.12.1 ];then
cd $TmpSoft/nginx-1.12.1
else
RedBGChar "NginxDir not exist"
exit 3
fi

#對nginx打補丁
cd ${TmpSoft}/nginx-1.12.1
patch -p0 < ${TmpSoft}/nginx-upstream-jvm-route-master/jvm_route.patch &>> $LogFile

cd ${TmpSoft}/nginx-1.12.1
patch -p0 < ${TmpSoft}/nginx_upstream_check_module-master/check_1.11.5+.patch &>> $LogFile

cd ${TmpSoft}/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
patch -p0 < ${TmpSoft}/nginx_upstream_check_module-master/nginx-sticky-module.patch &>> $LogFile

# 開始編譯nginx
cd ${TmpSoft}/nginx-1.12.1
make clean all &> /dev/null

Log "${Nginx} Install --------------->"

./configure \
--prefix=/data/nginx \
--error-log-path=/data/log/nginx/error.log \
--http-log-path=/data/log/nginx/access.log \
--pid-path=/data/nginx/tmp/nginx.pid \
--lock-path=/data/nginx/tmp/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-openssl=${TmpSoft}/openssl-1.0.1s \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/data/nginx/tmp/client/ \
--http-proxy-temp-path=/data/nginx/tmp/proxy/ \
--http-fastcgi-temp-path=/data/nginx/tmp/fcgi/ \
--http-uwsgi-temp-path=/data/nginx/tmp/uwsgi \
--http-scgi-temp-path=/data/nginx/tmp/scgi \
--with-pcre=${TmpSoft}/pcre-8.33 \
--with-file-aio \
--add-module=${TmpSoft}/nginx-upstream-jvm-route-master \
--add-module=${TmpSoft}/ngx_cache_purge-master \
--add-module=${TmpSoft}/nginx_upstream_check_module-master \
--add-module=${TmpSoft}/nginx-upload-module-2.255 \
--add-module=${TmpSoft}/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 \
--with-http_image_filter_module &>> $LogFile

make &>> $LogFile
make install &>> $LogFile

Log "${Pcre} Install <---------------"

[ $? -eq 0 ] && GreenChar "<< $Nginx >> install is ..." 2>&1|tee -a $LogFile || RedChar "NGINX-1.2.9 install is ..." 2>&1|tee -a $LogFile
}

 

Nginx_Conf() {

Log "Nginx Config --------------->"
/bin/cp -p $Home/config/nginx/cutlog.sh /data/nginx/sbin/
/bin/cp -p $Home/config/nginx/nginx_status.sh /data/nginx/sbin/
/bin/cp -rfp $Home/config/nginx/conf /data/nginx/
chown -R nginx:nginx /data/nginx
chmod -R 755 /data/nginx

grep -i Nginx /etc/rc.local &> /dev/null && echo "nginx start config exist" &>> $LogFile || echo "/data/nginx/sbin/nginx" >> /etc/rc.local
#過濾/etc/rc.local文件中是否有nginx的啓動路徑,若是有則輸出「nginx start config exist」到日誌文件,沒有則追加啓動路徑「/data/nginx/sbin/nginx」到/etc/rc.local

IP=`ifconfig|grep -E "10\..*\..*\..*|172\..*\..*\..*" |head -n 1 |awk -F'[: ]+' '{print $4}'`
sed -i "s@server_name.*@server_name $IP 127.0.0.1\;@" /data/nginx/conf/vhosts/zabbix.com &>> $LogFile

# config crontab
n_f=$(crontab -l |grep 'find /data/log/nginx/' |wc -l) #過濾nginx的定時任務'find /data/log/nginx/'的行數
if [ "$n_f" == "0" ] #若是定時任務沒有關於'find /data/log/nginx/'的任務,則追加關於'find /data/log/nginx/'到/var/spool/cron/root定時任務文件
then
#echo "running"
cat >> /var/spool/cron/root << EOF
0 1 * * * find /data/log/nginx/ -mtime +30 -exec rm -rf {} \; &>> /var/log/cront.log
EOF
fi

n_c=$(crontab -l |grep '/data/nginx/sbin/cutlog.sh' |wc -l) 
if [ "$n_c" == "0" ]
then
#echo "running"
cat >> /var/spool/cron/root << EOF
t
0 0 * * * /data/nginx/sbin/cutlog.sh &>> /var/log/cront.log
EOF
fi

Log "Nginx Config <---------------"
[ $? -eq 0 ] && GreenChar "NGINX-1.12.1 Config is ..." 2>&1|tee -a $LogFile || RedChar "NGINX-1.12.1 Config is ..." 2>&1|tee -a $LogFile
#判斷nginx配置是否完成
}

main() {

> $LogFile
Usage 2>&1|tee -a $LogFile
Basic_Set
Pcre_Install
Nginx_Install
Nginx_Conf

}

main

if /data/nginx/sbin/nginx -t &> /dev/null;then
/data/nginx/sbin/nginx &>> $LogFile

echo ""
echo "|-------------------------------------------------------------------|" 2>&1|tee -a $LogFile
echo -e "Nginx-Version:\n \t`/data/nginx/sbin/nginx -V 2>&1 | sed -n '1p'`" 2>&1|tee -a $LogFile
echo ""
echo -e "Nginx-Config:\n \t`/data/nginx/sbin/nginx -V 2>&1 | sed -n '4p'`" 2>&1|tee -a $LogFile
echo "|-------------------------------------------------------------------|" 2>&1|tee -a $LogFile
echo ""

GreenBGChar " nginx 1.12.1 install Success complete " 2>&1|tee -a $LogFile

#RedBGChar "In the END ,please remember mount MFS" 2>&1|tee -a $LogFile

else
RedBGChar " nginx install Failed !!!" 2>&1|tee -a $LogFile
fi
相關文章
相關標籤/搜索