基于Centos使用脚本快速部署LNMP环境

前端之家收集整理的这篇文章主要介绍了基于Centos使用脚本快速部署LNMP环境前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #!/bin/bash-----编写脚本默认启动的进程
  2.  
  3. DATE=`date"+%F%H:%M:%S"`----获取系统时间的命令
  4. LOG=/tmp/$0.log-----日志的保存,其中$0意思为该文本的名字
  5. PATH='/usr/local/Nginx/sbin/Nginx'------源代码安装Nginx之后的默认启动Nginx的路径
  6. test=0
  7. NAME=Nginxd
  8. fpm='/usr/local/PHP/sbin/PHP-fpm'---PHP-fpm的启动路径
  9. mariadb=/var/lib/MysqL----数据库默认安装的路径
  10.  
  11. #ready为部署LNMP环境做一个检测,判断该环境是否有LNMP环境以及配置yum仓库源,安装需要用到的软件包等;
  12. ready(){
  13. if[!-f$PATH];then
  14. mv/etc/yum.repos.d/CentOs-Base.repo/etc/yum.repos.d/CentOs-source.repo
  15. yuminstall-ywget
  16. cd/etc/yum.repos.d/
  17. wgethttp://mirrors.aliyun.com/repo/Centos-6.repo
  18. wgethttp://mirrors.aliyun.com/repo/epel-6.repo
  19. yummakecache
  20. echo"theoperationisok">>$LOG
  21. echo"thismomentisok"
  22. else
  23. echo"Thesoftinstalled"
  24. echo"readyinstalled">>$LOG
  25. exit1
  26. fi
  27. }
  28.  
  29.  
  30. #现在是再次检测环境,然后进行安装Nginx进行对环境的部署。
  31. install_Nginx(){
  32. if[!-f$PATH];then
  33. yum-ygroupinstall"DevelopmentTools""ServerPlatformDeveopment"
  34. yum-yinstallopenssl-develpcre-devel
  35. cd/usr/local/src/
  36. wgethttp://Nginx.org/download/Nginx-1.12.0.tar.gz
  37. useraddNginx
  38. tarzxvfNginx-1.12.0.tar.gz
  39. cdNginx-1.12.0/
  40. ./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
  41. make&&makeinstall
  42. $PATH-t
  43. echo"Nginxinstallisok">>$LOG
  44. echo"Nginxinstallisok"
  45. else
  46. echo"install_Nginxisinstalled">>$LOG
  47. echo"install_Nginxinstalled"
  48. fi
  49. }
  50.  
  51.  
  52. #现在是检测Nginx是否安装成功,如果没有安装成功则输出“/usr/local/Nginx/sbin/Nginxnotinstalled!”
  53. check_Nginx(){
  54. if[!-x$PATH];then
  55. echo-n"${PATH}notinstalled!"
  56. exit10
  57. fi
  58. }
  59.  
  60.  
  61. #如果没有安装,则手动进行一下的操作启动Nginx
  62. start(){
  63. echo-n"starting$NAME;"
  64. $PATH-t
  65. test=$?
  66. if[$test-eq0];then
  67. touch/tmp/Nginx.pid
  68. $PATH
  69. echo"$DATEand$PATHisstarting">>$LOG
  70. curlhttp://127.0.0.1
  71. else
  72. echo"pleasecheckyouconfig"
  73. exit20
  74. fi
  75. }
  76.  
  77.  
  78. #在安装完Nginx之后,如果需要关闭Nginx服务,可以进行一下操作来关闭Nginx对外服务。
  79. stop(){
  80. echo-n"stopping$NAME;"
  81. ps-ef|grepNginx|awk'{print$2}'|xargskill-9
  82. test=0
  83. if[$test-eq0];then
  84. rm-rf/tmp/Nginx.pid
  85. echo"$DATEand$PATHisstop">>$LOG
  86. fi
  87. }
  88.  
  89.  
  90. #这里是关于Nginx的重加载,重启动。
  91. reload(){
  92. echo-n"reloading$NAME;"
  93. # ps-ef|grepNginx|awk'{print$2}'|xargskill-9
  94. $PATH-t
  95. test=$?
  96. if[$test-eq0];then
  97. touch/tmp/reload.pid
  98. echo"$DATEand$PATHisreloading">>$LOG
  99. rm-rf/tmp/reload.pid
  100. else
  101. echo"pleasecheckyouconfig"
  102. exit20
  103. fi
  104.  
  105. }
  106.  
  107.  
  108. #现在进行安装PHP,同样脚本先进行对环境检测,如果没有安装,则自动使用脚本安装PHP
  109. PHP_install(){
  110. if[!-f$PHP];then
  111. cd/usr/local/src
  112. yum-yinstalllibmcrypt-develbzip2-develgccopenssl-develPHP-mcryptlibmcryptlibxml2-devellibjpeg-devellibpng-develfreetype-devel
  113. wgethttp://cn2.PHP.net/distributions/PHP-5.5.38.tar.gz
  114. tarzxvfPHP-5.5.38.tar.gz
  115. cdPHP-5.5.38/
  116. ./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
  117. make&&makeinstall
  118. cp/usr/local/src/PHP-5.5.38/PHP.ini-production/usr/local/PHP/etc/PHP.ini
  119. mv/usr/local/PHP/etc/PHP-fpm.conf.default/usr/local/PHP/etc/PHP-fpm.conf
  120. useradd-M-s/sbin/nologinPHP
  121. 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
  122. sed-i's\;daemonize=yes\daemonize=no\g'/usr/local/PHP/etc/PHP-fpm.conf
  123. bash$fpm&
  124. fi
  125.  
  126. }
  127.  
  128.  
  129. #这里用于检测PHP是否安装和PHP-fpm是否启动成功。
  130. check_PHP(){
  131. if[-x$fpm];then
  132. $fpm-t
  133. echo"fpmisdead">>$LOG
  134. else
  135. echo"fpmisstarting">>$LOG
  136. fi
  137. }
  138.  
  139.  
  140. #现在安装数据库,这里没有使用源代码安装方式,使用了YUM仓库源的方式进行安装。
  141. MysqL_install(){
  142. if[!-f$mariadb];then
  143. yumgroupinstallMysqL-y
  144. serviceMysqLdstart
  145. MysqLadmin-urootpassword"000000"
  146. echo"mariadbisinstallok">>$LOG
  147. echo"mariadbisinstallok"
  148. else
  149. echo"mariadbisontinstalled"
  150. fi
  151. }
  152.  
  153.  
  154. #整个脚本使用的是case语句,适合初学者或者对用脚本源代码安装模糊的同学。
  155. #该脚本需要手动执行语句,当你输入出错的时候脚本会提醒你需要执行的命令就是有以下的case语句进行调度的。
  156. case"$1"in
  157. ready)ready;;
  158. install_Nginx)install_Nginx;;
  159. check_Nginx)check_Nginx;;
  160. PHP_install)PHP_install;;
  161. check_PHP)check_PHP;;
  162. MysqL_install)MysqL_install;;
  163. Nginx_stop)stop;;
  164. Nginx_reload)reload;;
  165. Nginx_start)start;;
  166. *)echo"pleaseaddready|install_Nginx|check_Nginx|PHP_install|check_PHP|MysqL_install|Nginx_stop|Nginx_reload|Nginx_start";;
  167. esac
  168.  
  169.  
  170.  
  171.  
  172.  
  173. ######---这里是退出脚本之后的界面---######
  174. 1.在创建完脚本之后需要让脚本拥有执行的权限。
  175.  
  176. chmod+xlnmp.sh(这里是脚本的名称)
  177.  
  178. 2.或者在创建完脚本之后测试脚本是否有问题
  179. bash+xlnmp.sh

猜你在找的CentOS相关文章