php-fpm经常出现502解决方法
更多请支持:http://www.webyang.net/Html/web/article_134.html
最近nginx经常502,解决502最好的办法就是重启php-fpm。
手动去重启还是比较麻烦的事情,最好的就是扔到shell里去。
vim restart-php-fpm.sh
<span class="com">#!/bin/sh</span><span class="pln">SERVICE</span><span class="pun">=</span><span class="pln">\'php</span><span class="pun">-</span><span class="pln">fpm\'</span><span class="com">#if ps ax | grep -v grep | grep $SERVICE > /dev/null</span><span class="pln"></span><span class="kwd">if</span><span class="pln"> netstat </span><span class="pun">-</span><span class="pln">anp </span><span class="pun">|</span><span class="pln"> grep </span><span class="lit">9000</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="str">/dev/</span><span class="kwd">null</span><span class="pln"></span><span class="kwd">then</span><span class="pln"> echo \"$SERVICE service running</span><span class="pun">,</span><span class="pln"> everything </span><span class="kwd">is</span><span class="pln"> fine\"</span><span class="kwd">else</span><span class="pln"> echo \"$SERVICE </span><span class="kwd">is</span><span class="pln"> </span><span class="kwd">not</span><span class="pln"> running\" service php</span><span class="pun">-</span><span class="pln">fpm start</span><span class="kwd">fi</span>本地虚拟机测试了一下,可行,扔到服务器上去,结果发现502的时候并未php-fpm没有进程,而是php-fpm僵死。
vim restart-php-fpm.sh
<span class="com">#!/bin/bash</span><span class="pln">MY_URL</span><span class="pun">=</span><span class="pln">\"http</span><span class="pun">:</span><span class="com">//www.webyang.net\"</span><span class="pln">RESULT</span><span class="pun">=</span><span class="pln">\'curl </span><span class="pun">-</span><span class="pln">I $MY_URL </span><span class="pun">|</span><span class="pln"> grep \"HTTP</span><span class="pun">/</span><span class="lit">1.1</span><span class="pln"> </span><span class="lit">502</span><span class="pln">\"\' </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n \"$RESULT\" </span><span class="pun">];</span><span class="pln"> </span><span class="kwd">then</span><span class="pln"></span><span class="com">#如果502则会执行这里的内容,随便加或改</span><span class="pln">killall </span><span class="pun">-</span><span class="lit">9</span><span class="pln"> php</span><span class="pun">-</span><span class="pln">cgiservice php</span><span class="pun">-</span><span class="pln">fpm restart</span><span class="kwd">fi</span>这样的话,还是手动的,如果需要自动,自己添加到crontab里去。
或者写一个shell死循环,隔一个时间段去执行即可。