Apache Restart CentOS Script

Filed Under: Scripts

Apache web server restart is easy but how would you do it automatically? Few days back, my apache server crashed and my website was down for almost 3 hours. I only find it out when I opened my website in browser. So I thought of writing a script which will check if my website is down at regular interval and if it’s down then apache restart command will be issued.

Apache Restart CentOS Script

I am using CentOS operating system but my script should work on other Unix servers too. To have apache restart script setup we need the following:

  1. A PHP script that will check if website is online and PHP is running fine.
  2. A shell script that will use above PHP to check if website is offline, in that case restart apache web server.
  3. Setting cron job to run at regular intervals.

PHP Script to check website status

Here is the PHP script I wrote to check my website status.

servercheck.php


<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.journaldev.com'); // Connect to your server
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");
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$info = curl_getinfo($curl);
     
// Check server's state
if ((!curl_error($curl)) && ($info['http_code'] != 0)) {
 echo 1;
} else {
 echo 0;
}
     
curl_close($curl);
?>

This is a very simple script which returns 1 if the website is UP or 0 if it’s DOWN.

Apache restart script

Here is the shell script I wrote to call the above PHP.

apache_restart.sh


#!/bin/bash
 
# Make sure you make these paths correct
result=`/usr/local/bin/php /home/journal/scripts/apache_restart/servercheck.php`
echo $result
if [ $result != 1 ]
then
/etc/init.d/httpd restart
sleep 10
restartcheck=`/usr/local/bin/php /home/journal/scripts/apache_restart/servercheck.php`
if [ $restartcheck == 1 ]
then
echo "Apache server have been successfully restarted!" | mail -s "Server Alert: Apache restarted" pankaj.kumar@journaldev.com
exit
else
echo "Apache server is still in error state!" | mail -s "Server Alert: Apache in error state" pankaj.kumar@journaldev.com
exit
fi
exit
fi

This apache restart script first calls the PHP to check website status. If the website is down then it restarts the apache server and again checks the status after 10 seconds. If it’s UP then it sends a mail to the given email ID so that we know how many times server has been restarted.

If restart fails, apache restart script sends a mail with an error message that means something is really wrong with the server.

Make sure to give the script execute permission using chmod command and execute it once to test it. Also, check the server restart command as it depends on your server operating system.

Setting Cron Job for Apache Restart Script

Just add below line in the crontab entry to run the above script every five minutes. Make sure to give the correct path from root.


*/5 * * * * /home/journal/scripts/apache_restart/apache_restart.sh > /home/journal/scripts/apache_restart/cron/apache_restart.log 2>&1

The log file will contain the output of the last run of the script. Now we are all set with the script and you will get notified whenever your server has been restarted or it’s in error state via email.

Reference: Apache HTTPD

Comments

  1. Matt says:

    Could this be updated to also check for mysql and restart, or restart it if apache needs restarted?

    Thanks.

  2. Walmor says:

    Thanks for the article!

  3. Dan says:

    Excellent idea. I get occasional 502 gateway errors from nginx which are triggered from backup activities on the website which tend to just be a resource leech.
    The problem is these backups run at a time I am usually unavailable or in bed. I had other resource monitors running but they were resource intensive and were only compounding the issue and not always detecting it. Your simple script above lets me do a check for 502 or other errors and restart nginx and php which brings the site backup.

    I have the php script hosted on another site so that I can do the cURL from there as well in case there are other errors so I do a 2 point check. I tested it with a forced PHP-fpm crash and everything worked fine. Excellent and you have saved me countless sleepless nights and middle of the night phone calls.

    Good work buddy!

  4. Rafael says:

    Nice article 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages