一鍵安裝經常使用服務腳本 原文筆記連接請點擊文章尾部連接: 最近,入職一家新公司,開始公司編譯安裝須要輸入大量的命令,若是是配置測試環境須要耗費大量 的時間.如今無需一個一個的輸入命令,無需值守,編譯安裝優化編譯參數,提升性能,解決沒必要要的 軟件間依賴. #!/bin/bash #Version:1.0 # USER YYYY-MM-DD - ACTION # bruce fu 20017-08-08 - Created # mail 270064522@qq.com #The software list:Nginx,MySQL,PHP,Memcached,Tomcat,Java. #This script can automatically install all software on your machine. script_description(){ echo "+-----------------------------------------------------------------------+" echo "| Addons script for LNMP V1.0, Written by Baojia |" echo "+-----------------------------------------------------------------------+" echo "| The software list:Nginx,Mysql,PHP,Memcached,Tomcat,Java. |" echo "+-----------------------------------------------------------------------+" echo "| Hope to work with you to optimize this Script. |" echo "+-----------------------------------------------------------------------+" echo "| devops1995@gmail.com |" echo "+-----------------------------------------------------------------------+" } version(){ version_systemctl=`cat /etc/redhat-release |grep 6|awk '{print $3}'|awk -F '[.]' '{print $1}'` language if [ $? -eq 0 ];then if [ ${version_systemctl} -ne 6 ];then echo "對不起,當前腳本只適用於centOS6.x" exit fi else if [ ${version_systemctl} -ne 6 ];then echo "Sorry, the current script only applies to centOS6.x." exit fi fi} #Determine the language environment(肯定語言環境) 腳本會檢測目標主機的語音環境,若是目標主機運行中文環境,則腳本運行中的全部提示信息均爲中文,反之則提示信息爲英文。language(){ echo $LANG |grep -q zh #顯示當前字符集,grep過濾zh關鍵字 -q 不現實任何信息 if [ $? -eq 0 ];then #若是是zh說明是中文字符集,返回0. return 0 #r若是返回的0,說明是zh字符集,因此咱們這裏返回0. else #不然若是過濾到的不是zh,那麼上面的返回的就是1,因而就會執行else代碼塊. return 1 fi} #Define user selection menu. (定義用戶選擇菜單)menu(){ #菜單 clear #清除當前屏幕全部信息 version #判斷當前Server版本 language #調用language函數判斷當前系統字符集而選擇對應的字符提示. if [ $? -eq 0 ];then #判斷返回值是0就是中文哦,咱們上面grep -q zh 過濾的結果 echo " ##############----Menu----##############" echo "# 1. 安裝Nginx" echo "# 2. 安裝MySQL" echo "# 3. 安裝PHP" echo "# 4. 安裝Memcached" echo "# 5. 安裝Java" echo "# 6. 安裝Tomcat" echo "# 7. 安裝以上全部軟件" echo "# 8. 退出程序" echo " ########################################" else #不是中文,那麼就執行英文的哈 echo " ##############----Menu----##############" echo "# 1. Install Nginx" echo "# 2. Install MySQL" echo "# 3. Install PHP" echo "# 4. Install Memcached" echo "# 5. Install Java" echo "# 6. Install Tomcat" echo "# 7. Install all above" echo "# 8. Exit Program" echo " ########################################" fi}#Read user's choice (讀取用戶的選擇) #Read User's INPUT (讀取用戶的輸入)choice(){ #定義一個函數 language #調用language函數,判斷用戶當前環境是zh仍是其餘環境 if [ $? -eq 0 ];then #read -p 指定讀取值時的提示符 ,read 命令後面 select是變量名,讀取的數據將被自動複製給這個變量. read -p "請選擇一個菜單[1-8]:" select else read -p "Please choice a menu[1-8]:" select fi} rotate_line(){ #當執行一個比較費時的操做時,若是能在終端顯示進度指示器,則對用戶來講是比較有好的。#下面的代碼展現瞭如何達到這種效果。#函數rotate_line是一個死循環,會在終端每隔一秒交替顯示不一樣的字符(-|/)。#主程序調用rotate_line,使其在後臺執行,用$!得到其進程ID號;#接下來調用比較費時的操做,這裏簡單模擬一下,sleep 30秒,#當費時的操做執行完以後,就殺掉在後臺執行的rotate_line,操做完成。 INTERVAL=0.1 TCOUNT="0" while : #while 死循環 do TCOUNT=`expr $TCOUNT + 1` #expr 是表達式計算工具,+ - \* / % (加減乘除取餘) case $TCOUNT in #case 語句匹配一個值或一個模式,若是匹配成功,執行相匹配的命令。 "1") echo -e '-'"\b\c" #-e 激活轉義字符,-b 刪除前一個字符, -c 最後不加上換行符號. sleep $INTERVAL ;; "2") echo -e '\\'"\b\c" sleep $INTERVAL ;; "3") echo -e "|\b\c" sleep $INTERVAL ;; "4") echo -e "/\b\c" sleep $INTERVAL ;; *) TCOUNT="0";; esac done}#Install failed error messages.(安裝失敗的錯誤消息) error_install(){ #定義一個安裝錯誤函數 language #調用language函數,判斷當前字符集. if [ $? -eq 0 ];then clear echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo -e "\033[1;31m錯誤:編譯安裝[ ${1} ]失敗! \033[0m" #更改字體顏色! echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;31mERROR:Install[ ${1} ]Failed!\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi} #Couldn't use yum tool #不能使用yum工具error_yum(){ language if [ $? -eq 0 ];then clear echo echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo "錯誤:本機YUM不可用,請正確配置YUM後重試." echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo exit else clear echo echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo "ERROR:Yum is disable,please modify yum repo file then try again." echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo exit fi} #Test target system whether have yum repo. #測試當前系統是否有yum 源#Return 0 dedicate yum is enable. #返回0yum源是啓用。 #Return 1 dedicate yum is disable. #返回1yum源是禁用。test_yum(){ #set yum configure file do not display Red Hat Subscription Management info. (設置yum配置文件不顯示紅帽訂閱管理信息。)#sed -i '/enabled/s/1/0/' /etc/yum/pluginconf.d/subscription-manager.conf #關閉Redhat自帶的插件subscription-manager. yum clean all &>/dev/null #清除緩存目錄下的軟件包及舊的headers repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//') #yum repolist:查看當前的可用的yum倉庫。 若是yum源庫建立成功,會顯示其庫的大小,狀態值不爲0;#[root@mac init.d]# yum repolist 2>/dev/null |awk '/repolist:/'#repolist: 19,567 #其實就是要看看這個庫的總大小們這裏咱們會取到19567 if [ $repolist -le 0 ];then #repolist的結果若是小於等於0,那麼確定是有問題,因此咱們這裏就調用error_yum函數. error_yum fi} #This function will check depend software and install them. (此功能將檢查依賴軟件並安裝它們。)solve_depend(){ #解決依賴函數 language if [ $? -eq 0 ];then echo -en "\033[1;34m正在安裝依賴軟件包,請稍後...\033[0m" else echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m" fi case $1 in nginx) rpmlist="gcc pcre-devel openssl-devel zlib-devel make" ;; cmake) rpmlist="gcc gcc-c++ make" ;; mysql) rpmlist="ncurses-devel" ;; mhash) rpmlist="mhash mcrypt" ;; libmcrypt) rpmlist="libmcrypt-devel" ;; php) rpmlist="gcc zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel libmcrypt-devel mhash mcrypt freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel" ;; libevent) rpmlist=" libevent libevent-devel nc telnet" ;; memcached) rpmlist="gcc" ;; memcache) rpmlist="autoconf" ;; java) rpmlist="glibc.i686" esac#for 循環上述rpmlist變量裏的依賴軟件包名字 for i in $rpmlist do rpm -q $i &>/dev/null #先用rpm 裝,若是失敗則調用yum 安裝. if [ $? -ne 0 ];then #-ne //不等於 yum -y install $i &>/dev/null fi done} #Determing how to uncompress a tar file. (肯定如何解壓縮tar文件)untar(){ type=$(file $1 |awk '{print $2}') #使用file 命令判斷文件的格式,若是是gzip格式那麼就執行如下. if [ "$type" == "gzip" ];then language #引用判斷當前字符集函數 if [ $? -eq 0 ];then echo -e "\033[1;34m正在解壓$1,請稍後...\033[0m" else echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m" fi tar -xzf $1 elif [ "$type" == "bzip2" ];then #若是是bzip2 則使用如下代碼塊進行解壓 language if [ $0 -eq 0 ];then echo -e "\033[1;34m正在解壓$1,請稍後...\033[0m" else echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m" fi tar -xjf $1 else #若是是其餘的壓縮格式則報錯. language if [ $? -eq 0 ];then clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34m錯誤:未知的壓縮包類型.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34mERROR:Unknow compress file type.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi fi } #Display a begin mesages (顯示開始訊息)begin(){ #開始函數 language if [ $? -eq 0 ];then clear echo -e "\033[1;36m----------------------------------\033[0m" echo -e "\033[1;32m\t如今開始安裝${1}!\033[0m" echo -e "\033[1;36m----------------------------------\033[0m" else clear echo -e "\033[1;36m-----------------------------------\033[0m" echo -e "\033[1;32m\tInstall ${1} Now!\033[0m" echo -e "\033[1;36m-----------------------------------\033[0m" fi} #If not found the software package, this script will be exit. (若是沒有找到軟件包,這個腳本將退出。)error_nofile(){ language if [ $? -eq 0 ];then clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34m錯誤:未找到[ ${1} ]軟件包,請下載軟件包至${install_package_dir}目錄.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34mERROR:Not found [ ${1} ] package in ${install_package_dir} directory, please download it.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi} #Display a hint for configure (顯示配置提示)configure_info(){ language if [ $? -eq 0 ];then echo -e "\033[1;34m正在檢測編譯環境並生成編譯文件,請稍後...\033[0m" else echo -e "\033[1;34mCheck system environment and configure,Please wait a moment...\033[0m" fi} #Display a hint for make and make install (顯示make和make install的提示) make_info(){ language if [ $? -eq 0 ];then echo -e "\033[1;34m正在編譯安裝軟件,請稍後...\033[0m" else echo -e "\033[1;34mComplie and install software,Please wait a moment...\033[0m" fi} #Install cmake to complie MySQL (安裝cmake來編譯mysql)install_cmake(){ test_yum #測試yum是否可用 solve_depend cmake #調用solve_depend解決依賴的函數並賦值 cmake 給他,因而就是自動安裝cmake的依賴. cd ${install_package_dir} if [ -f ${cmake_version} ];then #if [ -f ] 判斷是否爲文件,是則爲真.這裏的版本號和後綴是咱們前面定義函數給的哦. untar ${cmake_version} #調用untar函數來解壓. cmake_dir=`ls -d */|grep cmake` #找到cmake目錄 cd ${cmake_dir} #進入解壓後的cmake目錄 configure_info #調用顯示配置信息的函數configure_info#主程序調用rotate_line,使其在後臺執行,用$!得到其進程ID號;#接下來調用比較費時的操做,當費時的操做執行完以後,#就kill -9 PID殺掉在後臺執行的rotate_line,操做完成。 rotate_line & #只要在命令的尾部加上符號&,啓動的進程就會成爲"後臺任務"。若是要讓正在運行的"前臺任務"變爲"後臺任務",能夠先按ctrl + z,而後執行bg命令(讓最近一個暫停的"後臺任務"繼續執行)。 disown $! # $!表示Shell最後運行的後臺Process的PID ./bootstrap --prefix=/usr/local/cmake &>/dev/null result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/cmake目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/cmake.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &> /dev/null && make install &>/dev/null result=$? kill -9 $! cd .. if [ $result -ne 0 ];then error_install cmake fi fi else error_nofile cmake fi}install_mhash(){ #安裝MHASH函數#mhash是基於離散數學原理不可逆向的PHP加密方式擴展庫,其在默認狀況下不會開啓。#mhash能夠用於建立校驗數值、消息摘要、消息認證碼,#以及無需原文的關鍵信息保存(如密碼)等。它爲PHP提供了多種散列算法,如MD五、SHA一、GOST等。#能夠經過MHASH_hashname()查看其支持的算法有哪些。# 不過,須要注意的是:# ?該擴展不能提供最新的散列算法。# ?該擴展結果原則上運算不可逆。#同理,咱們選擇更簡單的yum安裝方法來安裝mhash。安裝PHP須要的相關包mhash的命令以下: solve_depend mhash #安裝軟件的依賴函數 if [ $? -ne 0 ];then error_install mhash fi} install_libmcrypt(){ #這是一個使用動態加載的模塊化的libmcrypt。libmcrypt對於在程序運行時添加/移除算法是有用的。 #limbcrypt-nm目前再也不被官方支持,#其軟件地址爲http://mcrypt.hellug.gr/lib/,編譯PHP的過程當中,libmcrypt庫不是必需要安裝的包。#不少同窗採用的是很複雜的編譯安裝libmcrypt的方法,這裏咱們選擇更簡單的yum安裝方法。 #在CentOS默認的yum源裏沒有libmcrypt-devel,所以須要事先配置epel第三方yum源,具體命令以下:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo solve_depend libmcryptif [ $? -ne 0 ];then errot_nofile libmcryptfi} #Install libevent 安裝libeventinstall_libevent(){ #nc 和telnet 是客戶端工具,libevent是事件處理機制。 solve_depend libevent if [ $? -ne 0 ];then error_nofile libevent fi} #Install Nginx 安裝nginx install_nginx(){ begin nginx test_yum solve_depend nginx #查看是否有nginx用戶 grep -q nginx /etc/passwd#沒有則建立 nginx用戶 if [ $? -ne 0 ];then useradd nginx -s /sbin/nologin -M #-s指定用戶登陸SHELL,這裏是禁止用戶登陸,-M:不建立家目錄。 ficd ${install_package_dir} if [ -f ${nginx_version} ];then untar ${nginx_version} nginx_dir=`ls -d */|grep nginx` #找到nginx目錄 cd $nginx_dir configure_info rotate_line & disown $! ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module >/dev/null#編譯Nginx軟件時,可使用./configure--help 查看相關參數幫助。#下面是本次編譯時指定的參數及簡單說明:#--prefix=PATH set installation prefix#設置安裝路徑#--user=USER set non-privileged user for worker processes #進程用戶權限#--with-http_stub_status_module enable ngx_http_stub_status_module#激活狀態信息。#--with-http_ssl_module enable ngx_http_ssl_module #激活ssl功能#提示:Nginx的大部分模塊功能都會默認編譯到軟件中,不須要單獨指定編譯參數。 result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/nginx目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/nginx.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &>/dev/null && make install &>/dev/null result=$? kill -9 $! cd .. if [ $result -ne 0 ];then error_install nginx fi fi else error_nofile Nginx fi} #Install MySQL 安裝MySQL install_mysql(){ begin mysql #提示正在安裝MySQL test_yum #測試yum 是否能夠 install_cmake #安裝cmake solve_depend mysql #安裝MySQL依賴 #判斷mysql用戶是否存在不存在即建立 grep -q mysql /etc/passwd #判斷MySQL用戶是否存在,不存在即建立MySQL用戶. if [ $? -ne 0 ];then useradd -s /sbin/nologin mysql -M ficd ${install_package_dir} if [ -f ${mysql_version} ];then untar ${mysql_version} mysql_dir=`ls -d */|grep mysql` #找到mysql目錄 cd ${mysql_dir} configure_info rotate_line & disown $! /usr/local/cmake/bin/cmake . &>/dev/null result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/mysql目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/mysql.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &> /dev/null && make install &>/dev/null result=$? kill -9 $! if [ $result -ne 0 ];then error_install MySQL fi cd .. fi language if [ $? -eq 0 ];then echo -e "\033[1;34m正在初始化數據庫...\033[0m" else echo -e "\033[1;34mInitialization database...\033[0m" fi#初始化MySQL /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ &>/dev/null chown -R mysql.mysql /usr/local/mysql chown -R mysql /usr/local/mysql/data#生成簡單的/etc/init.d/mysqld 就能夠啓動和關閉MySQL /bin/cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /bin/cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf #ldconfig 命令的用途主要是搜索/etc/ld.so.cpnf(動態庫配置文件) or /lib or /usr/lib內所列的目錄下,#搜索出克共享的動態連接庫(格式如lib*.so*),進而建立出動態裝入程序(ld.so)所需的鏈接和緩存文件.#緩存文件默認爲/etc/ld.so.cache,此文件保存已排好序的動態連接庫名字列表,爲了讓動態連接庫爲系統所共享,#需運行動態連接庫的管理命令ldconfig,此執行程序存放在/sbin目錄下. echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf #ldconfig一般在系統啓動時運行,而當用戶安裝了一個新的動態連接庫時,就須要手工運行安裝這個命令. ldconfig #把mysql的路徑加入環境變量cat <<EOF>> /etc/profile PATH=\$PATH:/usr/local/mysql/bin/export PATH EOF#從新加載環境變量./etc/profile#設置開機自啓動chkconfig --add mysqld chkconfig --list mysqld else error_nofile mysql fi }#Install PHP 安裝PHP install_php(){ begin php test_yum install_mhash install_libmcrypt solve_depend php#安裝yum沒法安裝的libiconv庫 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar zxf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv rotate_line & disown $! make &>/dev/null && make install &>/dev/null return=$? language if [ $? -eq 0 ];then if [ $return -ne 0 ];then echo "libiconv庫編譯失敗." else echo "libiconv庫編譯成功." fi else if [ $return -ne 0 ];then echo "Libiconv library failed to compile." else echo "The libiconv library was compiled successfully." fi fi result=$? kill -9 $! cd ../#開始解壓編譯安裝PHPcd ${install_package_dir} if [ -f ${php_version} ];then untar ${php_version} php_dir=`ls -d */|grep php` #找到php目錄 cd ${php_dir} configure_info rotate_line & disown $! ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --enable-fpm --enable-mbstring --with-mcrypt --with-mhash --with-config-file-path=/usr/local/php5/etc --with-mysqli=/usr/local/mysql/bin/mysql_config &>/dev/null#PHP FastCGI模式的設置說明:若是是PHP5.3及以上版本,所使用的編譯參數爲--enable-fpm,若是是PHP5.2版本,編譯參數則爲--enable-fastcgi--enable-fpm--enable-force-cgi。#?--enable-fpm:表示激活PHP-FPM方式服務,即以FastCGIF方式運行PHP服務。 result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/php5目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/php5.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &> /dev/null && make install &>/dev/null result=$? kill -9 $! if [ $result -ne 0 ];then error_install php fi /bin/cp -f php.ini-production /usr/local/php5/etc/php.ini /bin/cp -f /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf cd .. fi else error_nofile php fi} #Install memcached install_memcached(){ begin memcached test_yum install_libevent solve_depend memcached cd ${install_package_dir} if [ -f ${memcached_version} ];then untar ${memcached_version} memcached_dir=`ls -d */|grep memcached` #找到memcached目錄 cd ${memcached_dir} configure_info rotate_line & disown $! ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null result=$? kill -9 $! if [ $result -ne 0 ];then error_install memcached fi cd .. else error_nofile memcached fi} #Install JRE 安裝JREinstall_java(){ begin JAVA solve_depend java cd ${install_package_dir} if [ -f ${jdk_version} ];then untar ${jdk_version} jdk_dir=`ls -d */|grep jdk` #找到jdk目錄 if [ ! -e /usr/local/jdk ];then mv ${jdk_dir} /usr/local/jdk fi cat <<EOF>> /etc/profile export JRE_HOME=/usr/local/jdkexport JAVA_BIN=/usr/local/jdk/binexport PATH=/usr/local/jdk/bin:\$PATH export CLASSPATH=.:/usr/local/jdk/lib/dt.jar:\$JRE_HOME/lib/tools.jar export JRE_HOME JAVA_BIN PATH CLASSPATH EOF source /etc/profile else error_nofile JAVA fi} #Install Tomcat 安裝Tomcatinstall_tomcat(){ begin Tomcat cd ${install_package_dir} if [ -f ${tomcat_version} ];then untar ${tomcat_version} tomcat_dir=`ls -d */|grep tomcat` #找到tomcat目錄 if [ ! -e /usr/local/tomcat ];then mv ${tomcat_dir} /usr/local/tomcat fi if [ -f session.tar.gz ];then tar -xzf session.tar.gz &> /dev/null cp session/* /usr/local/tomcat/lib/ fi else error_nofile tomcat fi} while : do menu choicecase $select in 1) install_nginx 2>&1 | tee /root/nginx-install.log ;; 2) install_mysql 2>&1 | tee /root/mysql-install.log ;; 3) install_php 2>&1 | tee /root/php-install.log ;; 4) install_memcached 2>&1 | tee /root/memcached-install.log ;; 5) install_java 2>&1 | tee /root/java-install.log ;; 6) install_tomcat 2>&1 | tee /root/tomcat-install.log ;; 7) install_nginx 2>&1 | tee /root/nginx-install.log install_mysql 2>&1 | tee /root/mysql-install.log install_php 2>&1 | tee /root/php-install.log install_memcached 2>&1 | tee /root/memcached-install.log install_java 2>&1 | tee /root/java-install.log install_tomcat 2>&1 | tee /root/tomcat-install.log ;; 8|exit|quit) exit ;; *) echo -e "\033[1;32mEnter your chice (1, 2, 3, 4, 5, 6, 7 or 8:exit)\033[0m" sleep 3esacdone Original script(原始腳本) #!/bin/bash #Data:2017-04-18#Version:1.0#Author:Baojia(devops1995@gmail.com) #The software list:Nginx,MySQL,PHP,Memcached,Tomcat,Java. #This script can automatically install all software on your machine.script_description(){echo "+-----------------------------------------------------------------------+"echo "| Addons script for LNMP V1.0, Written by Baojia |"echo "+-----------------------------------------------------------------------+"echo "| The software list:Nginx,Mysql,PHP,Memcached,Tomcat,Java. |"echo "+-----------------------------------------------------------------------+"echo "| Hope to work with you to optimize this Script. |"echo "+-----------------------------------------------------------------------+"echo "| devops1995@gmail.com |"echo "+-----------------------------------------------------------------------+"} install_package_dir=/tmp/ nginx_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /nginx/'{print $9}'` mysql_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /mysql/'{print $9}'` cmake_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /cmake/'{print $9}'` php_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /php/'{print $9}'` memcached_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /memcached/'{print $9}'` jdk_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /jdk/'{print $9}'` tomcat_version=`ls -l ${install_package_dir}|grep -v '^d'|awk /tomcat/'{print $9}'` version(){ version_systemctl=`cat /etc/redhat-release |grep 6|awk '{print $3}'|awk -F '[.]' '{print $1}'` language if [ $? -eq 0 ];then if [ ${version_systemctl} -ne 6 ];then echo "對不起,當前腳本只適用於centOS6.x" exit fi else if [ ${version_systemctl} -ne 6 ];then echo "Sorry, the current script only applies to centOS6.x." exit fi fi} #Determine the language environmentlanguage(){ echo $LANG |grep -q zh if [ $? -eq 0 ];then return 0 else return 1 fi} # Check if user is rootcheck_user(){if [ $(id -u) != "0" ]; then echo "Error: You must be root to run this script, please use root to install lnmp" exit 1fi}#Define user selection Menu.menu(){ check_user clear version script_description language if [ $? -eq 0 ];then echo " ##############----Menu----##############" echo "# 1. 安裝Nginx" echo "# 2. 安裝MySQL" echo "# 3. 安裝PHP" echo "# 4. 安裝Memcached" echo "# 5. 安裝Java" echo "# 6. 安裝Tomcat" echo "# 7. 安裝以上全部軟件" echo "# 8. 退出程序" echo " ########################################" else echo " ##############----Menu----##############" echo "# 1. Install Nginx" echo "# 2. Install MySQL" echo "# 3. Install PHP" echo "# 4. Install Memcached" echo "# 5. Install Java" echo "# 6. Install Tomcat" echo "# 7. Install all above" echo "# 8. Exit Program" echo " ########################################" fi}#Read user's choice #Read User's INPUT choice(){ language if [ $? -eq 0 ];then read -p "請選擇一個菜單[1-8]:" select else read -p "Please choice a menu[1-8]:" select fi} rotate_line(){ INTERVAL=0.1 TCOUNT="0" while : do TCOUNT=`expr $TCOUNT + 1` case $TCOUNT in "1") echo -e '-'"\b\c" sleep $INTERVAL ;; "2") echo -e '\\'"\b\c" sleep $INTERVAL ;; "3") echo -e "|\b\c" sleep $INTERVAL ;; "4") echo -e "/\b\c" sleep $INTERVAL ;; *) TCOUNT="0";; esac done}#Install failed error messages.error_install(){ language if [ $? -eq 0 ];then clear echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo -e "\033[1;31m錯誤:編譯安裝[ ${1} ]失敗! \033[0m" echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;31mERROR:Install[ ${1} ]Failed!\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi} #Couldn't use yum toolerror_yum(){ language if [ $? -eq 0 ];then clear echo echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo "錯誤:本機YUM不可用,請正確配置YUM後重試." echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo exit else clear echo echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo "ERROR:Yum is disable,please modify yum repo file then try again." echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' echo exit fi} test_yum(){ yum clean all &>/dev/null repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//') if [ $repolist -le 0 ];then error_yum fi} #This function will check depend software and install them.solve_depend(){ language if [ $? -eq 0 ];then echo -en "\033[1;34m正在安裝依賴軟件包,請稍後...\033[0m" else echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m" fi case $1 in nginx) rpmlist="gcc pcre-devel openssl-devel zlib-devel make" ;; cmake) rpmlist="gcc gcc-c++ cmake" ;; mysql) rpmlist="ncurses-devel" ;; mhash) rpmlist="mhash mcrypt" ;; libmcrypt) rpmlist="libmcrypt-devel" ;; php) rpmlist="gcc zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel libmcrypt-devel mhash mcrypt freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel" ;; libevent) rpmlist=" libevent libevent-devel nc telnet" ;; memcached) rpmlist="gcc" ;; memcache) rpmlist="autoconf" ;; java) rpmlist="glibc.i686" esac for i in $rpmlist do rpm -q $i &>/dev/null if [ $? -ne 0 ];then yum -y install $i &>/dev/null fi done} #Determing how to uncompress a tar file.untar(){ type=$(file $1 |awk '{print $2}') if [ "$type" == "gzip" ];then language if [ $? -eq 0 ];then echo -e "\033[1;34m正在解壓$1,請稍後...\033[0m" else echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m" fi tar -xzf $1 elif [ "$type" == "bzip2" ];then language if [ $0 -eq 0 ];then echo -e "\033[1;34m正在解壓$1,請稍後...\033[0m" else echo -e "\033[1;34mUncompress $1,Please wait a moment...\033[0m" fi tar -xjf $1 else language if [ $? -eq 0 ];then clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34m錯誤:未知的壓縮包類型.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34mERROR:Unknow compress file type.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi fi } #Display a begin mesagesbegin(){ language if [ $? -eq 0 ];then clear echo -e "\033[1;36m----------------------------------\033[0m" echo -e "\033[1;32m\t如今開始安裝${1}!\033[0m" echo -e "\033[1;36m----------------------------------\033[0m" else clear echo -e "\033[1;36m-----------------------------------\033[0m" echo -e "\033[1;32m\tInstall ${1} Now!\033[0m" echo -e "\033[1;36m-----------------------------------\033[0m" fi} #If not found the software package, this script will be exit.error_nofile(){ language if [ $? -eq 0 ];then clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34m錯誤:未找到[ ${1} ]軟件包,請下載軟件包至${install_package_dir}目錄.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34mERROR:Not found [ ${1} ] package in ${install_package_dir} directory, please download it.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit fi} #Display a hint for configureconfigure_info(){ language if [ $? -eq 0 ];then echo -e "\033[1;34m正在檢測編譯環境並生成編譯文件,請稍後...\033[0m" else echo -e "\033[1;34mCheck system environment and configure,Please wait a moment...\033[0m" fi} #Display a hint for make and make installmake_info(){ language if [ $? -eq 0 ];then echo -e "\033[1;34m正在編譯安裝軟件,請稍後...\033[0m" else echo -e "\033[1;34mComplie and install software,Please wait a moment...\033[0m" fi} #Install cmake to complie MySQLinstall_cmake(){ test_yum solve_depend cmake if [ $? -ne 0 ];then error_nofile cmake fi}install_mhash(){ solve_depend mhash if [ $? -ne 0 ];then error_install mhash fi} install_libmcrypt(){ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo solve_depend libmcryptif [ $? -ne 0 ];then errot_nofile libmcryptfi} #Install libeventinstall_libevent(){ solve_depend libevent if [ $? -ne 0 ];then error_nofile libevent fi} #Install Nginxinstall_nginx(){ begin nginx test_yum solve_depend nginx grep -q nginx /etc/passwd if [ $? -ne 0 ];then useradd nginx -s /sbin/nologin -M ficd ${install_package_dir} if [ -f ${nginx_version} ];then untar ${nginx_version} nginx_dir=`ls -d */|grep nginx` cd $nginx_dir configure_info rotate_line & disown $! ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module >/dev/null result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/nginx目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/nginx.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &>/dev/null && make install &>/dev/null result=$? kill -9 $! cd .. if [ $result -ne 0 ];then error_install nginx fi fi else error_nofile Nginx fi} #Install MySQLinstall_mysql(){ begin mysql test_yum install_cmake solve_depend mysql grep -q mysql /etc/passwd if [ $? -ne 0 ];then useradd -s /sbin/nologin mysql -M ficd ${install_package_dir} if [ -f ${mysql_version} ];then untar ${mysql_version} mysql_dir=`ls -d */|grep mysql`# cd ${mysql_dir} configure_info make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/mysql目錄.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/mysql.\033[0m" fi language if [ $? -eq 0 ];then echo -e "\033[1;34m正在初始化數據庫...\033[0m" else echo -e "\033[1;34mInitialization database...\033[0m" fi mv ${mysql_dir} /usr/local/${mysql_dir} ln -s /usr/local/${mysql_dir} /usr/local/mysql /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ &>/dev/null chown -R mysql.mysql /usr/local/mysql chown -R mysql /usr/local/mysql/data /bin/cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /bin/cp -f /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf ldconfig cat <<EOF>> /etc/profile PATH=\$PATH:/usr/local/mysql/bin/export PATH EOF . /etc/profile chkconfig --add mysqld chkconfig --list mysqld else error_nofile mysql fi }#Install PHPinstall_php(){ begin php test_yum install_mhash install_libmcrypt solve_depend php#install libiconv wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar zxf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv rotate_line & disown $! make &>/dev/null && make install &>/dev/null return=$? language if [ $? -eq 0 ];then if [ $return -ne 0 ];then echo "libiconv庫編譯失敗." else echo "libiconv庫編譯成功." fi else if [ $return -ne 0 ];then echo "Libiconv library failed to compile." else echo "The libiconv library was compiled successfully." fi fi result=$? kill -9 $! cd ../cd ${install_package_dir} if [ -f ${php_version} ];then untar ${php_version} php_dir=`ls -d */|grep php` cd ${php_dir} configure_info rotate_line & disown $! ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --enable-fpm --enable-mbstring --with-mcrypt --with-mhash --with-config-file-path=/usr/local/php5/etc --with-mysqli=/usr/local/mysql/bin/mysql_config &>/dev/null result=$? kill -9 $! make_info language if [ $? -eq 0 ];then echo -e "\033[1;32m正常狀況下軟件被安裝在/usr/local/php.5.\033[0m" else echo -e "\033[1;32mSoftware will be installed to /usr/local/php5.\033[0m" fi if [ $result -eq 0 ];then rotate_line & disown $! make &> /dev/null && make install &>/dev/null result=$? kill -9 $! if [ $result -ne 0 ];then error_install php fi /bin/cp -f php.ini-production /usr/local/php5/etc/php.ini /bin/cp -f /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf cd .. fi else error_nofile php fi} #Install memcached install_memcached(){ begin memcached test_yum install_libevent solve_depend memcached cd ${install_package_dir} if [ -f ${memcached_version} ];then untar ${memcached_version} memcached_dir=`ls -d */|grep memcached` cd ${memcached_dir} configure_info rotate_line & disown $! ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null result=$? kill -9 $! if [ $result -ne 0 ];then error_install memcached fi cd .. else error_nofile memcached fi} #Install JREinstall_java(){ begin JAVA solve_depend java cd ${install_package_dir} if [ -f ${jdk_version} ];then untar ${jdk_version} jdk_dir=`ls -d */|grep jdk` if [ ! -e /usr/local/jdk ];then mv ${jdk_dir} /usr/local/jdk fi cat <<EOF>> /etc/profile export JRE_HOME=/usr/local/jdkexport JAVA_BIN=/usr/local/jdk/binexport PATH=/usr/local/jdk/bin:\$PATH export CLASSPATH=.:/usr/local/jdk/lib/dt.jar:\$JRE_HOME/lib/tools.jar export JRE_HOME JAVA_BIN PATH CLASSPATH EOF . /etc/profile else error_nofile JAVA fi} #Install Tomcatinstall_tomcat(){ begin Tomcat cd ${install_package_dir} if [ -f ${tomcat_version} ];then untar ${tomcat_version} tomcat_dir=`ls -d */|grep tomcat` if [ ! -e /usr/local/tomcat ];then mv ${tomcat_dir} /usr/local/tomcat fi if [ -f session.tar.gz ];then tar -xzf session.tar.gz &> /dev/null cp session/* /usr/local/tomcat/lib/ fi else error_nofile tomcat fi} while : do menu choicecase $select in 1) install_nginx 2>&1 | tee /root/nginx-install.log ;; 2) install_mysql 2>&1 | tee /root/mysql-install.log ;; 3) install_php 2>&1 | tee /root/php-install.log ;; 4) install_memcached 2>&1 | tee /root/memcached-install.log ;; 5) install_java 2>&1 | tee /root/java-install.log ;; 6) install_tomcat 2>&1 | tee /root/tomcat-install.log ;; 7) install_nginx 2>&1 | tee /root/nginx-install.log install_mysql 2>&1 | tee /root/mysql-install.log install_php 2>&1 | tee /root/php-install.log install_memcached 2>&1 | tee /root/memcached-install.log install_java 2>&1 | tee /root/java-install.log install_tomcat 2>&1 | tee /root/tomcat-install.log ;; 8|exit|quit) exit ;; *) echo -e "\033[1;32mEnter your chice (1, 2, 3, 4, 5, 6, 7 or 8:exit)\033[0m" sleep 3esacdone
原文連接php