PHP-FPM Process CPU 100% Problem Solving method

Source: Internet
Author: User
Tags fpm memory usage php code php source code cpu usage high cpu usage

First, process tracking

The code is as follows Copy Code
# Top//Find process PID with high CPU usage
# Strace-p PID//tracking process
# LL/PROC/PID/FD//See which files the process is working on

There will be suspicious PHP code modifications, such as: File_get_contents did not set the timeout time.


second, memory allocation

If process tracking cannot find the problem, and then look at the system for the reason, is it possible that there is not enough memory? It is said that a relatively clean php-cgi open about 20m-30m about the memory, depends on the PHP module to open how much.
Viewing memory usage of php-cgi processes through the PMAP directive
# Pmap $ (pgrep php-cgi |head-1)
By output, the number of processes (Max_children) configured php-cgi, combined with the system's memory size.

Third, monitoring

Finally, the service can be guaranteed to function properly through monitoring and automatic recovery scripts. Here are some of the scripts I used:
Kill a php-cgi process as long as it consumes more memory than%1

The code is as follows Copy Code

#!/bin/sh
Pids= ' PS aux|grep php-cgi|grep-v Grep|awk ' {if ($4>=1) print $} '
For PID in $PIDS
Todo
echo ' Date +%f....%t ' >>/data/logs/phpkill.log
echo $PID >>/data/logs/phpkill.log
Kill-9 $PID
Done

Detecting PHP-FPM processes

The code is as follows Copy Code

#!/bin/bash
NETSTAT-TNLP | grep "php-cgi" >>/dev/null #2 &>/data/logs/php_fasle.log
If ["$?"-eq "1"];then #&& [' netstat-tnlp | grep 9000 | awk ' {print $} ' | awk-f ': "' {print $} '-eq ' 1"] ; then
/USR/LOCAL/WEBSERVER/PHP/SBIN/PHP-FPM start
echo ' Date +%f....%t ' "System memory OOM. Kill php-cgi. PHP-FPM service start. ">>/data/logs/php_monitor.log
Fi

detecting PHP Execution via HTTP

The code is as follows Copy Code

#!/bin/bash
Status= ' Curl-s–head "http://127.0.0.1:8080/chk.php" | awk '/http/{print $} '
If [$status!= "-A $status!=" 304 "]; Then
/USR/LOCAL/WEBSERVER/PHP/SBIN/PHP-FPM restart
echo ' Date +%f....%t ' "PHP-FPM service restart" >>/data/logs/php_monitor.log
Fi

Workaround Two

If you use the above complex, we can follow the steps to monitor and reboot the PHP-FPM to solve the problem.

PHP-FPM Startup script/etc/init.d/php-fpm
Since the beginning of the PHP 5.3.3 has been integrated PHP-FPM so php-fpm after the PHP 5.3.2 version does not support the previous php-fpm (start|restart|stop|reload), do not have to make a special patch, Just to unlock the source directly configure, about the PHP-FPM compilation parameters have

The code is as follows Copy Code

--ENABLE-FPM--with-fpm-user=www--with-fpm-group=www--with-libevent-dir=libevent

Position
Enter the PHP source code directory, and then:

The code is as follows Copy Code

Cp-f (Php-5.3.x-source-dir)/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm

The code is as follows Copy Code

#! /bin/sh

### BEGIN INIT INFO
# PROVIDES:PHP-FPM
# Required-start: $remote _fs $network
# required-stop: $remote _fs $network
# Default-start:2 3 4 5
# default-stop:0 1 6
# Short-description:starts PHP-FPM
# description:starts The PHP FastCGI Process Manager Daemon
### End INIT INFO

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=${prefix}/var/run/php-fpm.pid

php_opts= "--fpm-config $php _fpm_conf"

Wait_for_pid () {
Try=0

While Test $try-LT 35; Todo

Case "$" in
' Created ')
If [-F "$"]; Then
Try= '
Break
Fi
;;

' Removed ')
if [!-f ' $ ']; Then
Try= '
Break
Fi
;;
Esac

Echo-n.
try= ' expr $try + 1 '
Sleep 1

Done

}

Case "$" in
Start
Echo-n "Starting PHP-FPM"

$php _fpm_bin $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
;;

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)
$ stop
$ 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"
;;

*)
echo "Usage: $ {start|stop|force-quit|restart|reload}"
Exit 1
;;

Esac

Save after editing and execute the following command

The code is as follows Copy Code

chmod +x/etc/init.d/php-fpm
Chkconfig PHP-FPM on
# Check it out #
Chkconfig--list PHP-FPM
PHP-FPM 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Complete! You can use the following command to manage PHP-FPM

The code is as follows Copy Code

Service PHP-FPM Start
Service PHP-FPM Stop
Service PHP-FPM Restart
Service PHP-FPM Reload

/ETC/INIT.D/PHP-FPM start
/ETC/INIT.D/PHP-FPM stop
/ETC/INIT.D/PHP-FPM restart
/ETC/INIT.D/PHP-FPM Reload

Run service php-fpm start prompt error, how did not find the problem, then know to put the php-fpm.conf in the PID path before: Semicolon delete!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.