1.  
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4.  
  5. # Check if user is root
  6. if [ $(id -u) != "0" ]; then
  7. printf "Error: You must be root to run this script!\n"
  8. exit 1
  9. fi
  10.  
  11. printf "=========================================================================\n"
  12. printf "Manager for LNMP service , Written by lixiuran \n"
  13. printf "\n"
  14. printf "Usage: /root/lnmp {start|stop|reload|restart|kill|status}\n"
  15. printf "=========================================================================\n"
  16.  
  17. NGINXNAME=nginx
  18. NGINXCONFIGFILE=/usr/local/nginx/conf/$NGINXNAME.conf
  19. NGINXPIDFILE=/var/run/$NGINXNAME.pid
  20. NGINXDAEMON=/usr/local/nginx/sbin/$NGINXNAME
  21. PHPFPMNAME=php-fpm
  22. PHPFPMCONFIGFILE=/usr/local/php/etc/$PHPFPMNAME.conf
  23. PHPFPMPIDFILE=/var/run/$PHPFPMNAME.pid
  24. PHPFPMDAEMON=/usr/local/php/sbin/$PHPFPMNAME
  25. HOSTNAME=`hostname`
  26. MYSQLPIDFILE=/data/mysql/$HOSTNAME.pid
  27.  
  28. function_start()
  29. {
  30. printf "Starting LNMP...\n"
  31. if [ -f $NGINXPIDFILE ]; then
  32. printf "Nginx is runing!\n"
  33. exit 1
  34. else
  35. $NGINXDAEMON -c $NGINXCONFIGFILE
  36. printf "Nginx start successfully!\n"
  37. fi
  38.  
  39. if [ -f $PHPFPMPIDFILE ]; then
  40. printf "php-fpm is runing!\n"
  41. else
  42. $PHPFPMDAEMON &
  43. printf "PHP-FPM start successfully!\n"
  44. fi
  45.  
  46. if [ -f $MYSQLPIDFILE ]; then
  47. printf "MySQL is runing!\n"
  48. else
  49. /etc/init.d/mysql start
  50. printf "MySQL start successfully!\n"
  51. fi
  52. }
  53.  
  54. function_stop()
  55. {
  56. printf "Stoping LNMP...\n"
  57. if [ -f $NGINXPIDFILE ]; then
  58. kill `cat $NGINXPIDFILE`
  59. printf "Nginx program is stop\n"
  60. else
  61. printf "Nginx program is not runing!\n"
  62. fi
  63.  
  64. if [ -f $PHPFPMPIDFILE ]; then
  65. kill -INT `cat $PHPFPMPIDFILE`
  66. printf "PHP-FPM program is stop\n"
  67. else
  68. printf "PHP-FPM program is not runing!\n"
  69. fi
  70.  
  71. if [ -f $MYSQLPIDFILE ]; then
  72. /etc/init.d/mysql stop
  73. printf "MySQL program is stop\n"
  74. else
  75. printf "MySQL program is not runing!\n"
  76. fi
  77. }
  78.  
  79. function_reload()
  80. {
  81. printf "Reload LNMP...\n"
  82. printf "Reload Nginx configure...\n"
  83. $NGINXDAEMON -t
  84. $NGINXDAEMON -s reload
  85. printf "Nginx program is reloding!\n"
  86. /etc/init.d/mysql reload
  87. kill -USR2 `cat $PHPFPMPIDFILE`
  88. }
  89.  
  90. function_restart()
  91. {
  92. printf "Reload LNMP...\n"
  93. printf "Reload Nginx configure...\n"
  94. $NGINXDAEMON -t
  95. kill `cat $NGINXPIDFILE`
  96. $NGINXDAEMON -c $NGINXCONFIGFILE
  97. printf "Nginx program is restarting!\n"
  98. /etc/init.d/mysql restart
  99. printf "PHP-FPM program is restarting!\n"
  100. kill -USR2 `cat $PHPFPMPIDFILE`
  101. }
  102.  
  103. function_kill()
  104. {
  105. kill `cat $NGINXPIDFILE`
  106. kill `cat $PHPFPMPIDFILE`
  107. kill `cat $MYSQLPIDFILE`
  108. }
  109.  
  110. function_status()
  111. {
  112. if [ -f $NGINXPIDFILE ]; then
  113. printf "Nginx is runing!\n"
  114. else
  115. printf "Nginx is stop!\n"
  116. fi
  117.  
  118. if [ -f $PHPFPMPIDFILE ]; then
  119. printf "php-fpm is runing!\n"
  120. else
  121. printf "php-fpm is stop!\n"
  122. fi
  123. /etc/init.d/mysql status
  124. }
  125.  
  126. case "$1" in
  127. start)
  128. function_start
  129. ;;
  130. stop)
  131. function_stop
  132. ;;
  133. restart)
  134. function_stop
  135. function_start
  136. ;;
  137. reload)
  138. function_reload
  139. ;;
  140. kill)
  141. function_kill
  142. ;;
  143. status)
  144. function_status
  145. ;;
  146. *)
  147. printf "Usage: /root/lnmp {start|stop|reload|restart|kill|status}\n"
  148. esac
  149. exit
  1.  
  1.  

