- #!/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
- yuminstall-ywget
- cd/etc/yum.repos.d/
- wgethttp://mirrors.aliyun.com/repo/Centos-6.repo
- wgethttp://mirrors.aliyun.com/repo/epel-6.repo
- yummakecache
- echo"theoperationisok">>$LOG
- echo"thismomentisok"
- else
- echo"Thesoftinstalled"
- echo"readyinstalled">>$LOG
- exit1
- fi
- }
- #现在是再次检测环境,然后进行安装Nginx进行对环境的部署。
- install_Nginx(){
- if[!-f$PATH];then
- yum-ygroupinstall"DevelopmentTools""ServerPlatformDeveopment"
- yum-yinstallopenssl-develpcre-devel
- cd/usr/local/src/
- wgethttp://Nginx.org/download/Nginx-1.12.0.tar.gz
- useraddNginx
- tarzxvfNginx-1.12.0.tar.gz
- cdNginx-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&&makeinstall
- $PATH-t
- echo"Nginxinstallisok">>$LOG
- echo"Nginxinstallisok"
- else
- echo"install_Nginxisinstalled">>$LOG
- echo"install_Nginxinstalled"
- fi
- }
- #现在是检测Nginx是否安装成功,如果没有安装成功则输出“/usr/local/Nginx/sbin/Nginxnotinstalled!”
- check_Nginx(){
- if[!-x$PATH];then
- echo-n"${PATH}notinstalled!"
- exit10
- fi
- }
- #如果没有安装,则手动进行一下的操作启动Nginx
- start(){
- echo-n"starting$NAME;"
- $PATH-t
- test=$?
- if[$test-eq0];then
- touch/tmp/Nginx.pid
- $PATH
- echo"$DATEand$PATHisstarting">>$LOG
- curlhttp://127.0.0.1
- else
- echo"pleasecheckyouconfig"
- exit20
- fi
- }
- #在安装完Nginx之后,如果需要关闭Nginx服务,可以进行一下操作来关闭Nginx对外服务。
- stop(){
- echo-n"stopping$NAME;"
- ps-ef|grepNginx|awk'{print$2}'|xargskill-9
- test=0
- if[$test-eq0];then
- rm-rf/tmp/Nginx.pid
- echo"$DATEand$PATHisstop">>$LOG
- fi
- }
- #这里是关于Nginx的重加载,重启动。
- reload(){
- echo-n"reloading$NAME;"
- # ps-ef|grepNginx|awk'{print$2}'|xargskill-9
- $PATH-t
- test=$?
- if[$test-eq0];then
- touch/tmp/reload.pid
- echo"$DATEand$PATHisreloading">>$LOG
- rm-rf/tmp/reload.pid
- else
- echo"pleasecheckyouconfig"
- exit20
- fi
- }
- #现在进行安装PHP,同样脚本先进行对环境检测,如果没有安装,则自动使用脚本安装PHP。
- PHP_install(){
- if[!-f$PHP];then
- cd/usr/local/src
- yum-yinstalllibmcrypt-develbzip2-develgccopenssl-develPHP-mcryptlibmcryptlibxml2-devellibjpeg-devellibpng-develfreetype-devel
- wgethttp://cn2.PHP.net/distributions/PHP-5.5.38.tar.gz
- tarzxvfPHP-5.5.38.tar.gz
- cdPHP-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&&makeinstall
- 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/nologinPHP
- 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"fpmisdead">>$LOG
- else
- echo"fpmisstarting">>$LOG
- fi
- }
- #现在安装数据库,这里没有使用源代码安装方式,使用了YUM仓库源的方式进行安装。
- MysqL_install(){
- if[!-f$mariadb];then
- yumgroupinstallMysqL-y
- serviceMysqLdstart
- MysqLadmin-urootpassword"000000"
- echo"mariadbisinstallok">>$LOG
- echo"mariadbisinstallok"
- else
- echo"mariadbisontinstalled"
- 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"pleaseaddready|install_Nginx|check_Nginx|PHP_install|check_PHP|MysqL_install|Nginx_stop|Nginx_reload|Nginx_start";;
- esac
- ######---这里是退出脚本之后的界面---######
- 1.在创建完脚本之后需要让脚本拥有执行的权限。
- chmod+xlnmp.sh(这里是脚本的名称)
- 2.或者在创建完脚本之后测试脚本是否有问题
- bash+xlnmp.sh