LNMP自动安装部署脚本
我们的业务使用了nginx、MySQL、php、和tomcat,写了一个自动化安装这些软件的脚本,虽然网上类似的自动安装脚本很多,但这个是自己写的,特此mark一下,希望能给别人一些参考,同时也灰常欢迎看到的朋友们提些改进意见。话不多说,直接上脚本。
#!/bin/sh
aliasecho='echo-e'
###判断系统,此脚本只适用于64位系统
machine=`uname-m`
if[$machine!=x86_64];then
echo"\e[1;31m\nyoursystemis32bit,butthisscriptisonlyrunon64bit!\n\e[0m"
exit-1
fi
###创建保存下载文件的目录
mkdir/usr/local/src/auto_install&>/dev/null
source_dir=/usr/local/src/auto_install
###定义一个交互式的选择提示
functioninteract{
echo"Pleaseinput\"yes\"or\"no\""
readchoice
case"$choice"in
no)
exit0;;
yes)
echo;;
*)
echo"\e[1;31mInputErrot!\e[0m"
exit-1;;
esac
}
###定义一个函数检查上一条命令的执行状态
functioncheck{
if[$?-ne0];then
echo"\e[1;31m\nthelastcommondexecfailed,pleasecheckit!\e[0m\n"
sleep1
exit-1
fi
}
###定义下载各软件的函数(此处我隐藏了我们的下载地址,需各位自行设置下载地址)
functiondownload_install_epel{
cd$source_dir
rm-fepel*&>/dev/null
echo"\e[1;32m---yuminstallepelrepo---\e[0m\n"
sys_version=`uname-r|cut-d'.'-f4`
if[$sys_version=el7];then
rpm-qa|grep-iepel>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/epel-release-7-2.noarch.rpm
rpm-ivh$source_dir/epel-release-7-1.noarch.rpm
check;
fi
elif[$sys_version=el6];then
rpm-qa|grep-iepel>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/epel-release-6-8.noarch.rpm
rpm-ivh$source_dir/epel-release-6-8.noarch.rpm
check;
fi
fi
}
functiondownload_nginx{
cd$source_dir
echo"\e[1;32m\n---downloadnginx-1.4.7---\e[0m\n"
lsnginx-1.4.7.tar.gz&>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/nginx-1.4.7.tar.gz
check;
fi
}
functiondownload_mysql{
cd$source_dir
echo"\e[1;32m\n---downloadMySQL-5.5.39---\e[0m\n"
lsmysql-5.5.39-linux2.6-x86_64.tar.gz&>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/mysql-5.5.39-linux2.6-x86_64.tar.gz
check;
fi
}
functiondownload_php{
cd$source_dir
echo"\e[1;32m\n---downloadphp-5.3.28---\e[0m\n"
lsphp-5.3.28.tar.gz&>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/php-5.3.28.tar.gz
check;
fi
}
functiondownload_jre{
cd$source_dir
echo"\e[1;32m---downloadjre-6u33-linux-x64.bin---\e[0m"
sleep1
lsjre-6u33-linux-x64.bin&>/dev/null
if[$?-ne0];then
wget-nvhttp://www.example.com/jre-6u33-linux-x64.bin
check;
fi
}
###定义依赖包安装函数
functiondependence_install{
download_install_epel;
echo"\e[1;32m---yuminstalltheDependenciessoftware---\e[0m\n"
sleep1
pgrepyum|xargskill-9>/dev/null
rm-f/var/run/yum.pid>/dev/null
echo"yuminstallgcczlibcmakelibxml2libxml2-develbzip2bzip2-develcurlcurl-devellibjpeglibjpeg-devellibpnglibpng-develfreetype-develpspell-devellibmcryptlibm
crypt-develfreetypepcre-developensslopenssl-devellibaio*"
yum-yinstallgcczlibcmakelibxml2libxml2-develbzip2bzip2-develcurlcurl-devellibjpeglibjpeg-devellibpnglibpng-develfreetype-develpspell-devellibmcryptlibmcry
pt-develfreetypepcre-developensslopenssl-devellibaio*>/dev/null2>&1
check;
}
###定义mysql安装函数###
functionmysql_install{
###判断本机是否已在运行mysql,或者开机启动项有没有mysql###
###判断机器上是否以运行mysql实例
echo"\e[1;32m\n---checkifthereisalreadyinstalledmysqlinstence---\e[0m\n"
ps-ef|grep-wmysqld|grep-v"grep"&>/dev/null
if[$?-eq0];then
mysqlbasedir=`ps-ef|grep-w"mysqld"|grep-v"grep"|awk'{print$9}'|tr-d'\-\-'`
mysqldatadir=`ps-ef|grep-w"mysqld"|grep-v"grep"|awk'{print$10}'|tr-d'\-\-'`
echo"\e[1;32myoursystemhasrunamysqlinstancealready\nmysqlbasediris:"$mysqlbasedir"\nmysqldatadiris:"$mysqldatadir"\ndoyoustillwanttoinstallanewmysql-server?\e[0m\n"
interact;
fi
###判断系统是否已经有rpm方式安装的mysql
echo"MySQL-server-5.5.39-2.el6.x86_64.rpm"|whilereadline
do
prefix=`echo"$line"|awk-F"-"'{print$1"-"$2}'`##togetlike"MySQL-server"
rpm-qa|grep-i"$prefix"&>/tmp/check_mysql.log
if[$?-eq0];then
already_install=`cat/tmp/check_mysql.log|awk-F"-"'{print$1"-"$2"-"$3}'`##togetthealreadyinstalledmysqlversion
echo"\e[1;31m---the$already_installisalreadyinstalledbyrpm!---\n---Doyoureallywanttoinstallanewmysql?---\e[0m\n"
interact;
fi
done
###判断chkconfig里面是否有mysql启动脚本并且为开机启动
chkconfig|grepmysql|awk'{print$5""$7}'|grep-e"启用"-e"on"&>/dev/null
if[$?-eq0];then
echo"thereisabootstartmysqlscriptinthechkconfig,pleasecheck"
exit0
fi
###判断/etc/rc.local文件里是否有mysql启动命令
cat/etc/rc.local|grep"mysqld"&>/dev/null
if[$?-eq0];then
echo"thereisabootstartmysqlcommondinthe/etc/rc.local,pleasecheckit"
exit0
fi
######判断结束######
###检查是否有系统自带的mysql-lib-5.1,有则删除
rpm-qa|grep-i-e"mysql.*5\.1.*"|xargsrpm-e--nodeps2>/dev/null
###交互式选择mysql安装路径
echo"\e[1;31mwheredoyouwanttoinstallmysql(defaultis/usr/local/mysql)\e[0m"
readmybasedir
if["$mybasedir"=""];then
mybasedir=/usr/local/mysql
fi
###选择data目录
echo"\e[1;31mwheredoyouwanttostoragemysqldata(defaultis/usr/local/mysql/data)\e[0m"
readmydatadir
if["$mydatadir"=""];then
mydatadir=/usr/local/mysql/data
fi
###开始安装mysql
echo"\e[1;32m---nowstarttoinstallmysql---\e[0m\n"
sleep1
#dependence_install;
echo"\e[1;32m---addusermysql---\e[0m"
useraddmysql-s/sbin/nologin
echo"\e[1;32m\n---unpackmysql-5.5.39-linux2.6-x86_64.tar.gz---\e[0m"
cd$source_dir
tarzxfmysql-5.5.39-linux2.6-x86_64.tar.gz
check;
rm-rf$mybasedir
mvmysql-5.5.39-linux2.6-x86_64$mybasedir
###判断/etc下是否有my.cnf,若有,重命名为my.cnf.bak
ls/etc/my.cnf&>/dev/null
if[$?=0];then
\mv/etc/my.cnf/etc/my.cnf.bak
echo"\e[1;32m\n---detected/etc/my.cnfisalreadyexit,renameitto/etc/my.cnf.bak---\e[0m\n"
fi
cp$mybasedir/support-files/my-huge.cnf/etc/my.cnf
###拷贝开机启动脚本到/etc/init.d/
ls/etc/init.d/mysql&>/dev/null
if[$?=0];then
\mv/etc/init.d/mysql/etc/init.d/mysql.bak
echo"\e[1;32m---detected/etc/init.d/mysqlisalreadyexit,renameitto/etc/init.d/mysql.bak---\e[0m"
fi
cp$mybasedir/support-files/mysql.server/etc/init.d/mysql
###修改/etc/init.d/mysql脚本里面basedir和datadir
sed-i-e"/^basedir*=$/i\basedir=$mybasedir"-e"/^datadir*=$/i\datadir=$mydatadir"/etc/init.d/mysql
sed-i-e'/^basedir*=$/d'-e'/^datadir*=$/d'/etc/init.d/mysql
###初始化数据库
echo"\e[1;32m\n---initializemysqldatabase---\e[0m"
sleep1
chown-Rmysql$mydatadir$mybasedir>/dev/null2>&1
$mybasedir/scripts/mysql_install_db--defaults-file=/etc/my.cnf--user=mysql--basedir=$mybasedir--datadir=$mydatadir
check;
###将basedir/bin/添加到PATH
echo"PATH=$PATH:$mybasedir/bin">>/etc/profile
exportPATH=$PATH:$mybasedir/bin
echo"\e[1;32minstallmysqlsuccess,youcanrun\"servicemysqlstart\"tostartthemysql\ndonotforgettosetapasswordformysql\e[0m"
###至此mysql安装完毕
}
###定义nginx安装函数
functionnginx_install{
echo
echo"\e[1;32m---startinstallingnginx,it'sbasediris/usr/local/nginx---\e[0m\n"
sleep1
cd$source_dir
ls-l|grep"^d"|grepnginx|xargsrm-rf&>/dev/null
tarzxfnginx-1.4.7.tar.gz
check;
cdnginx-1.4.7
echo"\e[1;32m---configure:configureinformationin/tmp/configure_nginx.log---\e[0m\n"
sleep1
./configure--with-http_stub_status_module--prefix=/usr/local/nginx&>/tmp/configure_nginx.log
check;
echo"\e[1;32m---make:makeinformationin/tmp/make_nginx.log---\e[0m\n"
sleep1
make&>/tmp/configure_nginx.log
check;
echo"\e[1;32m---makeinstall:installinto/usr/local/nginx---\e[0m\n"
sleep1
makeinstall&>/tmp/make_install.log
check;
echo"\e[1;32mnginxisinstalledsuccessfully\e[0m\n"
}
###定义php安装函数
functionphp_install{
dependence_install;
cd$source_dir
ls-l|grep"^d"|grepphp|xargsrm-rf&>/dev/null
tarzxvfphp-5.3.28.tar.gz&>/dev/null
check;
cdphp-5.3.28
echo
echo"\e[1;32m---configurephp:informationin/tmp/php_configure.log---\e[0m\n"
echo"./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--with-mysql--with-mysqli--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--disable-rpath--enable-discard-path--enable-safe-mode--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--with-curlwrappers--enable-mbregex--enable-fastcgi--enable-fpm--enable-force-cgi-redirect--enable-mbstring--with-mcrypt--with-gd--enable-gd-native-ttf--with-openssl--with-mhash--enable-pcntl--enable-sockets--with-iconv-dir=/usr/local--with-zlib--enable-zip--with-curl"
./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--with-mysql--with-mysqli--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir=/usr--enable-xml--disable-rpath--enable-discard-path--enable-safe-mode--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--with-curlwrappers--enable-mbregex--enable-fastcgi--enable-fpm--enable-force-cgi-redirect--enable-mbstring--with-mcrypt--with-gd--enable-gd-native-ttf--with-openssl--with-mhash--enable-pcntl--enable-sockets--with-iconv-dir=/usr/local--with-zlib--enable-zip--with-curl&>/tmp/php_configure.log
check;
###make
echo"\e[1;32m---makephp:informationin/tmp/php_make.log---\e[0m\n"
make&>/tmp/php_make.log
check;
###makeinstall
echo"\e[1;32m---makeinstallphp:installinto/usr/local/php---\n---phpcofigurefileinthe/usr/local/php/etc---\e[0m\n"
makeinstall>/tmp/make_install_php.log
check;
echo"\e[1;32m---preparephpconfigurefiles---\e[0m\n"
echo"cp$source_dir/php-5.3.28/php.ini-production/usr/local/php/etc/php.ini"
cpphp.ini-production/usr/local/php/etc/php.ini
echo"cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf"
cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
echo"\e[1;32m\nphpisinstalledsuccessfully!\nnow,pleasestartphpandmodificationthephp.ini\e[0m"
}
###安装jre
functionjre_install{
###删除系统自带的java
rpm-qa|grepjava|xargsrpm-e--nodeps
cd$source_dir
rm-rfjre1.6.0_33&>/dev/null
chmod+xjre-6u33-linux-x64.bin&>/dev/null
./jre-6u33-linux-x64.bin>/dev/null2>&1
check;
ls-d/usr/java
if[$?-ne0];then
mkdir/usr/java&>/dev/null
else
echoecho"\e[1;31m/usr/javahasalreadyexits,moveto/usr/java_bak!\e[0m"
mkdir/usr/java_bak&>/dev/null
mv/usr/java/*/usr/java_bak/
fi
mvjre1.6.0_33/usr/java/
echo"\e[1;32m\n---addjavapathto/etc/profile---\e[0m"
sleep1
echo>>/etc/profile
echo'JAVA_HOME=/usr/java/jre1.6.0_33'>>/etc/profile
echo'CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar'>>/etc/profile
echo'PATH=$JAVA_HOME/bin:$PATH:$HOME/bin:$JAVA_HOME/jre/bin'>>/etc/profile
echo'exportPATHJAVA_HOME'>>/etc/profile
source/etc/profile
echo"\e[1;32m\nJREisinstalledsuccess!\e[0m"
echo"\e[1;32m\n---installcronolog---\e[0m"
wget-nvhttp://www.example.com/cronolog-1.6.2.tar.gz
dependence_install;
tarzxfcronolog-1.6.2.tar.gz
cdcronolog-1.6.2
./configure&>/tmp/cronolog_configure.log&&make>/dev/null&&makeinstall>/dev/null
check;
echo"\e[1;32mcronologisinstalledsuccess\e[0m\n"
}
###下面开始选择那些软件需要安装
echo"\e[1;31m\n---选择哪些软件需要安装---\e[0m\n"
echo"\e[1;32m\"Nginx\"INPUT\"1\"\e[0m\n"
echo"\e[1;32m\"Mysql\"INPUT\"2\"\e[0m\n"
echo"\e[1;32m\"Php\"INPUT\"3\"\e[0m\n"
echo"\e[1;32m\"JRE\"INPUT\"4\"\e[0m\n"
#echo"\e[1;32m\"Tomcat\"INPUT\"5\"\e[0m\n"
read-p"pleasechoicewhichsoftwaredoyouwanttoinstall?"input
if[$input=1];then
download_nginx;
dependence_install;
nginx_install;
elif[$input=2];then
download_mysql;
mysql_install;
elif[$input=3];then
echo"\e[1;32m\nphpneedsmysqlclient\e[0m"
echo"\e[1;32msoifyouhadalreadyinstallamysqlclient\npleasemakeasoftlinkforall\"libmysqlclient.so.*\"filetoboth/var/lib64and/var/lib\e[0m\n"
echo"\e[1;31mPleaseinputyourchoice\n\e[0m\e[1;32m1:goonbydefault\(installmysql-client-5.5.39\)\n2:Iwillcheckmy\"libmysqlclient.so.*\"fileandredothisscriptlater\n3:IhadmakethesoftlinksandIwanttogoingon\e[0m"
readchoice
case"$choice"in
1)
cd$sourcedir
echo"\e[1;32m---downloadmysql-clientrpmfiles---\e[0m"
wget-nvhttp://www.example.com/MySQL-client-5.5.39-2.el6.x86_64.rpm
wget-nvhttp://www.example.com/MySQL-devel-5.5.39-2.el6.x86_64.rpm
wget-nvhttp://www.example.com/MySQL-shared-5.5.39-2.el6.x86_64.rpm
echo"\e[1;32m\n---installmysql-clientinthewayofrpm---\e[0m"
sleep1
rpm-ivhMySQL-client-5.5.39-2.el6.x86_64.rpmMySQL-devel-5.5.39-2.el6.x86_64.rpmMySQL-shared-5.5.39-2.el6.x86_64.rpm
download_php;
###makesoftlinkfrom/usr/lib64/libmysqlclient*to/usr/lib/
ls-l/usr/lib64/libmysqlclient*|grep-v"^l"|awk'{print$NF}'|whilereadline
do
usrlib_fulldir=`ls-l/usr/lib64/libmysqlclient*|grep-v"^l"|awk'{print$NF}'|sed's/lib64/lib/'`
ln-s$line$usrlib_fulldir
done
php_install;
;;
2)
exit0;;
3)
download_php;
php_install;
;;
*)
echo"InputErrot!"&&exit-1;;
esac
elif[$input=4];then
download_jre;
jre_install;
else
echo"\e[1;31myourinputisworng!\e[0m"
sleep1
exit-1
fi
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。