`

如何检测和重启Apache和Lighttpd

阅读更多

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

原文链接:http://www.cyberciti.biz/tips/howto-monitor-and-restart-linux-unix-service.html


分享到:
评论

相关推荐

    lighttpd代替apache

    标题和描述提到的"lighttpd代替apache"这一话题,揭示了lighttpd在性能和资源效率方面的优势,使其成为高速Web服务器部署的选择。 Apache,全名Apache HTTP Server,是一款开源且功能丰富的Web服务器,广泛应用于...

    lighttpd-1.4.45_lighttpd服务器_

    lighttpd,这个名字在Web服务器领域中或许不如Apache或Nginx那样耳熟能详,但其独特的轻量级特性和高效性能,使得它在特定场景下成为理想的解决方案。lighttpd-1.4.45是lighttpd服务器的一个重要版本,它以其小巧、...

    Linux服务器端Web服务三剑客(Apache Lighttpd Nginx).docx

    ### Linux服务器端Web服务三剑客(Apache Lighttpd Nginx) #### 一、Apache ##### 概述 Apache HTTP Server(简称Apache)是...接下来,我们将继续探讨另外两款轻量级Web服务器——Lighttpd和Nginx的特点及配置方法。

    Web服务器选型(Apache+Nginx+Lighttpd)之性能对比测试报告.pdf

    这篇性能对比测试报告主要关注了三个主流的Web服务器——Apache、Nginx和Lighttpd在性能上的差异。测试的目的是为了评估这三款服务器在实际应用中的性能表现,为未来的项目选择提供依据。测试环境是在一台配置为8核...

    ARM平台lighttpd服务器

    尽管lighttpd可能不如Apache或Nginx那样功能全面,但它的轻量级特性使其在特定场景下更具优势。 总之,ARM平台上的lighttpd服务器为开发者提供了快速、低资源消耗的Web服务解决方案。通过适当的配置和管理,...

    Nginx、Apache、Lighttpd禁止目录执行php配置示例

    本篇文章将详细介绍如何在Nginx、Apache和Lighttpd这三种流行的Web服务器上实现这一功能。 **Apache配置** 在Apache服务器上,我们可以使用`<Directory>`指令来限制指定目录内的PHP执行。以下是一个示例: ```...

    lighttpd-1.4.39.tar.gz

    4. 启动与管理:使用lighttpd命令启动、停止或重启服务器,如`lighttpd -t`检查配置文件,`lighttpd -D`以守护进程模式启动。 五、lighttpd与FastCGI的结合 lighttpd的FastCGI支持是其一大亮点,它能与PHP、Python...

    phpStudy 最新 Apache+Nginx+LightTPD+PHP+MySQL+phpMyAdmin+Zend 稳定 强大

    1、全面适合 Win2000/XP/2003/win7/win8/win2008 操作系统 ,支持Apache、IIS、Nginx和LightTPD。 2、该程序包集成以下软件,括号内为phpstudy下对应的目录。 php 7.0.12 php 5.6.27 php 5.5.38 php 5.4.35...

    Apache、lighttpd、Nginx用哪个搭建高性能服务器

    其实Apache、lighttpd、Nginx都用他们优点,在什么情况下我们如何去选择适合自己的Web高性能服务器,  对于Web高性能服务器上的选择,这个是很多人头痛的问题。其实Apache、lighttpd、Nginx都用他们优点,在什么...

    lighttpd基础入门——状态机与插件

    - 在状态机的不同状态下,Lighttpd会调用相应的插件回调函数来处理事件。 - 开发者需要了解每个回调函数的执行顺序、位置、次数和条件,以便正确地处理事件。 - 在处理事件时,开发者还需要知道可以访问哪些数据以及...

    linux下apache+web服务器搭建教程知识.pdf

    虽然Apache可以在低配置的系统上运行,但为了提供高效的服务,尤其是处理大量并发请求,需要足够的硬盘空间和内存。获取Apache软件,可以从官方网站下载最新版本的源代码或预编译的二进制文件。对于Linux用户,许多...

    suse linux下安装php5、Lighttpd .

    ### SUSE Linux 下安装 PHP5 和 Lighttpd 的详细步骤 在 SUSE Linux 系统中安装 PHP5 和 Lighttpd 需要遵循一系列具体的步骤。本文将详细介绍这一过程中的关键步骤和技术要点,帮助读者顺利完成安装配置。 #### 1....

    phpLight 2013 网上首套LightTPD+PHP集成包

    2. 此为Lighttpd+PHP方案,需要apache+php方案的请到www.phpStudy.net下载phpStudy,Nginx+php方案下载phpfind。 3. 重装系统后或相关服务丢失时,只需要点一下『运行模式-应用』即可,更改路径自动启动。 4. 可通过...

    lighttpd web服务器

    - **与Apache**:相比Apache,lighttpd在资源占用和并发处理上更具优势,但Apache提供了更丰富的模块和广泛的社区支持。 - **与Nginx**:Nginx以其反向代理和负载均衡能力出名,lighttpd在轻量级应用中可能更胜一筹...

    LightTPD-1.4.16-Win32安装版

    1. **高效性能**:LightTPD采用事件驱动模型,支持epoll和kqueue等高效I/O多路复用技术,可以处理大量的并发连接请求,确保在高负载下依然保持稳定的性能。 2. **内存优化**:LightTPD在设计时就注重内存管理,其...

    Apache配置.docx

    ### Apache配置知识点详解 ... ...这类软件通常分为静态服务软件和动态服务软件...综上所述,Apache不仅是一款功能强大的Web服务器软件,而且其灵活的安装方式、广泛的适应性和稳定的性能使其成为许多企业和个人建站的首选。

    Apache svn trac搭建及工具包

    - 安装Web服务器插件:Trac通常与Apache或lighttpd等Web服务器结合使用。对于Apache,需要安装mod_wsgi或mod_python。以mod_wsgi为例,确保已安装该模块,然后在Apache配置文件中添加以下内容: ``` ...

    利用Nginx替代apache实现高性能的Web环境.docx

    相比之下,Nginx 使用异步事件驱动架构,能更有效地处理高并发,同时内存占用更低,因此在需要高性能和低资源消耗的场景下,Nginx 成为了替代 Apache 的理想选择。 【Nginx 作为 PHP Fastcgi 服务器】 在上述环境...

Global site tag (gtag.js) - Google Analytics