设为首页 收藏本站
查看: 420|回复: 0

[经验分享] 如何检测和重启Apache和Lighttpd

[复制链接]

尚未签到

发表于 2017-1-7 09:57:14 | 显示全部楼层 |阅读模式
    Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,用于Unix平台。它可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误。


When you cannot monitor your server for service availability, it is better to take help of automated monitor and restart utility. Last 4 days I was away from my server as I was enjoying my vacation. During this time due to load my lighttpd webserver died but it was restarted automatically within 2 minutes. I had utility configured for monitoring services on a Linux system called monit. It offers all features you ever needed for system monitoring and perform error recovery for UNIX like system.
  Before monit I had my own shell and perl script for monitoring service. If service failed script will try to restart service and send an automated email to me. However monit is a superior solution.
  monit is a utility for managing and monitoring processes, files, directories and devices on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. For example, monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses to much resources. You may use monit to monitor files, directories and devices for changes, such as timestamps changes, checksum changes or size changes.
  You may also use monit to monitor files, directories and devices on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons you can monitor the md5 checksum of files that should not change.
  Personally I always install and configure monit on all boxes which are under my control.

Install monit under Debian or Ubuntu Linux
  Use apt-get command to install monit
# apt-get install monit
OR$ sudo apt-get install monit


Install monit under Red Hat enterprise Linux (source code installation)
  Many distributions include monit. However monit is not included in official Red hat enterprise Linux. Just download monit source code from official web site using wget command:
# cd /opt

# wget http://www.tildeslash.com/monit/dist/monit-4.8.2.tar.gz

Untar monit
# tar -zcvf monit-4.8.2.tar.gz

# cd monit-4.8.2


Configure and compile monit:
  # ./configure

# make


Install monit
  # make install


Copy monit configuration file:
  # cp monitrc /etc/monitrc
  By default monit is located at 
/usr/local/bin/monit


How do I Configure monit?
  monitrc
 
is name of monit configuration file and it is by default located at 
/etc/monitrc
location. However each distribution places file in different location: .

=> Source code installation : /etc/monitrc

=> Debian / Unentu Linux installation : /etc/monit/monitrc
  Open monit configuration file and setup values as follows:
# vi /etc/monitrc

  a) Run it as daemon and check the services (such as web, mysql, sshd) at 2-minute

intervals.
set daemon 
120


  b) Set syslog logging with the ‘daemon’ facility:
set logfile syslog facility log_daemon

  c) Set mail server name to send email alert
set mailserver 
mail.cyberciti.biz



Set email format such as from email
set mail-format { from: alert@nixcraft.in

subject: $SERVICE $EVENT at $DATE

message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.

}

  d) Now most important part, restart lighttpd or apache web server if failed or killed by Linux kernel due to any causes:
check process lighttpd with pidfile /var/run/lighttpd.pid

group lighttpd

start program = "/etc/init.d/lighttpd start"

stop program = "/etc/init.d/lighttpd stop"

if failed host 75.126.43.232 port 80

protocol http then restart

if 5 restarts within 5 cycles then timeout



Where,



  • check process lighttpd with pidfile /var/run/lighttpd.pid 

    : You are specifying lighttpd pid file and daemon name

  • group lighttpd
    : Specify group name, which is allowed or used to start/restart lighttpd

  • start program = “/etc/init.d/lighttpd start”
     
    : Command to start lighttpd server

  • stop program = “/etc/init.d/lighttpd stop”
     
    : Command to stop lighttpd server

  • if failed host 127.0.0.1 port 80
     
    : Server IP address and port number (80)

  • protocol http then restart
     
    : If above IP and port failed restart the webserver

  • if 5 restarts within 5 cycles then timeout 

    : Try to restart 5 times; if monit cannot restart webserver 5 times; just time out to avoid race condition.
  Here is my mysql server restart configuration directives:
check process mysqld with pidfile /var/run/mysqld/mysqld.pid

group database

start program = "/etc/init.d/mysqld start"

stop program = "/etc/init.d/mysqld stop"

if failed host 127.0.0.1 port 3306 then restart

if 5 restarts within 5 cycles then timeout



Here is my sshd server configuration directives:
check process sshd with pidfile /var/run/sshd.pid

start program "/etc/init.d/sshd start"

stop program "/etc/init.d/sshd stop"

if failed host 127.0.0.1 port 22 protocol ssh then restart

if 5 restarts within 5 cycles then timeout


  Replace IP address 127.0.0.1 with your actual IP address. If you are using Debian just start monit:
# /etc/init.d/monit start

  If you are using 
Red Hat Enterprise Linux

, start monit from /etc/inittab file:

Open /etc/inittab file:
# vi /etc/inittab


Append following line:
mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc

  Now start monit:
# inittab -q

  You can verify that monit is started from /var/log/message log file:
# tail -f /var/log/message
Output:

Nov 21 04:39:21 server monit[8759]: Starting monit daemon
Nov 21 04:39:21 server monit[8759]: Monit started
  If lighttpd died, you will see something as follows in log file:

Nov 21 04:45:13 server monit[8759]: 'lighttpd' process is not running
Nov 21 04:45:13 server monit[8759]: 'lighttpd' trying to restart
Nov 21 04:45:13 server monit[8759]: 'lighttpd' start: /etc/init.d/lighttpd
  You may use monit to monitor daemon processes or similar programs running on localhost or started from /etc/init.d/ location such as

=> Apache Web Server

=> SSH Server

=> Postfix/Sendmail MTA

=> MySQL etc

Further readings


  • monit man page

  • monit official
     
    web site and documenation
  原文链接:http://www.cyberciti.biz/tips/howto-monitor-and-restart-linux-unix-service.html




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324974-1-1.html 上篇帖子: Apache POI HSSF的简单使用 下篇帖子: 用Apache POI导出Excel的改进
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表