php-cgi 502检测脚本
小服务器上跑php-cgi,由于内存太小没法编译带php-fpm的php,只好用二进制源的php+spawn-fcgi。由于众所周知的稳定性问题,需要个脚本检测是否出现 502 Bad Gateway,在网上找了一圈发现这么简单个东西居然用php写(http://www.deepvps.com/nginx-502-bad-gateway-automatically-restart-script.html),顿感杀鸡用牛刀——这东西用shell脚本就足够了嘛。而且shell_exec出于安全考虑都是关掉的。
自己写了个shell脚本,供需要的人用:
status=`/usr/bin/curl -m 3 -s -I http://your-domain/index.php | head -1 | grep 502`
if ["$status" = ""];
then
exit 0
else
kill `cat /var/run/fastcgi-php.pid`
kill `cat /var/run/fastcgi-php2.pid`
/usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a 127.0.0.1 -p 9000 -P /var/run/fastcgi-php.pid -u www-data -g www-data
/usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a 127.0.0.1 -p 9001 -P /var/run/fastcgi-php2.pid -u www-data -g www-data
fi
由于spawn-fcgi不能用kill -HUP重启,也没写init.d的启动脚本,所以这里用了笨办法重启。
用之前最好先curl试试header是不是301 permanently moved,如果是的话把路径写全,直到结果出现200 OK为止。