php-fpm服务启动脚本

时间:2022-10-14 12:18:35

这个我自己在用,没问题,有三个path需要自己酌情修改。

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

内容如下:

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

 

配置php-fpm服务

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

php-fpm使用方法

# 启动
service php-fpm start
# 关闭
service php-fpm stop
# 重启
service php-fpm restart
# 重载
service php-fpm reload
#检查配置文件
service php-fpm configtest

脚本说明

# Source function library.  
. /etc/rc.d/init.d/functions  
 
# Source networking configuration.  
. /etc/sysconfig/network

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

NETWORKING=yes
HOSTNAME=E10162

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

/etc/init.d/php-fpm