Shell脚本实现超简洁的在Linux服务器上安装nginx、resin、java、tomcat、redis等程序

说明:

用平常的方式在Linux服务器上安装程序,需要下载安装包、进入安装包位置、给安装包文件赋予可执行权限、执行安装、设置环境变量……等等一系列复杂的操作。并且如果有关联也需要一个一个的挨着安装。耗时并且繁琐。而用shell脚本来执行这些操作,则很好的解决了这些问题!

步骤:

  1. wget http://img006.com/badusoft/centosupNEW.sh

    // 在Linux服务器上执行wget命令下载shell脚本

  2. chmod +x centosupNEW.sh

    // 给下载下来的 centosupNEW.sh 文件赋可执行权限

  3. ./centosupNEW.sh all

    //执行命令安装所有程序,还可单独安装nginx、redis、tomcat等,如想单独安装resin,则执行 ./centosupNEW.sh resin 命令即可

截图:

Shell脚本实现超简洁的在Linux服务器上安装nginx、resin、java、tomcat、redis等程序_第1张图片

Shell脚本实现超简洁的在Linux服务器上安装nginx、resin、java、tomcat、redis等程序_第2张图片

Shell脚本实现超简洁的在Linux服务器上安装nginx、resin、java、tomcat、redis等程序_第3张图片

Shell脚本实现超简洁的在Linux服务器上安装nginx、resin、java、tomcat、redis等程序_第4张图片

代码:

#!/bin/bash
#version1.0
#Wangwei
#date 2015/04/02
#System Centos6.4
get_bin_tar="/bin/tar"
get_cmd_dns="/etc/resolv.conf"
get_cmd_soft="/opt/badusoft"
get_cmd_name=`/bin/uname -a | awk '{print $1,$3}'`
get_cmd_service="/usr/local/service"
get_cmd_user=`whoami`
get_cmd_cron="/var/spool/cron/"
get_cmd_url="http://img006.com/badusoft"
echo -e "System for version:\e[32m$get_cmd_name \e[0m\n"
echo -e "Nginx for version:\e[32mTengine2.1 and nginx 1.8.0\e[0m\n"
echo -e "Java for version:\e[32mJdk 1.7\e[0m\n"
echo -e "Resin pro for version:\e[32mResin pro 3.1\e[0m\n"
echo -e "Apache for tomcat version:\e[32m tomcat 7\e[0m\n"
echo -e "Redis version:\e[32m Redis 2.8\e[0m\n"
echo -e "Firewall:\e[32moff\e[0m\n"
cat /dev/null > $get_cmd_dns
echo "nameserver 114.114.114.114" >> $get_cmd_dns
echo -e "NameServer:\e[32m114.114.114.114\e[0m\n"
sleep 10
/etc/init.d/iptables stop
/sbin/chkconfig iptables off
function package(){
        for  i  in wget  gcc gcc-c++ make gzip autoconf automake lrzsz telnet zlib zlib-devel openssl openssl-devel pcre-devel pcre patch net-snmp
        do
    yum install -y $i
    sleep 1
        done
}

function del(){
if  [ ! -d $get_cmd_soft ] && [ ! -d $get_cmd_service ]; then
    mkdir /opt/badusoft >> /dev/null 2>&1
    mkdir /usr/local/service/ >> /dev/null 2>&1
else 
        rm -rf $get_cmd_soft  $get_cmd_service
        cp -f /etc/profile.bak /etc/profile
        mkdir /opt/badusoft >> /dev/null 2>&1
        mkdir /usr/local/service/ >> /dev/null 2>&1
fi
}
function timecron(){
#date cron
if [ $get_cmd_user = "root" ];then
       echo "*/30 * * * * /usr/sbin/ntpdate 0.rhel.pool.ntp.org | /usr/sbin/hwclock -w" > $get_cmd_cron/$get_cmd_user
else
       if [ -f "$get_cmd_cron/root" ];then
               echo "*/30 * * * * /usr/sbin/ntpdate 0.rhel.pool.ntp.org | /usr/sbin/hwclock -w" >> $get_cmd_cron/$get_cmd_user 
       else
               echo "$get_cmd_user error"
               return 0
       fi
fi
}

function sysctl(){
    cd $get_cmd_soft
    if [ ! -f kernels_sysctl ] && [ ! -f limits.conf ];then
    wget $get_cmd_url/kernels_sysctl
    wget $get_cmd_url/limits.conf
    if [ -f $get_cmd_soft/kernels_sysctl ] && [ -f $get_cmd_soft/limits.conf ];then
        #cp -f /etc/sysctl.conf /etc/sysctl.conf.bak
        #cp -f /etc/security/limits.conf /etc/security/limits.conf
        cat $get_cmd_soft/kernels_sysctl > /etc/sysctl.conf
        cp -f  $get_cmd_soft/limits.conf  /etc/security/limits.conf
        /sbin/sysctl -p 
        if [ $? -eq 0 ];then
          echo -e "\e[32mcopy done\e[0m\n"
          sleep 5
        else
            echo -e "\e[31m copy error!!\e[0m\n"
            exit 0
        fi
    else
        echo -e "\e[31mfile not exits\[0m\n"
        exit 0
    fi
    else
        echo -e "\e[31mfile  exists\e[0m\n"
        exit 0
    fi 
 }
