Lnmp one-button package 502 Bad Gateway solution

  

Using LNmp for a while, but when traffic is heavy, sometimes Nginx 502 Bad Gateway will appear inexplicably, not php-fpm is dead, or nginx is dead. When there are many websites, there is no time to check them one by one every day. It is often found that the website dwon is discovered after 1 or 2 days, and the traffic is lost a lot. Easy to give a way to solve such errors, automatically detect whether the computer configuration website is normal, if not normal, then automatically restart LNmp. First, create a php file test200.php in the root directory of the website, and write a few words at random, such as:
<?php echo "200_OK"; ?>

Then create a script vi test200_monitor. The contents of the sh script are as follows:
wget http://www.piaoyi.org/test200.php --spider c_monitor=$? if [ $c_monitor = "1" ]; then echo "# Error no 200 Status, time: " $(date +"%y-%m-%d %H:%M:%S") "restart lnmp.. " |  Mail -s "Critical Info: Website is offline." [email protected] /usr/local/php/sbin/php-fpm restart echo "restart lnmp: php-fpm" fi

Just monitor the http://www.piaoyi.org/test200.php page and restart PHP if it is not a 200 status code. The command line /usr/local/php/sbin/php-fpm restart can be replaced with other commands, such as /root/lnmp restart. Change your mailbox to your own and send you an email each time an error occurs. [Supplement vi vim usage]: i enter edit state; :wq save exit; :q! Exit without saving. Then set the permissions:
chmod 777 /root/test200_monitor.sh

Run the timer:
crontab -e

Enter the following to check every 5 minutes
*/5 * * * * /root/test200_monitor.sh

Ok, you're done. You can work with confidence, lnmp will no longer appear 502 Bad Gateway error. If you want to run the command line directly in the php script, you can create a php file monitor.php in the root directory of the website. The contents are as follows:
<? $url = 'http://www.piaoyi.org' $cmd = '/usr/local/php/sbin/php-fpm restart'; $exec = "curl --connect-timeout 5 -I $url 2>/dev/null"; $res = shell_exec($ Exec); if(stripos($res,'502 Bad Gateway') !== false){ shell_exec($cmd); exit(); } ?>

Then, as above, add a row to run with crontab Command, online monitoring can be.

Copyright © Windows knowledge All Rights Reserved