#!/bin/bash       -----编写脚本默认启动的进程

DATE=`date "+%F %H:%M:%S"`         ----获取系统时间的命令
LOG=/tmp/$0.log                       -----日志的保存,其中$0意思为该文本的名字
PATH='/usr/local/nginx/sbin/nginx'       ------源代码安装NGINX之后的默认启动nginx的路径
test=0                                
NAME=nginxd                            
fpm='/usr/local/php/sbin/php-fpm'         ---php-fpm的启动路径
mariadb=/var/lib/mysql                ----数据库默认安装的路径

#ready为部署LNMP环境做一个检测,判断该环境是否有LNMP环境以及配置yum仓库源,安装需要用到的软件包等;
ready(){
	if [ ! -f $PATH ];then
		mv /etc/yum.repos.d/CentOs-Base.repo  /etc/yum.repos.d/CentOs-source.repo
                yum install -y wget
                cd /etc/yum.repos.d/
                wget http://mirrors.aliyun.com/repo/Centos-6.repo
                wget  http://mirrors.aliyun.com/repo/epel-6.repo
                yum makecache
		echo "the operation is ok">>$LOG
		echo "this moment is ok"
	else
	echo "The soft installed"
	echo "ready installed" >> $LOG
	exit 1
	fi
}


#现在是再次检测环境,然后进行安装NGINX进行对环境的部署。
install_nginx(){
	if [ ! -f $PATH ];then
		yum -y groupinstall "Development Tools" "Server Platform Deveopment"
		yum -y install openssl-devel pcre-devel
		cd /usr/local/src/
		wget http://nginx.org/download/nginx-1.12.0.tar.gz
		useradd nginx
		tar zxvf nginx-1.12.0.tar.gz
		cd nginx-1.12.0/
		./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre
		make && make install
		$PATH -t
		echo "nginx install is ok">>$LOG
		echo "nginx install is ok"
	else
		echo "install_nginx is installed">>$LOG
		echo "install_nginx installed"
	fi
}


#现在是检测nginx是否安装成功,如果没有安装成功则输出“ /usr/local/nginx/sbin/nginx not installed!”
check_nginx(){
	if [ ! -x $PATH ];then
	echo  -n "${PATH} not installed!"
	exit 10
fi
}


#如果没有安装,则手动进行一下的操作启动nginx
start(){
	echo -n " starting $NAME ;"
	$PATH -t
	test=$?
	if [ $test -eq 0 ];then
	touch /tmp/nginx.pid
	$PATH
	echo "$DATE and $PATH is starting" >> $LOG
	curl http://127.0.0.1
else
	echo "please check you config"
	exit 20
	fi
}


#在安装完nginx之后,如果需要关闭nginx服务,可以进行一下操作来关闭nginx对外服务。
stop(){
	echo -n "stopping $NAME ;"
	ps -ef |grep nginx | awk '{print $2}' | xargs kill -9
	test=0
	if [ $test -eq 0 ] ;then
	rm -rf /tmp/nginx.pid 
	echo "$DATE and $PATH is stop" >> $LOG
	fi
}


#这里是关于nginx的重加载,重启动。
reload(){
	echo -n "reloading $NAME ;"
#	ps -ef |grep nginx | awk '{print $2}' | xargs kill -9
	$PATH -t
	test=$?
	if [ $test -eq 0 ];then
	touch /tmp/reload.pid
	echo "$DATE and $PATH is reloading " >> $LOG
	rm -rf /tmp/reload.pid
else
	echo "please check you config"
	exit 20
	fi

}


#现在进行安装PHP,同样脚本先进行对环境检测,如果没有安装,则自动使用脚本安装PHP。
php_install(){
	if [ ! -f $PHP ]; then
	cd /usr/local/src
	yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt libxml2-devel libjpeg-devel libpng-devel freetype-devel
	 wget http://cn2.php.net/distributions/php-5.5.38.tar.gz
	 tar zxvf php-5.5.38.tar.gz
	cd php-5.5.38/
	./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt  --with-bz2 --enable-fpm --with-gd
	make && make install
	cp /usr/local/src/php-5.5.38/php.ini-production /usr/local/php/etc/php.ini
	mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
	useradd -M -s /sbin/nologin php
	sed -i -e 's\;pid = run/php-fpm.pid\pid = run/php-fpm.pid\g' -e 's\nobody\php\g' -e 's\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g' /usr/local/php/etc/php-fpm.conf
	sed -i 's\;daemonize = yes\daemonize = no\g' /usr/local/php/etc/php-fpm.conf
	bash $fpm &
	fi

}


#这里用于检测PHP是否安装和php-fpm是否启动成功。
check_php(){
                if [ -x  $fpm ];then
                        $fpm -t
                        echo "fpm is dead" >>$LOG
                else
                        echo "fpm is starting">>$LOG
                fi
}


#现在安装数据库,这里没有使用源代码安装方式,使用了YUM仓库源的方式进行安装。
mysql_install(){
	if [ ! -f $mariadb ];then 
	yum groupinstall mysql -y
	service mysqld start
	mysqladmin -uroot password "000000"
	echo "mariadb is install ok">>$LOG
	echo "mariadb is install ok"
	else
	echo "mariadb is ont installed"
	fi
}


#整个脚本使用的是case语句,适合初学者或者对用脚本源代码安装模糊的同学。
#该脚本需要手动执行语句,当你输入出错的时候脚本会提醒你需要执行的命令就是有以下的case语句进行调度的。
case "$1" in
	ready) ready;;
	install_nginx)install_nginx;;
	check_nginx)check_nginx;;
	php_install)php_install;;
	check_php)check_php;;
	mysql_install)mysql_install;;
	nginx_stop) stop;;
	nginx_reload) reload;;
	nginx_start) start;;
	*) echo "please add ready|install_nginx|check_nginx|php_install|check_php|mysql_install|nginx_stop|nginx_reload|nginx_start";;
esac





######---这里是退出脚本之后的界面---######
1.在创建完脚本之后需要让脚本拥有执行的权限。

chmod +x lnmp.sh(这里是脚本的名称)

2.或者在创建完脚本之后测试脚本是否有问题
bash +x lnmp.sh