2022-1-21更新,老版本的设置已经不能使用到新版本,现在7.4的1G内存可开子进程20--30,你也可以查看PHP每个进程使用多少内存,然后再根据你的内存来设置即可。

2011年7月24日
我的502 Bad Gateway问题主要是php-fpm导致的,所以为了更省心的去检查vps的状态我基本上使用定时脚本来维护vps的运行,下面转载一个502脚本

vps版本centos5.6

nginx 1.0.3

php 5.3.5/5.3.6

建立脚本  vim 502.sh


#!/bin/bash
##
#Program:502
##
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# web_URL #
site=http://www.baidu.com
# mailto #
receiver_email="接收邮箱@139.com"
sender_email="发送邮箱@gmail.com"
recevier="接收名"
sender="发送名"
# email #
function run_502(){
sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: VPS has encountered a 502 error!
-------------------------------------------------------------------
The VPS has encountered a 502 error, and it has been handled.
Congratulations! The VPS runs well now!
-------------------------------------------------------------------
EOF
}
function restart_error(){
sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: A terrible error occurred to the VPS.
------------------------------------------------------------------
The VPS has encountered an error, but it failed to restart php-fpm.
The information has already saved to /home/wwwlogs/502log.log.
------------------------------------------------------------------
EOF
}
# test #
stat=`curl -I $site | head -1 | cut -d ' ' -f2`
if [ $stat == "502" ]; then
service php-fpm restart 2>>/home/wwwlogs/502log.log
temp=`echo $?`
if [ $temp != "0" ]; then
restart_error
else
run_502
fi
fi
exit 0

脚本出自: www.ninthday.net/2011/05/auto-restart-httpd-when-error.html

保存并授权chmod 755 502.sh

添加定时任务   我的脚本路径/home/502.sh 执行crontab –e

内容:

*/10 * * * * (/home/502.sh >> /home/wwwlogs/502log.log)

解释:每十分钟检查一次状态并写记录。crontab –l检查任务添加。

开始测试效果:

先service php-fpm stop然后top -c看看是否停掉了php-fpm

然后执行/home/502.sh后再次top -c应该重启了php-fpm和发送了邮件到你指定的邮箱了。

2019-03-01更新
宝塔管理,在后台的软件商城搜索:PHP守护,安装即可,但是该出现的还是会出现,然后可进行微调,
PHP管理-》性能调整

16设置的理由,在终端出用命令查看

top -p `pidof php-fpm | sed 's/[[:space:]]/,/g'`

观察后台php-fpm的线程占用的Res栏内存占用,
php-fpm内存占用在40-70M一个线程,我的站小,占用不固定所以我设16线程,按满来算在60M一个,我的是1G内存的VPS,16x60M=960M是跑满的的话必定是502出现的,现在网站流量几十,所以我设16,当流量大时最稳设在10比较合适。因为你还有nginx、mysql等。

怎么样才能让php-fpm内存占用小点呢?
1、站点流量低
2、你的站点全部使用cdn,比如用cloudflare作全站的cdn,那么php-fpm单线程的内存占用在20-30m左右。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。