You need to login to send post. No account yet? Create one:
Create account
Script to Restart Apache after crash
Mon, 05/30/2011 - 12:03
It unfortunately happens quite often: at yours web designer's beginnings you were fighting with web hosting's settings and limits and now, when you have switched on VPS, is there the same old trouble in new form - memory limit.
The main problem is that Apache is crashing if there is no memory when is needed. The more you are using you VPS, the more people people is using you websites, the more often is Apache down.
To prevent penalties which could be given to your websites by Search engines because long website's offline status (and avoid necessity of login on VPS and restart Apache manually everytime is down), you can find useful two scripts below, which will restart Apache whenever is down (even if its process is still alive and Apache just not responding on HTTPD).
1. Check if Website is online and PHP is running
Upload this PHP script named 'vpservercheck.php' anywhere on your server:
<?php curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"); // Check server's state echo 1; } else { echo 0; } ?>
As you can see, it will just return 1 or 0 (or nothing) depends on server state.
2. Restart Apache when needed
Save thish BASH script in /root/scripts folder, name it apache_restart.sh and give it an exec permission:
#!/bin/bash # Make sure you make these paths correct result=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php` if [ $result != 1 ] then /etc/init.d/apache2 restart restartcheck=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php` if [ $restartcheck == 1 ] then echo "Apache on VPS have been successfully restarted!" | mail -s "Server Alert: VPS restarted" sentry@yourserver.com exit else echo "Apache on VPS is still in error state!!!" | mail -s "Server Alert: VPS in error state" sentry@yourserver.com exit fi exit fi
Tweak paths to PHP command & vpservercheck.php as needed and make sure that Apache restart command is appropriate for your linux. Stop, Start and Restart vary as:
On RedHat-like operating systems:
~# /etc/init.d/httpd stop ~# /etc/init.d/httpd start ~# /etc/init.d/httpd restart
on Debian/Ubuntu:
~# /etc/init.d/apache2 stop ~# /etc/init.d/apache2 start ~# /etc/init.d/apache2 restart
on FreeBSD:
~# /usr/local/psa/rc.d/httpd stop ~# /usr/local/psa/rc.d/httpd start ~# /usr/local/psa/rc.d/httpd restart
Now is time to test:
- Stop Apache (look above for correct command).
- Try open your website in browser - it should be offline.
- Start BASH script using ~# ./apache_restart.sh
- Refresh browser. Website should be online again.
Last step is start script regularly (once in 5 minuts should be OK) using CRON. As we are on VPS, you should use control panel under root account (or alternatively use command crontab -e) to add:
*/5 * * * * /root/scripts/apache_restart.sh >/dev/null 2>&1
And enjoy peace in mind!
PS: If you wish take care not just about Apache but mail server too, check this post.


















