You need to login to send post. No account yet? Create one:
Create account

Script to Restart Apache after crash

No replies
admin

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:

  1. <?php
  2. $curl = curl_init();
  3. curl_setopt($curl, CURLOPT_URL, 'http://www.someyourserver.com'); // Connect to your server
  4. 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");
  5. curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  6. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  7. curl_setopt($curl, CURLOPT_HEADER, false);
  8. curl_setopt($curl, CURLOPT_NOBODY, true);
  9. curl_exec($curl);
  10. $info = curl_getinfo($curl);
  11.  
  12. // Check server's state
  13. if ((!curl_error($curl)) && ($info['http_code'] != 0)) {
  14. echo 1;
  15. } else {
  16. echo 0;
  17. }
  18.  
  19. curl_close($curl);
  20. ?>

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:

  1. #!/bin/bash
  2.  
  3. # Make sure you make these paths correct
  4. result=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php`
  5.  
  6. if [ $result != 1 ]
  7. then
  8. /etc/init.d/apache2 restart
  9. restartcheck=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php`
  10. if [ $restartcheck == 1 ]
  11. then
  12. echo "Apache on VPS have been successfully restarted!" | mail -s "Server Alert: VPS restarted" sentry@yourserver.com
  13. exit
  14. else
  15. echo "Apache on VPS is still in error state!!!" | mail -s "Server Alert: VPS in error state" sentry@yourserver.com
  16. exit
  17. fi
  18. exit
  19. 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.