下载地址:

 

    最近几天上班仍然没有什么事情做,一如既往的自己在那里学习,最近喜欢上了写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/bashcat << 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/nullif [ "$?" -eq "0" ];thenecho "Mysql is exist."elseecho -e "\e[1;31m Could not found Mysql. \e[0m"exit 10fils|grep  "^nginx-.*.tar.gz$" > /dev/nullif [ "$?" -eq "0" ];thenecho "Apache is exist."elseecho -e "\e[1;31m Could not found Apache. \e[0m"exit 10fils|grep  "^php-.*.tar.gz$" > /dev/nullif [ "$?" -eq "0" ];thenecho "PHP is exist."elseecho -e "\e[1;31m Could not found PHP. \e[0m"exit 10fi# 安装所需依赖包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 vimecho "Install software successfully."echo# 安装并配置Mysqlecho -e "\e[1;31mInstalling Mysql. \e[0m"useradd -s /sbin/nologin -M mysqltar -xf $MYSQL.tar.gzcd $MYSQLcmake . -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=mysqlmake && make installcd /usr/local/mysql/chown -R mysql:mysql  .scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysqlchown -R root .\cp support-files/my-medium.cnf /etc/my.cnfsed -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/mysqldBASELINE=`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/mysqldsed -i "${DATALINE}s#datadir=#datadir=/data/mysql#" /etc/init.d/mysqldln bin/* /usr/bin/chkconfig add mysqldchkconfig mysqld onservice mysqld startecho -e "\e[1;32mInstalled Mysql successfully. \e[0m"# 安装Nginxecho -e "\e[1;31mInstalling Nginx. \e[0m"cd $LUJINtar xf ${NGINX}.tar.gzcd ${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-pcremakemake installecho -e "\e[1;32mInstalled Nginx successfully. \e[0m"#安装PHPecho -e "\e[1;31mInstalling PHP. \e[0m"cd ${LUJIN}tar xf ${PHP}.tar.gzcd ${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-zipmakemake installcp php.ini-production /usr/local/php/lib/php.inicd /usr/local/php/etc/cp -a php-fpm.conf.default php-fpm.confcd ..sbin/php-fpmecho -e "\e[1;32mInstalled PHP successfully. \e[0m"#配置Nginx,使其支持PHPIP=`ifconfig|grep inet|grep "inet "|head -1|awk -F'[ ,:]' '{print $13}'`sed -i 's/#user  nobody;/user www www;/' /usr/local/nginx/conf/nginx.confsed -i 's/#gzip  on;/gzip on;/' /usr/local/nginx/conf/nginx.confsed -i '45s/index /& index.php/' /usr/local/nginx/conf/nginx.confsed -i '65,71s/#//g'  /usr/local/nginx/conf/nginx.confsed -i '69s#\/scripts$fastcgi_script_name#$document_root$fastcgi_script_name#'  /usr/local/nginx/conf/nginx.confsed -i "37s/localhost/$IP/"  /usr/local/nginx/conf/nginx.conf/usr/local/nginx/sbin/nginx# 创建PHP测试页面cat << EOF > /usr/local/nginx/html/index.php
EOFecho -e "\e[1;32mLNMP has been successfully installed. \e[0m"