Nginx和PHP-FPM的启动/重启脚本

一、Nginx启动脚本/etc/init.d/nginx

  1. #!/bin/bash
  2. #
  3. # Startup script for Nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: - 85 15
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
  7. # processname: nginx
  8. # config: /usr/local/nginx/conf/nginx.conf
  9. # pidfile: /usr/local/nginx/logs/nginx.pid
  10.  
  11. # Source function library.
  12. . /etc/rc.d/init.d/functions
  13.  
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16.  
  17. # Check that networking is up.
  18. [ "$NETWORKING" = "no" ] && exit 0
  19.  
  20. nginx="/usr/local/nginx/sbin/nginx"
  21. prog=$(basename $nginx)
  22.  
  23. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  24.  
  25. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  26.  
  27. lockfile=/var/lock/subsys/nginx
  28.  
  29. start() {
  30. [ -x $nginx ] || exit 5
  31. [ -f $NGINX_CONF_FILE ] || exit 6
  32. echo -n $"Starting $prog: "
  33. daemon $nginx -c $NGINX_CONF_FILE
  34. retval=$?
  35. echo
  36. [ $retval -eq 0 ] && touch $lockfile
  37. return $retval
  38. }
  39.  
  40. stop() {
  41. echo -n $"Stopping $prog: "
  42. killproc $prog -QUIT
  43. retval=$?
  44. echo
  45. [ $retval -eq 0 ] && rm -f $lockfile
  46. return $retval
  47. }
  48.  
  49. restart() {
  50. configtest || return $?
  51. stop
  52. sleep 1
  53. start
  54. }
  55.  
  56. reload() {
  57. configtest || return $?
  58. echo -n $"Reloading $prog: "
  59. killproc $nginx -HUP
  60. RETVAL=$?
  61. echo
  62. }
  63.  
  64. force_reload() {
  65. restart
  66. }
  67.  
  68. configtest() {
  69. $nginx -t -c $NGINX_CONF_FILE
  70. }
  71.  
  72. rh_status() {
  73. status $prog
  74. }
  75.  
  76. rh_status_q() {
  77. rh_status >/dev/null 2>&1
  78. }
  79.  
  80. case "$1" in
  81. start)
  82. rh_status_q && exit 0
  83. $1
  84. ;;
  85. stop)
  86. rh_status_q || exit 0
  87. $1
  88. ;;
  89. restart|configtest)
  90. $1
  91. ;;
  92. reload)
  93. rh_status_q || exit 7
  94. $1
  95. ;;
  96. force-reload)
  97. force_reload
  98. ;;
  99. status)
  100. rh_status
  101. ;;
  102. condrestart|try-restart)
  103. rh_status_q || exit 0
  104. ;;
  105. *)
  106. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  107. exit 2
  108. esac

