Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to monitor an Apache web server using Monit

Monit is a tool that can monitor your Apache web server, MySQL database, or other daemon process. It can restart the service based on configurable conditions such as CPU usage, memory usage, number of children, etc. It can log status to a file, email status, and it has a web interface for monitoring or restarting the service. Here are the steps I took to install and configure the Monit tool on Ubuntu Hardy. It merely monitors the status of my Apache web server and restarts it if it stops. It also checks if the memory used by Apache is greater than 1 MB and logs it in /var/log/monit.log. For more configuration options, see the examples in the default /etc/monit/monitrc file or the configuration examples in the monit documentation. I also found Ubuntu Geek's guide to be very helpful.

  • Install monit
    $ sudo apt-get install monit
  • Edit the config file
    $ sudo nano /etc/monit/monitrc
    Insert the following:
    # check services every 2 minutes
    set daemon 120
    
    # logging 
    set logfile /var/log/monit.log
    
    # web interface
    set httpd port 2812 and
        use address localhost # only accept connection from localhost
        allow localhost       # allow localhost to connect to the server    
        allow admin:monit     # require user ‘admin’ with password ‘monit’
    
    # monitor apache
    check process apache2 with pidfile /var/run/apache2.pid
        start program = "/etc/init.d/apache2 start"
        if totalmem > 1.0 MB for 2 cycles then alert
  • Check the file syntax
    $ sudo monit -t
  • Enable the service
    $ sudo nano /etc/default/monit
    Change the following line:
    startup=1
  • Start monit
    $ sudo /etc/init.d/monit start
  • Point your browser at http://localhost:2812 and log in using the user "admin" and the password "monit".
  • Click on "apache2" and you can see information about the Apache process.

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Slicehost