最近抽時間寫了一份LNMP部署腳本,使用源碼安裝所需軟件,源碼軟件包網絡上很容易獲取,這裏僅貼出腳本內容,你們能夠自行在網絡上下載對應的軟件放在腳本當前目錄便可,實際下載的軟件包若是與腳本所調用的軟件版本號及壓縮格式有差別時,能夠修改腳本開始的變量定義便可。
腳本會檢測目標主機的語音環境,若是目標主機運行中文環境,則腳本運行中的全部提示信息均爲中文,反之則提示信息爲英文。腳本在安裝相關軟件的依賴包時會調用YUM安裝對應的軟件,運行腳本前確認YUM是可用的,不然腳本檢測無YUM源可用後將直接退出。
腳本測試環境:RHEL6.5,但願能夠成爲你們編寫自動部署LNMP及相關腳本的參考。php
腳本內容以下:java
#!/bin/bash #Data:2015-12-09 #Version:1.0 #Author:Jacob(ydh0011@163.com) #The software list:Nginx,MySQL,PHP,Memcached,memcache for php,Tomcat,Java. #This script can automatically install all software on your machine. #@丁丁歷險博客(manual.blog.51cto.com) #Define default variables, you can modify the value. nginx_version=nginx-1.8.0 mysql_version=mysql-5.6.25 cmake_version=cmake-2.8.10.2 mhash_version=mhash-0.9.9.9 libmcrypt_version=libmcrypt-2.5.8 php_version=php-5.4.24 libevent_vertion=libevent-2.0.21-stable memcached_version=memcached-1.4.24 memcache_version=memcache-2.2.5 format1=tar.gz format2=tgz #Determine the language environment #@丁丁歷險博客(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. #@丁丁歷險博客(manual.blog.51cto.com) menu(){ clear language if [ $? -eq 0 ];then echo " ##############----Menu----##############" echo "# 1. 安裝Nginx" echo "# 2. 安裝MySQL" echo "# 3. 安裝PHP" echo "# 4. 安裝Memcached" echo "# 5. 安裝memcache for php" echo "# 6. 安裝Java" echo "# 7. 安裝Tomcat" echo "# 8. 安裝以上全部軟件" echo "# 9. 退出程序" echo " ########################################" else 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 above" echo "# 9. Exit Program" echo " ########################################" fi } #Read user's choice #@丁丁歷險博客(manual.blog.51cto.com) #Read User's INPUT choice(){ language if [ $? -eq 0 ];then read -p "請選擇一個菜單[1-9]:" select else read -p "Please choice a menu[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. #@丁丁歷險博客(manual.blog.51cto.com) error_install(){ language if [ $? -eq 0 ];then clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34m錯誤:編譯安裝[ ${1} ]失敗!\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 "錯誤:本機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. #Return 0 dedicate yum is enable. #Return 1 dedicate yum is disable. #@丁丁歷險博客(manual.blog.51cto.com) test_yum(){ #set yum configure file do not display Red Hat Subscription Management info. sed -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 $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++ 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 install $i &>/dev/null fi done } #Determing how to uncompress a tar file. #@丁丁歷險博客(manual.blog.51cto.com) 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 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} ]軟件包,請下載軟件包至當前目錄.\033[0m" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" exit else clear echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" echo -e "\033[1;34mERROR:Not found [ ${1} ] package in current 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 #@丁丁歷險博客(manual.blog.51cto.com) 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 install_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正常狀況下軟件被安裝在/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(){ 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 libmcrypt fi } #Install libevent install_libevent(){ solve_depend libevent if [ -f ${libevent_vertion}.${format1} ];then untar ${libevent_vertion}.${format1} cd ${libevent_vertion} configure_info ./configure &>/dev/null make &>/dev/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 libevent fi } #Install Nginx #@丁丁歷險博客(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 untar ${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正常狀況下軟件被安裝在/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 install_mysql(){ begin mysql test_yum install_cmake solve_depend mysql grep -q mysql /etc/passwd if [ $? -ne 0 ];then useradd -s /sbin/nologin mysql fi if [ -f ${mysql_version}.${format1} ];then untar ${mysql_version}.${format1} cd ${mysql_version} 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 /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/local/mysql/support-files/my-default.cnf /etc/my.cnf echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf ldconfig cat >> /etc/profile << EOF PATH=\$PATH:/usr/local/mysql/bin/ export PATH EOF else error_nofile mysql fi } #Install PHP install_php(){ begin php test_yum install_mhash install_libmcrypt solve_depend php if [ -f ${php_version}.${format1} ];then untar ${php_version}.${format1} cd ${php_version} 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/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 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 php install_memcache(){ begin memcache if [ ! -f /usr/local/php5/bin/phpize ];then language if [ $? -eq 0 ];then clear echo -e "\033[1;34m錯誤:未安裝PHP.\033[0m" exit else clear echo -e "\033[1;34mERROR:Can't found PHP.\033[0m" exit fi else if [ -f ${memcache_version}.${format2} ];then untar ${memcache_version}.${format2} cd ${memcache_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 JRE install_java(){ begin JAVA solve_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 <<EOF export JRE_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export 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 install_tomcat(){ begin Tomcat if [ -f apache-tomcat-6.0.32.tar.gz ];then tar -xzf apache-tomcat-6.0.32.tar.gz &>/dev/null if [ ! -e /usr/local/tomcat ];then mv apache-tomcat-6.0.32 /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 choice case $select in 1) 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 "@丁丁歷險博客(manual.blog.51cto.com)" esac done