Skip to content

Latest commit

 

History

History
204 lines (169 loc) · 4.21 KB

管理源码.md

File metadata and controls

204 lines (169 loc) · 4.21 KB

目录

service管理

fpm的service管理工具的代码在源码目录的sapi/fpm/init.d.php-fpm

  • cd /root/php-7.2.9/sapi/fpm/init.d.php-fpm
  • cp init.d.php-fpm /etc/init.d/php-fpm
  • chmod +x /etc/init.d/php-fpm

然后就可以service php-fpm start|stop|configtest|reload|restart|force-quit|status

systemctl管理

fpm的systemctl管理工具在sapi/fpm/php-fpm.service

  • cp /root/php-7.2.9/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
  • 根据自己配置的fpm配置更改php-fpm.service里面的PIDFILE和ExecStart
  • systemctl daemon-reload
  • systemctl start php-fpm

注意PrivateTmp默认为true,为创建一个临时目录用来保存pid,如果在fpm里面配置的pid为/tmp/fpm.pid,php-fpm.server的PIDFile配置成/tmp/fpm.pid,则会找不到pid文件,因为配置文件在

/tmp/systemd-private-a2895f782f31429d8acc306cf280bb64-php-fpm.service-6wel0O/tmp/php-fpm.pid

可以将PrivateTmp设置为false

shell源码

prefix=/usr/local/php
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=/tmp/fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"

以上为初始化的参数,可以根据自己的fpm配置来修改

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN --daemonize $php_opts
		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		fi

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if [ ! -r $php_fpm_PID ] ; then
			echo "php-fpm is stopped"
			exit 0
		fi

		PID=`cat $php_fpm_PID`
		if ps -p $PID | grep -q $PID; then
			echo "php-fpm (pid $PID) is running..."
		else
			echo "php-fpm dead but pid file exists"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	configtest)
		$php_fpm_BIN -t
	;;

	*)
		echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
		exit 1
	;;

esac

以上为不同的管理方式,支持start|stop|force-quit|restart|reload|status|configtest这几种
可见start默认就是

/usr/local/php/sbin/php-fpm --daemonize --fpm-config /usr/local/php/etc/php-fpm.conf  --pid /usr/local/php/var/run/php-fpm.pid

然后还会创建pid文件

wait_for_pid () {
	try=0

	while test $try -lt 35 ; do
		case "$1" in
			'created')
			if [ -f "$2" ] ; then
                echo 1
			fi
			if [ ! -f "$2" ] ; then
                echo 2
			fi
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;

			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

会判断pid文件是否创建成功,如果不成功就每隔一秒判断一次,在有些情况下,会发生service启动fpm成功,但是service一直不退出情况,可以排查pid文件配置的位置
stop操作就是

kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`

然后会删除pid文件,如果pid文件配置错误,也会发生fpm已经停止了但是service不退出情况
force-quit操作就是

kill -TERM `cat /usr/local/php/var/run/php-fpm.pid`

然后删除pid文件
restart操作就是

$0 stop
$0 start

先停止然后重启启动
reload操作就是

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`