#==============================================================

编辑好后保存,执行以下命令

  1. sudo chmod +x /etc/init.d/nginx
    sudo /sbin/chkconfig nginx on
  2. # 检查一下
    sudo /sbin/chkconfig --list nginx
  3. nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

完成!可以使用以下命令管理Nginx了

service nginx start
service nginx stop
service nginx restart
service nginx reload
 
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx reload

二、PHP-FPM启动脚本/etc/init.d/php-fpm

  1. #!/bin/bash
  2. #
  3. # Startup script for the PHP-FPM server.
  4. #
  5. # chkconfig: 345 85 15
  6. # description: PHP is an HTML-embedded scripting language
  7. # processname: php-fpm
  8. # config: /usr/local/php/etc/php.ini
  9.  
  10. # Source function library.
  11. . /etc/rc.d/init.d/functions
  12.  
  13. PHP_PATH=/usr/local
  14. DESC="php-fpm daemon"
  15. NAME=php-fpm
  16. # php-fpm路径
  17. DAEMON=$PHP_PATH/php/sbin/$NAME
  18. # 配置文件路径
  19. CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
  20. # PID文件路径(在php-fpm.conf设置)
  21. PIDFILE=$PHP_PATH/php/var/run/$NAME.pid
  22. SCRIPTNAME=/etc/init.d/$NAME
  23.  
  24. # Gracefully exit if the package has been removed.
  25. test -x $DAEMON || exit 0
  26.  
  27. rh_start() {
  28. $DAEMON -y $CONFIGFILE || echo -n " already running"
  29. }
  30.  
  31. rh_stop() {
  32. kill -QUIT `cat $PIDFILE` || echo -n " not running"
  33. }
  34.  
  35. rh_reload() {
  36. kill -HUP `cat $PIDFILE` || echo -n " can't reload"
  37. }
  38.  
  39. case "$1" in
  40. start)
  41. echo -n "Starting $DESC: $NAME"
  42. rh_start
  43. echo "."
  44. ;;
  45. stop)
  46. echo -n "Stopping $DESC: $NAME"
  47. rh_stop
  48. echo "."
  49. ;;
  50. reload)
  51. echo -n "Reloading $DESC configuration..."
  52. rh_reload
  53. echo "reloaded."
  54. ;;
  55. restart)
  56. echo -n "Restarting $DESC: $NAME"
  57. rh_stop
  58. sleep 1
  59. rh_start
  60. echo "."
  61. ;;
  62. *)
  63. echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
  64. exit 3
  65. ;;
  66. esac
  67. exit 0

#=====================================================

编辑好后保存,执行以下命令

  1. sudo chmod +x /etc/init.d/php-fpm
  2. sudo /sbin/chkconfig php-fpm on
  3. # 检查一下
    sudo /sbin/chkconfig --list php-fpm
  4. php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  5.  
  6. 完成!可以使用以下命令管理php-fpm
  7.  
  8. service php-fpm start
  9. service php-fpm stop
  10. service php-fpm restart
  11. service php-fpm reload
  12.  
  13. /etc/init.d/php-fpm start
  14. /etc/init.d/php-fpm stop
  15. /etc/init.d/php-fpm restart
  16. /etc/init.d/php-fpm reload

注意:里面的程序路径必须写对,这里用的都是默认的路径哟!而且对于php-fpm,默认的pid文件是没有设置的,要使用这个必须先在配置文件启用pid文件!~

二、mysql启动脚本/etc/init.d/mysql

添加服务,拷贝服务脚本到init.d目录,并设置开机启动

  1. cp /usr/local/mysql/support-files/support-files/mysql.server /etc/init.d/mysql
  2. chkconfig mysql on
  3. service mysql start --启动MySQL

lnmp启动脚本的更多相关文章

  1. centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课

    centos  LNMP第一部分环境搭建 LAMP安装先后顺序  LNMP安装先后顺序 php安装 安装nginx  编写nginx启动脚本   懒汉模式  mv   /usr/local/php/{ ...

  2. LNMP 1.4 nginx启动脚本和配置文件

    编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...

  3. logstash服务启动脚本

    logstash服务启动脚本 最近在弄ELK,发现logstash没有sysv类型的服务启动脚本,于是按照网上一个老外提供的模板自己进行修改 #添加用户 useradd logstash -M -s ...

  4. 改进uwsgi启动脚本,使其支持多个独立配置文件

    最近在研究flask,在架设运行环境的时候犯了难.因为我想把每个独立的应用像NGINX处理多个网站那样,每个应用单独一个配置文件.而网上流传的uwsgi启动脚本都只支持单个配置文件.虽然有文章说可以把 ...

  5. linux nginx 启动脚本

    linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...

  6. busybox rootfs 启动脚本分析(二)

    上次分析了busybox的启动脚本,这次分析一下init.d中一些脚本的内容. 参考链接 http://www.cnblogs.com/helloworldtoyou/p/6169678.html h ...

  7. Tomcat启动脚本

    记录一个比较好的tomcat启动脚本,截取<OneinStack>,修改如下两个参数即可用. 使用之前修改下面2个参数: #Location of JAVA_HOME (bin files ...

  8. Linux Runlevel 启动 脚本

    Linux 操作系统自从开始启动至启动完毕需要经历几个不同的阶段,这几个阶段就叫做 Runlevel,同样,当Linux操作系统关闭时也要经历另外几个不同的 Runlevel,下面详细介绍一下 Run ...

  9. [Tomcat 源码分析系列] (二) : Tomcat 启动脚本-catalina.bat

    概述 Tomcat 的三个最重要的启动脚本: startup.bat catalina.bat setclasspath.bat 上一篇咱们分析了 startup.bat 脚本 这一篇咱们来分析 ca ...

随机推荐

  1. FreeRTOS 动态内存管理

    以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 动态内存管理,动态内存管理是 FreeRTOS 非常重要的一项功能,前面 ...

  2. 一款基于jquery和css3的响应式二级导航菜单

    今天给大家分享一款基于jquery和css3的响应式二级导航菜单,这款导航是传统的基于顶部,鼠标经过的时候显示二级导航,还采用了当前流行的响应式设计.效果图如下: 在线预览   源码下载 实现的代码. ...

  3. linux查找系统中占用磁盘空间最大的文件

    Q:下午有一客户磁盘空间占用很大,使用df查看磁盘剩余空间很小了,客户想知道是哪些文件占满了文件. Q1:在Linux下如何查看系统占用磁盘空间最大的文件? Q2:在Linux下如何让文件夹下的文件让 ...

  4. http://blog.csdn.net/ce123_zhouwei/article/details/7364294

    http://blog.csdn.net/ce123_zhouwei/article/details/7364294

  5. MySql 生成日期随机数

    select DATE_ADD(sd, INTERVAL FLOOR(1+ RAND() * ((ABS(UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd))) - 1)) ...

  6. MDL---Material Design Lite框架推荐

    INTRO material design相比不会陌生, 现在的移动端基本遵循了这个设计规范, 微软退出过一个残次品universal design(花了半个月时间赶出来的规范)也是借鉴了MD的思想, ...

  7. easyui中datagrid用法,加载table数据与标题

    加载标题写法: 多行标题:columns: [[ columns: [[                       { field: 'itemid', title: 'Item ID', rows ...

  8. [mysql] MySQL Order By Rand()效率【转载】

    最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...

  9. python中的高阶函数

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...

  10. vs2008 x64编译环境 忽略了 #ifdef WIN32

    解决方法: 右键项目属性,在预处理器中添加WIN32即可 效果: