#!/bin/bash
# Name:Centos 6.4 安装nginx1.8.1
# Date:2016-05-27
# Author:qifei@meizu.com
Home=$(cd <span style="color: #0000ff">dirname</span> $<span style="color: #800080">0</span>;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/<span style="color: #0000ff">basename</span> $<span style="color: #800080">0</span>.log #<span style="color: #0000ff">basename</span> $<span style="color: #800080">0</span>值显示当前脚本或命令的名字,$0显示会包括当前脚本或命令的路径,<span style="color: #0000ff">basename</span> $<span style="color: #800080">1</span><span style="color: #000000">和$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=<span style="color: #0000ff">ifconfig</span>|<span style="color: #0000ff">grep</span> -E <span style="color: #800000">"</span><span style="color: #800000">10\..*\..*\..*|172\..*\..*\..*</span><span style="color: #800000">"</span> |<span style="color: #0000ff">head</span> -n <span style="color: #800080">1</span> |<span style="color: #0000ff">awk</span> -F<span style="color: #800000">'</span><span style="color: #800000">[: ]+</span><span style="color: #800000">'</span> <span style="color: #800000">'</span><span style="color: #800000">{print $4}</span><span style="color: #800000">'</span><span style="color: #000000">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