function nginx(){
    cd $get_cmd_soft
    if [ ! -f nginx-1.8.0.tar.gz ];then
       wget $get_cmd_url/nginx-1.8.0/nginx-1.8.0.tar.gz
       wget $get_cmd_url/nginx-1.8.0/nginx_upstream_check_module-master.zip
       wget $get_cmd_url/nginx-1.8.0/openssl-1.0.2c.tar.gz
       wget $get_cmd_url/nginx-1.8.0/ngx_cache_purge-2.3.tar.gz
       wget $get_cmd_url/pcre-8.12.tar.gz
       wget $get_cmd_url/zlib-1.2.5.tar.gz

       if [ $? -eq 0 ];then

        $get_bin_tar zxf nginx-1.8.0.tar.gz
        unzip nginx_upstream_check_module-master.zip
        $get_bin_tar zxf openssl-1.0.2c.tar.gz
        $get_bin_tar zxf ngx_cache_purge-2.3.tar.gz
        $get_bin_tar zxf pcre-8.12.tar.gz
        $get_bin_tar zxf zlib-1.2.5.tar.gz

        if [ $? -eq 0 ];then
           cd $get_cmd_soft/nginx-1.8.0

    patch -p1 < $get_cmd_soft/nginx_upstream_check_module-master/check_1.7.5+.patch
./configure --prefix=$get_cmd_service/nginx \
--with-pcre=$get_cmd_soft/pcre-8.12 \
--with-openssl=$get_cmd_soft/openssl-1.0.2c \
--with-zlib=$get_cmd_soft/zlib-1.2.5 \
--add-module=$get_cmd_soft/ngx_cache_purge-2.3 \
--with-http_realip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_ssl_module \
--add-module=$get_cmd_soft/nginx_upstream_check_module-master

           if [ $? -eq 0 ];then
            make && make install
            if [ $? -eq 0 ];then
               echo -e "\e[32mtengine for nginx install done\e[0m\n"
               sleep 5
            else
               echo -e "\e[31mmake && make error!!\e[0m\n"
            fi
           else
            echo -e "\e[31mconfigure error!!\e[0m\n"
            exit 0
           fi
        else
            echo -e "\e[31mtar error!!\e[0m\n"
            exit 0
        fi  
       else 
        echo -e "\e[31mtengine for nginx download error!!\e[0m\n"
        exit 0      
       fi
    else
        sleep 5
        echo -e "\e[31mtengine for nginx found\e[0m\n"
    fi
}
function java(){
        cd $get_cmd_soft
        if [ ! -f server-jre-7u80-linux-x64.tar.gz ];then
             wget $get_cmd_url/javapath
                     wget  $get_cmd_url/server-jre-7u80-linux-x64.tar.gz
                     $get_bin_tar zxf server-jre-7u80-linux-x64.tar.gz
                if [ $? -eq 0 ];then
                    mv jdk1.7.0_80 $get_cmd_service/java
                    cp -f /etc/profile /etc/profile.bak
                    cat $get_cmd_soft/javapath >> /etc/profile
                    source /etc/profile
                    if [ $? -eq 0 ];then
                        $get_cmd_service/java/bin/java -version
                        if [ $? -eq 0 ];then
                            echo -ne "\e[32mJava install done\e[0m\n"
                            sleep 5
                        else
                            echo -ne  "\e[31mJava install error!!\e[0m\n"
                            exit 0
                        fi
                    else 
                        echo -ne "\e[31mJava path error!!\e[0m\n"
                    fi
                else 
                    echo -ne "\e[31mJava tar error!!\e[0m\n"
                    exit 0
                fi

            else
                echo -ne "\e[31mJava download error\e[0m\n"
                exit 0
            fi  

}

function resin(){
    cd $get_cmd_soft
    if [ ! -f  resin-pro-3.1.12.tar.gz ];then
        wget $get_cmd_url/licenses/004444.license
            wget $get_cmd_url/resin-pro-3.1.12.tar.gz
        if [ $? -eq 0 ];then
            $get_bin_tar zxvf resin-pro-3.1.12.tar.gz
            if [ $? -eq 0 ];then
            cd $get_cmd_soft/resin-pro-3.1.12  
            ./configure --prefix=$get_cmd_service/resin \
            --with-java-home=$get_cmd_service/java \
            --enable-64bit=yes  \
            --enable-lfs  \
            --enable-jni \
            --disable-libtool-lock

            if [ $? -eq 0 ];then
                make && make install
                mkdir $get_cmd_service/resin/licenses
                cp $get_cmd_soft/004444.license $get_cmd_service/resin/licenses
                if [ $? -eq 0 ];then
                echo -ne "\e[32mResin  install done\e[0m\n"
                sleep 5
                else 
                echo -ne "\e[31mResin install error\e[0m\n"
                exit 0
                 fi
            else
                echo -ne  "\e[31mConfigure error\e[0m\n"
                exit 0
            fi
           else
               echo -ne "\e[31mtar error\e[0m\n"
               exit 0
           fi
        else
            echo -ne "\e[31mdownload error\e[0m\n"
            exit 0
        fi
    else 
        echo -ne "\e[31mDircetory or Resin  error!!\e[0m\n"
        exit 0  
    fi
}
function tomcat(){
    mkdir /usr/local/service/apache2

cd /opt/badusoft/

wget http://img006.com/badusoft/apr-1.5.2.tar.zip
wget http://img006.com/badusoft/apr-iconv-1.2.1.tar.bz2
wget http://img006.com/badusoft/apr-util-1.5.4.tar.bz2
wget http://img006.com/badusoft/tomcat-native.tar.gz
wget http://img006.com/badusoft/apache-tomcat-7.0.42.tar.gz


tar -xvf apr-1.5.2.tar.bz2
tar -xvf apr-iconv-1.2.1.tar.bz2
tar -xvf apr-util-1.5.4.tar.bz2
tar -xvf tomcat-native.tar.gz
tar -xvf apache-tomcat-7.0.42.tar.gz

mv /opt/badusoft/apache-tomcat-7.0.42  /usr/local/service/tomcat

cd /opt/badusoft/apr-1.5.2


./configure --prefix=/usr/local/service/apache2/apr

make && make install

echo "APR step over"

###############################################################

cd /opt/badushot/apr-util-1.5.4 


./configure --prefix=/usr/local/service/apache2/apr --with-apr=/usr/local/service/apache2/apr

make && make install

echo "apr-util step over"
##############################################################################


cd /opt/badusoft/tomcat-native-1.1.33-src/jni/native

./configure --prefix=/usr/local/service/apache2/apr --with-apr=/usr/local/service/apache2/apr --with-java-home=/usr/local/service/java


make && make install

echo "tomact-native step over"


echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/service/apache2/apr/lib" >> /etc/profile

source /etc/profile

cd /opt/badusoft/

wget http://img006.com/badusoft/catalina.txt

sed -i '/# limitations under the License./r catalina.txt' /usr/local/service/tomcat/bin/catalina.sh

 }
function redis(){
    cd $get_cmd_soft
    if [ ! -f redis-2.8.12.tar.gz ];then
       wget $get_cmd_url/redis-2.8.12.tar.gz
       if [ $? -eq 0 ];then
        tar zxf redis-2.8.12.tar.gz
        if [ $? -eq 0 ];then
          cd $get_cmd_soft/redis-2.8.12
          make && make install
          if [ $? -eq 0 ];then
             mkdir $get_cmd_service/redis/
             mkdir $get_cmd_service/redis/conf
             mv /usr/local/bin $get_cmd_service/redis/
             cp -f $get_cmd_soft/redis-2.8.12/redis.conf $get_cmd_service/redis/conf/
             echo -e "\e[32m Redis install done\e[0m\n"
             sleep 5
          else
              echo -e "\e[31m make && make install error!!\e[0m\n"
              exit 0
          fi
        else
                echo -e "\e[31m tar error\e[0m\n"
            exit 0
            fi

       else 
           echo -e "\e[31m Download Redis error!!\e[0m\n"
           exit 0
       fi
    else
       echo -e "\e[31m Redis file error!!\e[0m\n"
       exit 0
    fi
}           

case $1 in

package)
    package
;;

timecron)

    package
    del
    timecron
;;

sysctl)

    package
    del
    sysctl
;;


nginx)
    package
    del
    nginx
;;

resin)
    package
    del
    java
    resin

;;
tomcat)

    package
    del
    java
tomcat

;;
redis)
    package
    del
    redis
;;

all)
    package
    del 
    timecron
    sysctl
    nginx
    java
    resin
    tomcat
    redis
;;

*)
    echo -e "\e[32m package  timecron sysctl nginx resin tomcat redis or all\e[0m\n"
;;
esac

cd /opt/badusoft/

wget http://img006.com/badusoft/service.sh

chmod +x service.sh


/opt/badusoft/service.sh

你可能感兴趣的:(Linux,Shell,脚本程序)