下载地址:http://down.51cto.com/data/872036
最近几天上班仍然没有什么事情做,一如既往的自己在那里学习,最近喜欢上了写shell脚本,之前一直想写,可是总没有充足的时间写,所以现在比较有空就赶紧写写,也当是复习一下。上周写了一键LAMP的脚本,这次写的是一键LNMP的脚本,之所以写这种脚本,是觉得部署的时候,有这么一个脚本,放上去跑一下就完事了,很方便,还是挺有意义的。接下来还会写一个一键按Nagios的脚本,现在已经写好了服务端的脚本,还差客户端。写完会马上分享出来的。当然,鉴于本人能力有限,经验也少,所以脚本写得不好,还请大家多多指正。
注意:
脚本运行环境如下:
1、Centos6.3 最小化安装
2、所使用的版本:
(1)mysql-5.5.31.tar.gz
(2)php-5.4.17.tar.gz
(3)nginx-1.4.1.tar.gz
注意:在执行脚本之前,需要先下载好以上3个软件包,并将脚本与上述包放在同一目录下。
下面就是我写的LNMP脚本:
#!/bin/bash cat << EOF ********************************************************************************************************* * * * Welcome to Auto_LNMP! * * In order to install LNMP successfully,please ensure that your system's environment is as follow: * * 1.Make sure that the directory you are standing in is the directory where the softwares are. * * 2.Make sure that softwares are exist,such as nginx,mysql,php. * * 3.Make sure that the type of software is "*.tar.gz",such as "php-5.4.17.tar.gz". * * 4.Make sure that the system has configured yum. * * * * -- Design by Pmghong ^_^ * ********************************************************************************************************* EOF #获取文件名 MYSQL=`ls |grep "^mysql-.*.tar.gz$"|sed 's/.tar.gz//g'` NGINX=`ls|grep "^nginx-.*.tar.gz$"|sed 's/.tar.gz//g'` PHP=`ls|grep "^php-.*.tar.gz$"|sed 's/.tar.gz//g'` LUJIN=`pwd` #检查所需软件是否存在,存在则继续下面操作,不存在则报错,退出 ls |grep "^mysql-.*.tar.gz$" >/dev/null if [ "$?" -eq "0" ];then echo "Mysql is exist." else echo -e "\e[1;31m Could not found Mysql. \e[0m" exit 10 fi ls|grep "^nginx-.*.tar.gz$" > /dev/null if [ "$?" -eq "0" ];then echo "Apache is exist." else echo -e "\e[1;31m Could not found Apache. \e[0m" exit 10 fi ls|grep "^php-.*.tar.gz$" > /dev/null if [ "$?" -eq "0" ];then echo "PHP is exist." else echo -e "\e[1;31m Could not found PHP. \e[0m" exit 10 fi # 安装所需依赖包 echo -e "\e[1;31mInstalling dependent software packages. \e[0m" yum -y install cmake make gcc gcc-c++ perl bison ncurses-devel pcre-devel openssl-devel perl-ExtUtils-Embed libxml2-devel bzip2-devel libcurl-devel libjpeg libjpeg-devel libpng libpng-devel readline-devel net-snmp-devel vim echo "Install software successfully." echo # 安装并配置Mysql echo -e "\e[1;31mInstalling Mysql. \e[0m" useradd -s /sbin/nologin -M mysql tar -xf $MYSQL.tar.gz cd $MYSQL cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DINSTALL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DMYSQL_USER=mysql make && make install cd /usr/local/mysql/ chown -R mysql:mysql . scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql chown -R root . \cp support-files/my-medium.cnf /etc/my.cnf sed -i '/\[mysqld\]/a\basedir = /usr/local/mysql\ndatadir = /data/mysql\nuser = mysql\ncharacter_set_server = utf8' /etc/my.cnf \cp support-files/mysql.server /etc/init.d/mysqld BASELINE=`grep -n 'basedir=' mysql.server | grep -v '#'|head -1|cut -d: -f1` let "DATALINE= $BASELINE + 1" sed -i "${BASELINE}s#basedir=#basedir=/usr/local/mysql#" /etc/init.d/mysqld sed -i "${DATALINE}s#datadir=#datadir=/data/mysql#" /etc/init.d/mysqld ln bin/* /usr/bin/ chkconfig add mysqld chkconfig mysqld on service mysqld start echo -e "\e[1;32mInstalled Mysql successfully. \e[0m" # 安装Nginx echo -e "\e[1;31mInstalling Nginx. \e[0m" cd $LUJIN tar xf ${NGINX}.tar.gz cd ${NGINX} useradd -s /sbin/nologin -M www ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre make make install echo -e "\e[1;32mInstalled Nginx successfully. \e[0m" #安装PHP echo -e "\e[1;31mInstalling PHP. \e[0m" cd ${LUJIN} tar xf ${PHP}.tar.gz cd ${PHP} useradd -s /sbin/nologin -M fpmuser ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=fpmuser --with-fpm-group=fpmuser --disable-ipv6 --with-openssl --with-zlib --with-bz2 --with-curl --enable-dba=shared --with-pcre-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-mhash --enable-mbstring --with-mysql=/usr/local/mysql/ --with-mysql-sock=/tmp/mysql.sock --with-mysqli=/usr/local/mysql/bin/mysql_config --with-readline --with-snmp --enable-sockets --enable-zip make make install cp php.ini-production /usr/local/php/lib/php.ini cd /usr/local/php/etc/ cp -a php-fpm.conf.default php-fpm.conf cd .. sbin/php-fpm echo -e "\e[1;32mInstalled PHP successfully. \e[0m" #配置Nginx,使其支持PHP IP=`ifconfig|grep inet|grep "inet "|head -1|awk -F'[ ,:]' '{print $13}'` sed -i 's/#user nobody;/user www www;/' /usr/local/nginx/conf/nginx.conf sed -i 's/#gzip on;/gzip on;/' /usr/local/nginx/conf/nginx.conf sed -i '45s/index /& index.php/' /usr/local/nginx/conf/nginx.conf sed -i '65,71s/#//g' /usr/local/nginx/conf/nginx.conf sed -i '69s#\/scripts$fastcgi_script_name#$document_root$fastcgi_script_name#' /usr/local/nginx/conf/nginx.conf sed -i "37s/localhost/$IP/" /usr/local/nginx/conf/nginx.conf /usr/local/nginx/sbin/nginx # 创建PHP测试页面 cat << EOF > /usr/local/nginx/html/index.php <?php phpinfo() ?> EOF echo -e "\e[1;32mLNMP has been successfully installed. \e[0m"