php-fpm服务启动脚本的方法

这篇文章主要介绍了php-fpm服务启动脚本的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下,这个我自己在用,没问题,有三个path需要自己酌情修改。

先创建自启动文件:/etc/init.d/php-fpm

内容如下:

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: php-fpm
  4. # Required-Start: $remote_fs $network
  5. # Required-Stop: $remote_fs $network
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: starts php-fpm
  9. # Description: starts the PHP FastCGI Process Manager daemon
  10. ### END INIT INFO
  11. prefix=/usr/local/php
  12. exec_prefix=${prefix}
  13. php_fpm_BIN=${exec_prefix}/sbin/php-fpm
  14. php_fpm_CONF=${prefix}/etc/php-fpm.conf
  15. php_fpm_PID=${prefix}/var/run/php-fpm.pid
  16. php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
  17. wait_for_pid () {
  18. try=0
  19. while test $try -lt 35 ; do
  20. case "$1" in
  21. 'created')
  22. if [ -f "$2" ] ; then
  23. try=''
  24. break
  25. fi
  26. ;;
  27. 'removed')
  28. if [ ! -f "$2" ] ; then
  29. try=''
  30. break
  31. fi
  32. ;;
  33. esac
  34. echo -n .
  35. try=`expr $try + 1`
  36. sleep 1
  37. done
  38. }
  39. case "$1" in
  40. start)
  41. echo -n "Starting php-fpm "
  42. $php_fpm_BIN --daemonize $php_opts
  43. if [ "$?" != 0 ] ; then
  44. echo " failed"
  45. exit 1
  46. fi
  47. wait_for_pid created $php_fpm_PID
  48. if [ -n "$try" ] ; then
  49. echo " failed"
  50. exit 1
  51. else
  52. echo " done"
  53. fi
  54. ;;
  55. stop)
  56. echo -n "Gracefully shutting down php-fpm "
  57. if [ ! -r $php_fpm_PID ] ; then
  58. echo "warning, no pid file found - php-fpm is not running ?"
  59. exit 1
  60. fi
  61. kill -QUIT `cat $php_fpm_PID`
  62. wait_for_pid removed $php_fpm_PID
  63. if [ -n "$try" ] ; then
  64. echo " failed. Use force-quit"
  65. exit 1
  66. else
  67. echo " done"
  68. fi
  69. ;;
  70. status)
  71. if [ ! -r $php_fpm_PID ] ; then
  72. echo "php-fpm is stopped"
  73. exit 0
  74. fi
  75. PID=`cat $php_fpm_PID`
  76. if ps -p $PID | grep -q $PID; then
  77. echo "php-fpm (pid $PID) is running..."
  78. else
  79. echo "php-fpm dead but pid file exists"
  80. fi
  81. ;;
  82. force-quit)
  83. echo -n "Terminating php-fpm "
  84. if [ ! -r $php_fpm_PID ] ; then
  85. echo "warning, no pid file found - php-fpm is not running ?"
  86. exit 1
  87. fi
  88. kill -TERM `cat $php_fpm_PID`
  89. wait_for_pid removed $php_fpm_PID
  90. if [ -n "$try" ] ; then
  91. echo " failed"
  92. exit 1
  93. else
  94. echo " done"
  95. fi
  96. ;;
  97. restart)
  98. $0 stop
  99. $0 start
  100. ;;
  101. reload)
  102. echo -n "Reload service php-fpm "
  103. if [ ! -r $php_fpm_PID ] ; then
  104. echo "warning, no pid file found - php-fpm is not running ?"
  105. exit 1
  106. fi
  107. kill -USR2 `cat $php_fpm_PID`
  108. echo " done"
  109. ;;
  110. *)
  111. echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
  112. exit 1
  113. ;;
  114. esac

配置php-fpm服务

  1. # 设置权限
  2. chmod 755 /etc/init.d/php-fpm
  3. # php-fpm加入服务
  4. chkconfig --add php-fpm
  5. # php-fpm 234级别下设置为启动
  6. chkconfig php-fpm on
  7. # 查看php-fpm服务当前配置
  8. chkconfig --list php-fpm
  9. php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off

php-fpm使用方法

  1. # 启动
  2. service php-fpm start
  3. # 关闭
  4. service php-fpm stop
  5. # 重启
  6. service php-fpm restart
  7. # 重载
  8. service php-fpm reload
  9. #检查配置文件
  10. service php-fpm configtest

脚本说明

  1. # Source function library.
  2. . /etc/rc.d/init.d/functions
  3. # Source networking configuration.
  4. . /etc/sysconfig/network

以上量行代码有人会疑问他们到底是做什么的,'.'是source类似于程序中的include和require,将functions里面的方法全部倒入到这边,这边程序便可以使用,例如这边用到的daemon、status。第二行的network实际上就几行,如下

NETWORKING=yes

HOSTNAME=E10162

将他们作为变量赋值,判断网卡是否启动,如果你的nginx不走网卡,其实网络这段可以去掉.

/etc/init.d/php-fpm