Apache重新启动CentOS脚本

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 Web服务器重启很容易,但是您将如何自动进行重启呢? 几天前,我的apache服务器崩溃了,我的网站关闭了将近3个小时。 我只是在浏览器中打开网站时才发现的。 因此,我想到编写一个脚本,该脚本将检查我的网站是否定期关闭,如果关闭,则会发出apache restart命令。

Apache重新启动CentOS脚本 (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:

我正在使用CentOS操作系统,但是我的脚本也应该在其他Unix服务器上也可以使用。 要使apache重新启动脚本设置,我们需要以下内容:

  1. A PHP script that will check if website is online and PHP is running fine.

    一个PHP脚本,它将检查网站是否在线以及PHP是否运行良好。
  2. A shell script that will use above PHP to check if website is offline, in that case restart apache web server.

    一个shell脚本,将使用上述PHP来检查网站是否离线,在这种情况下,请重新启动Apache Web服务器。
  3. Setting cron job to run at regular intervals.

    将cron作业设置为定期运行。

PHP脚本检查网站状态 (PHP Script to check website status)

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

这是我编写的用于检查网站状态PHP脚本。

servercheck.php

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.

这是一个非常简单的脚本,如果网站打开,则返回1;如果网站关闭,则返回0。

Apache重启脚本 (Apache restart script)

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

这是我写来调用上述PHP的Shell脚本。

apache_restart.sh

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.

此apache重新启动脚本首先调用PHP以检查网站状态。 如果网站关闭,则它将重新启动apache服务器,并在10秒钟后再次检查状态。 如果启动,则它将邮件发送到给定的电子邮件ID,以便我们知道服务器已重启多少次。

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

如果重新启动失败,则apache重新启动脚本会发送一封带有错误消息的邮件,这表明服务器确实有问题。

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.

确保使用chmod命令授予脚本执行权限,并执行一次以对其进行测试。 另外,请检查服务器重新启动命令,因为它取决于您的服务器操作系统。

为Apache重新启动脚本设置Cron作业 (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.

只需在crontab条目中添加以下行即可每五分钟运行一次以上脚本。 确保从根目录提供正确的路径。

*/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

参考: Apache HTTPD

翻译自: https://www.journaldev.com/574/apache-restart-centos-script

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值