PS:写脚本的初衷,是为了方便自己更有效率的去工作,同时锻炼自己写脚本的能力,当然还希望能够给大家带来一些小的帮助,希望大家多多支持,评论评论,指出不足的地方。
一、测试环境
[[email protected] ~]# uname -r 2.6.32-279.el6.x86_64 [[email protected] ~]# cat /etc/issue CentOS release 6.3 (Final) Kernel \r on an \m [[email protected] ~]# dmidecode -s system-product-name VMware Virtual Platform
二、交互式安装Web服务脚本
注:在编译安装apche之前,必须确保你当前的系统中安装了Additional Development、Development tools、Base软件包组,其中包含了apache所需的库文件与编译工具,zlib,gcc,automake,pcre等,不然安装可能无法完成。
[[email protected] scripts]# cat auto_install_web.sh #!/bin/bash #blog http://cfwlxf.blog.51cto.com #create date of 2013-10-30 #Load user environment variable PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #source system functions library. . /etc/init.d/functions #definition software download and install directoy SOURCE_DIR=/download/source/ INSTALL_DIR=/application/ #Judge SOURCE_DIR INSTALL_DIR directoy if exits. [ ! -d ${SOURCE_DIR} ] && mkdir ${SOURCE_DIR} -p [ ! -d ${INSTALL_DIR} ] && mkdir ${INSTALL_DIR} -p && sleep 1 && printf "\033[32mDirectory $SOURCE_DIR $INSTALL_DIR by create.\033[0m" #Download httpd software printf "\033[33m ---------------------------------------------------------------------------- | Welcome to use apache auto install scripts | ----------------------------------------------------------------------------\033[0m\n" printf "\033[36mDownload Web Server of sourece package,please input staring \'y/n\': \033[0m" STR read STR #Judge User input of string if by 'y' if [ $STR = "y" ] then printf "\033[32m ---------------------------------------------------------------------------- | Apache Software List | ---------------------------------------------------------------------------- |1.Download web server software of httpd.2.4.4 versions. | |2.Download web server software of httpd.2.2.25 versions. | |3.Download web server software of httpd.2.4.6 versions. | ---------------------------------------------------------------------------- \033[0m\n\n" read -p "Please you input number'1-4', select httpd install of httpd versions: " VERSIONS case "$VERSIONS" in 1) cd ${SOURCE_DIR} wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.4.tar.gz ;; 2) cd ${SOURCE_DIR} wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.25.tar.gz ;; 3) cd ${SOURCE_DIR} wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.6.tar.gz ;; *) sleep 1 printf "\033[31mError: Please input number \'1-4\',select httpd versions.\033[0m\n" exit 0 esac else sleep 2 printf "\033[31mError: You enter the error string,Please anew input.\033[0m\n" exit 0 fi #Uncompress httpd software package cd ${SOURCE_DIR} HTTPD_SOFTWARE=$(find /download/source/ -type f | grep httpd |awk 'BEGIN{FS="/"}END{print $4}') tar -xf ${HTTPD_SOFTWARE} && sleep 2 && printf "\033[35mUncompress software by successfully.\033[0m\n" #Judge current directoy yes or on uncompress later of directory HTTPD_UNCOMPRESS=$(find ./ -maxdepth 1 -type d | grep httpd) cd ${HTTPD_UNCOMPRESS} RETURN_PWD=$(pwd | cut -d '/' -f4) ECHO_HTTPD=$(echo ${HTTPD_UNCOMPRESS}|awk 'BEGIN{FS="/"}END{print $2}') #Judge whether the current directory if uncompress after of directory if [ ${RETURN_PWD} = ${ECHO_HTTPD} ] then sleep 1 printf "\033[33mIf you need support \'--with-included-arp\',please download software after install,\'yes/no\': \033[0m" DOWNLOAD read DOWNLOAD if [ "$DOWNLOAD" = "yes" ] then wget http://mirrors.hust.edu.cn/apache/apr/apr-1.4.6.tar.gz wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.4.1.tar.gz #Judge download apr apr-util file of exits. APR_FILE=$(find ./ -maxdepth 1 -type f | sed -n '/apr-1/p' | grep -Po '(?<=./).*') APR_UTIL_FILE=$(find ./ -maxdepth 1 -type f | sed -n '/apr-util/p' | grep -Po '(?<=./).*') [ -e $APR_FIEL $APR_UTIL_FILE ] && sleep 2 && printf "\033[35mDownload File $APR_FILE,$APR_UTIL_FILE already successfully.\033[0m\n" #Uncompress apr apr-util software package. APACHE_UNCOMPRESS_FILE=$(find /download/source/ -maxdepth 1 -type d| grep httpd) APACHE_UNCOMPRESS_SRCLIB_DIR=${APACHE_UNCOMPRESS_FILE}/srclib/ tar -xf $APR_FILE -C $APACHE_UNCOMPRESS_SRCLIB_DIR && tar -xf $APR_UTIL_FILE -C $APACHE_UNCOMPRESS_SRCLIB_DIR && cd $APACHE_UNCOMPRESS_SRCLIB_DIR APR_UNCOMPRESS_FILE=$(find ./ -maxdepth 1 -type d | grep 'apr-1' | grep -Po '(?<=./).*') APR_UTIL_UNCOMPRESS_FILE=$(find ./ -maxdepth 1 -type d | grep 'apr-util' | grep -Po '(?<=./).*') mv -f $APR_UNCOMPRESS_FILE apr && mv -f $APR_UTIL_UNCOMPRESS_FILE apr-util && sleep 2 && action "Uncompress file apr aprutil is successfully." true #configure apache APACHE_UNCOMPRESS_FILE=$(find /download/source/ -maxdepth 1 -type d| grep httpd) cd $APACHE_UNCOMPRESS_FILE ./configure --prefix=/application/${ECHO_HTTPD} --enable-substitute --enable-deflate --enable-expires --enable-ssl --enable-http --enable-rewrite --enable-so --with-mpm=worker --enable-mods-shared=mos --with-z --with-included-apr else #configure apache APACHE_UNCOMPRESS_FILE=$(find /download/source/ -maxdepth 1 -type d| grep httpd) cd $APACHE_UNCOMPRESS_FILE ./configure --prefix=/application/${ECHO_HTTPD} --enable-substitute --enable-deflate --enable-expires --enable-ssl --enable-http --enable-rewrite --enable-so --with-mpm=worker --enable-mods-shared=mos --with-z fi else printf "\033[31mError: Your current of directory fault $ECHO_HTTPD.\033[0m\n" exit 0 fi #Compile apache software package RETVAL=0 make && [ $? = $RETVAL ] && make install sleep 2 && printf "\033[35mApache compile is successfully,please start apache server of 'yes/no\': \033[0m" START read START APACHE_BIN=$(find /application/ -maxdepth 1 -type d | grep httpd) [ "$START" = "yes" ] && ${APACHE_BIN}/bin/apachectl start > /dev/null 2>&1 #Judge elinsk sottware of already install. SERVER_IP=$(ifconfig eth0 | grep 'inet addr:'| sed 's/.*r:\(.*\) B.*$/\1/g') ELINKS_RPM=$(rpm -qa elinks| wc -l) [ $ELINKS_RPM = 1 ] && elinks --dump http://$SERVER_IP/index.html [ $ELINKS_RPM != 1 ] && yum install elinks && elinks --dump http://$SERVER_IP/index.html
#提示:
脚本执行过程中,会提示用户选择apache的版本,若脚本中所提供的版本都不能满足公司需求的话,那么你可以把需要的版本链接添加至脚本,略做修改即可。如果对APR不了解的朋友,可以点击网址:http://baike.baidu.com/link?url=8NJ28ufytnW1h6opu-Z3q3sVaAN82__ePE9okiCUkwsP-WB6ymhYGFmitXL5gUPg3MUaWpTVgglBnAfrRqDAdK 详细了解,脚本可能还有很多不足的地方,欢迎大家评论。
三、实例演示
#提示:
脚本中所提供的apache版本,都已测试通过,有需求的运维朋友们可以按照自己的思维,略作修改后使用。我觉得大家刚好可以借照此脚本,去编写一个LAMP,LNMP环境交互式安装的脚本,或者自动安装的脚本,动动手提升你写脚本的能力,避免繁琐的操作,有效率,也能突出你自己的实力。
转载于:https://blog.51cto.com/cfwlxf/1321064