Reproduced original address: http://manual.blog.51cto.com/3300438/1722282
Recently took time to write a copy of the LNMP deployment script, the use of the source code to install the required software, the source software package is easy to access the network, where only the script content, you can download the corresponding software on the network in the script of the current directory can be placed, If the actual downloaded package differs from the software version number and compression format that the script invokes, you can modify the variable definition at the beginning of the script.
The script detects the target host's voice environment, and if the target host is running a Chinese environment, all the prompts in the script run are in Chinese, whereas the message is in English. The script will call Yum to install the corresponding software when installing the dependent packages of the relevant software, before running the script to confirm that Yum is available, otherwise the script detects that no Yum source is available and will exit directly.
Script test Environment: RHEL6.5, hope can become a reference for you to write automatic deployment Lnmp and related scripts.
The script reads as follows:
#!/bin/bash#data:2015-12-09#version:1.0#author:jacob ([email protected]) #The software List:Nginx,MySQL,PHP, Memcached,memcache for Php,tomcat,java. #This script can automatically install all software on your machine.#@ The Adventures of Tintin blog (Manua l.blog.51cto.com) #Define default variables, you can modify the value.nginx_version=nginx-1.8.0mysql_version= Mysql-5.6.25cmake_version=cmake-2.8.10.2mhash_version=mhash-0.9.9.9libmcrypt_version=libmcrypt-2.5.8php_ version=php-5.4.24libevent_vertion=libevent-2.0.21-stablememcached_version=memcached-1.4.24memcache_version= Memcache-2.2.5format1=tar.gzformat2=tgz #Determine the Language environment#@ Tintin blog (manual.blog.51cto.com) language () {echo $LANG |grep-q zh if [$?-eq 0];then return 0 else return 1 fi} #Define a user portal menu.#@ Tintin blog (manual.blo g.51cto.com) menu () {Clear language if [$?-eq 0];then echo "##############----menu----##############" echo "# 1. Install Nginx "echo" # 2. Install MySQL "echo" # 3. Install PHP "echo" # 4. Install memcached "echo"# 5. Install memcache for PHP "echo" # 6. Install Java "echo" # 7. Install Tomcat "echo" # 8. Install all of the above Software "echo" # 9. Exit program "echo" ######################################## "Else echo" ##############----Menu----############## "EC Ho "# 1. Install Nginx "echo" # 2. Install MySQL "echo" # 3. Install PHP "echo" # 4. Install Memcached "echo" # 5. Install memcache for PHP "echo" # 6. Install Java "echo" # 7. Install Tomcat "echo" # 8. Install all Above "echo" # 9. Exit program "echo" ######################################## "fi} #Read user ' s choice#@ Tintin blog (manual.blog.51cto.com) # Read User ' s Inputchoice () {language if [$?-eq 0];then read-p "Select a menu [1-9]:" Select Else Read-p "Please choice a me NU[1-9]: "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.#@ Tintin Adventures blog (manual.blog.51cto.com) Error_install () {language if [$?-eq 0];then Cl Ear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Echo-e "\033[1;34m Error: Compilation installation [${1}] Failed!\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Exit Else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Echo-e "\033[1;34merror:install[${1}]failed!\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Exit fi} #Couldn ' t use Yum tool Error_yum () {language if [$?-eq 0];then Clear echo echo "!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!" echo "Error: The native yum is not available, please configure Yum correctly and try again." 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 has yum repo. #Return 0 Dedicate Yum is enable. #Return 1 Dedicate Yum is disable . #@ Tintin Blog (manual.blog.51cto.com) Test_yum () {#set Yum Configure file do not display Red Hat Subscription Management info. SE D-i '/enabled/s/1/0/'/etc/yum/pluginconf.d/subscription-manager.conf yum clean all &>/dev/null repolist=$ (yum Repolist 2>/dev/null |awk '/repolist:/{print $} ' |sed ' s/,//') if [$repolist-le 0];then error_yum fi} #This function Would check depend software and install Them.solve_depend () {language if [$?-eq 0];then echo-en "\033[1;34m is installing a dependent package, Please later ... \033[0m "Else echo-e" \033[1;34minstalling dependent software,please wait a moment...\033[0m "fi case $ in ngin x) rpmlist= "gcc pcre-devel openssl-devel zlib-devel make";; CMake) rpmlist= "gcc gcc-c++ make";; MySQL) rpmlist= "ncurses-devel";; Mhash) rpmlist= "GCC";; Libmcrypt) rpmlist= "GCC";; PHP) rpmlist= "gcc libxml2-devel";; Libevent) rpmlist="GCC";; 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 I Nstall $i &>/dev/null fi do} #Determing how to uncompress a tar file.#@ Tintin blog (manual.blog.51cto.com) Untar () {type=$ (file $ |awk ' {print $} ') if ["$type" = = "gzip"];then language if [$?-eq 0];then echo-e "\033[1 34M is extracting $, please later ... \033[0m "Else echo-e" \033[1;34muncompress $1,please Wait a moment...\033[0m "fi tar-xzf $ elif [ "$type" = = "Bzip2"];then language if [$0-eq 0];then echo-e "\033[1;34m is extracting $ $, please later ... \033[0m" Else echo-e "\03 3[1;34muncompress $1,please Wait a moment...\033[0m "fi tar-xjf $ else language if [$?-eq 0];then Clear Echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Echo-e "\033[1;34m Error: Unknown compression package type. \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 now starts to install ${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 would be exit.error_nofile () {language if [$?-eq 0];then Clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Echo-e "\033[1;34m error: [${1}] Package not found, please download the package to the current directory. \033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!" Exit Else Clear ECho "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" Echo-e "\033[1;34merror:not found [${1}] in the current 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 is detecting the compilation environment and generating the compiled files, please later ... \033[0m" Else echo-e "\033[1;34mch Eck system environment and configure,please wait a moment...\033[0m "fi} #Display a hint for make and make I nstall#@ Tintin blog (manual.blog.51cto.com) Make_info () {language if [$?-eq 0];then Echo -E "\033[1;34m is compiling the installation software, please later ... \033[0m" Else echo-e "\033[1;34mcomplie and install S Oftware,please Wait a moment...\033[0m "fi} #Install CMake to Complie Mysqlinstall_cmake () {Test_yum SolvE_depend CMake If [-f ${CMAKE_VERSION}.${FORMAT1}];then Untar ${CMAKE_VERSION}.${FORMAT1} CD ${cmake_version} configure_info rotate_line & Disown $! ./bootstrap--prefix=/usr/local/cmake &>/dev/null result=$? Kill-9 $! Make_info language if [$?-eq 0];then echo-e "\033[1;32m under normal circumstances the software is installed In the/usr/local/cmake directory. \033[0m "Else Echo-e" \033[1;32msoftware'll 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 ER Ror_nofile CMake Fi}install_mhash () {Test_Yum Solve_depend Mhash If [-f ${MHASH_VERSION}.${FORMAT1}];then Untar ${mhash_version}.${format1} CD ${mhash_version} Configure_info Rotate_line & Disown $! ./configure &>/dev/null result=$? Kill-9 $! If [$result-eq 0];then rotate_line & Disown $! Make &>/dev/null make install &>/dev/null result=$? Kill-9 $! fi if [$result-ne 0];then error_install mhash fi CD. if [!-f/usr/lib/libmhash.so];then ln-s/usr/local/lib/libmhash.so/usr/lib/fi ldconfig Else error_nofile Mhash Fi}install_libmcrypt () {solve_depend libmcrypt if [-f ${LIBMCRYPT_VERSION}.${FORMAT1}];then Untar ${libmcrypt_version }.${FORMAT1} CD ${libmcrypt_version} configure_info rotate_line & Disown $! ./configure &>/dev/null result=$? Kill-9 $! 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 Libmcrypt fi fi cd. if [!-f/usr/lib/libmcrypt.so];then ln-s/usr/local/lib/libmcrypt.so/usr/lib/fi ldconfig Else error_nofile Lib MCrypt fi} #Install libeventinstall_libevent () {solve_depend libevent if [-f ${LIBEVENT_VERTION}.${FORMAT1}];then Untar ${LIBEVENT_VERTION}.${FORMAT1} CD ${libevent_vertion} configure_info./configure &>/dev/null Make &>/de V/null && make install &>/dev/null CD. if [!-f/usr/lib/libevent.so];then ln-s/usr/local/lib/libevent.so/usr/lib/fi ldconfig Else error_nofile Libeve NT fi} #Install nginx#@ Tintin blog (manual.blog.51cto.com) Install_nginx () {begin Nginx Test_yum solve_depend Nginx grep-q NGINX/ETC/PASSWD if [$?-ne 0];then useradd-s/sbin/nologin nginx fi if [-f ${NGINX_VERSION}.${FORMAT1}];then u Ntar ${NGINX_VERSION}.${FORMAT1} CD $nginx _version 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 under normal circumstances the software is installed in the/usr/local/nginx directory. \033[0m" Else Echo-e "\03 3[1;32msoftware'll 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 () {Begi n MySQL test_yum install_cmake solve_depend mysql grep-q mysql/etc/passwd if [$?-ne 0];then useradd-s/sbin/nologi n MySQL fi if [-f ${MYSQL_VERSION}.${FORMAT1}];then Untar ${mysql_version}.${format1} CD ${mysql_version} configure_i NFO 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 under normal circumstances the software is installed in/usr/lOcal/mysql directory. \033[0m "Else Echo-e" \033[1;32msoftware'll be installed To/usr/loc al/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 initializing Database ... \033[0m" Else echo-e "\033[1;34minitialization data base...\033[0m "fi/usr/local/mysql/scripts/mysql_install_db--user=mysql--datadir=/usr/local/mysql/data/-- basedir=/usr/local/mysql/&>/dev/null chown-r root.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/l ocal/mysql/support-files/my-default.cnf/etc/my.cnf echo "/usr/local/mysql/lib/" >>/etc/ld. so.conf ldconfig Cat >>/etc/profile << eofpath=\ $PATH:/usr/local/mysql/bin/export patheof Else Error_nofile MySQL fi} #Install phpinstall_php () {begin PHP Test_yum install_mhash install_libmcrypt Solve_depe nd php If [-f ${PHP_VERSION}.${FORMAT1}];then Untar ${php_version}.${format1} CD ${php_version} configure_info rotat E_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 under normal circumstances the software is installed In the/USR/LOCAL/PHP5 directory. \033[0m "Else Echo-e" \033[1;32msoftware'll be installed to/u sr/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 memcachedinstall_memcached () {begin memcached Test_yum install_libevent Solve_depen D memcached If [-f ${MEMCACHED_VERSION}.${FORMAT1}];then Untar ${memcached_version}.${format1} CD ${memcached_version} 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 Memcahe module for Phpinstall_memcache () {begin memcache if [!-f/usr/local/php 5/bin/phpize];then language If [$?-EQ 0];then Clear echo-e "\033[1;34m error: php.\033[0m not installed" exit else clear echo-e "\033[1;34merror:can ' t found PH p.\033[0m "Exit fi else if [-f ${MEMCACHE_VERSION}.${FORMAT2}];then Untar ${memcache_version}.${format2} CD ${me Mcache_version} solve_depend memcache configure_info rotate_line & Disown $! /usr/local/php5/bin/phpize. &>/dev/null./configure--with-php-config=/usr/local/php5/bin/php-config--enable-memcache &>/dev/ Null make_info make &>/dev/null && make install &>/dev/null result=$? Kill $! If [$result-ne 0];then error_install memcache fi CD. Sed-i ' 728i extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/" '/usr/local/php5/etc/ PHP.ini sed-i ' 856i extension=memcache.so '/usr/local/php5/etc/php.ini fi fi} #Install Jreinstall_java () {Begin Java so Lve_depend Java If [-F jdk1.6.0_27.tar.gz];then tar-xzf jdk1.6.0_27.tar.gz &>/dev/null if [!-E/USR/LOCAL/JDK];then mv jdk1.6.0_27//usr/local/jdk fi cat >>/etc/profile <<eofexport JRE_HOME=/usr/loca L/jdkexport java_bin=/usr/local/jdk/binexport path=/usr/local/jdk/bin: $PATHexport classpath=.:/ Usr/local/jdk/lib/dt.jar: $JRE _home/lib/tools.jarexport jre_home java_bin PATH classpatheof source/etc/profile Else Error_nofile JAVA fi} #Install Tomcatinstall_tomcat () {begin TOMCAT if [-f apache-tomcat-6.0.32.tar.gz];then TAR-XZF AP ache-tomcat-6.0.32.tar.gz &>/dev/null if [!-e/usr/local/tomcat];then MV APACHE-TOMCAT-6.0.32/USR/LOCAL/TOMC At 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:d omenuchoicecase $select in1) Install_nginx;; 2) Install_mysql; 3) install_php; 4) install_memcached; 5) Install_memcache; 6) Install_java; 7) Install_tomcat; 8) Install_nginx install_mysql install_php install_memcached install_memcache Install_java insTall_tomcat;; 9) Exit; *) echo "@ The Adventures of manual.blog.51cto.com" blog (esacdone)
Automating the deployment of LNMP scripts