Installing Monit & Restart Apache Automatically

Monit is a free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. You can use Monit to monitor daemon processes or similar programs running on localhost. Monit is particular useful for monitoring daemon processes, such as those started at system boot time from /etc/init.d/.

Common monit usage:
a) Monit can start a process if it does not run.
b) Restart a process if it does not respond.
c) Stop a process if it uses to much resources etc.

Installation on CentOS

[bash]
yum install openssl*
wget http://mmonit.com/monit/dist/monit-5.3.2.tar.gz
tar -zxf monit-5.3.2.tar.gz
cd monit-5.3.2
./configure
make && make install
cp monitrc /etc/
cd ..
rm -rf monit-5.3.2*
echo "include /etc/monit.d/*" >> /etc/monitrc
chmod 700 /etc/monitrc
mkdir /etc/monit.d/
nano /etc/monit.d/apache
[/bash]

And copy/paste the following and save the file

Note: If you are on cPanel, use the codeblock posted next to the below post

[code]
check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host 127.0.0.1 port 80 protocol http
and request "/test.html"
then restart
if 5 restarts within 5 cycles then timeout
[/code]

[code]
check process httpd with pidfile /usr/local/apache/logs/httpd.pid
group nobody
start program = "/scripts/restartsrv httpd"
stop program = "/scripts/restartsrv httpd"
if failed host 127.0.0.1 port 80 protocol http
and request "/test.html"
then restart
if 5 restarts within 5 cycles then timeout
[/code]

I believe, it is better if you use “/scripts/restartsrv httpd” instead of “/etc/init.d/httpd start” if you are on cPanel/WHM server.

Note: Next step may change according to your system configuration. The location of apache on cPanel server is
/usr/local/apache and htdocs will be /usr/local/apache/htdocs

So you should use this command provided below

[bash]
echo "Test file" > /usr/local/apache/htdocs/test.html
[/bash]

If you are not using cPanel, then the apache location may be /etc/httpd and htdocs may be /var/www/html. In that case, change the code accordingly

[bash]
echo "Test file" > /var/www/html/test.html
[/bash]

Add monit to rc.local so that it starts automatically when your server starts.
[bash]
echo "/usr/local/bin/monit -d 60 -v -c /etc/monitrc -p /var/run/monit.pid -l /var/log/monit.log" >> /etc/rc.local
[/bash]

Finally, execute monit now.

[bash]
/usr/local/bin/monit -d 60 -v -c /etc/monitrc -p /var/run/monit.pid -l /var/log/monit.log
[/bash]

You will see a message “Starting monit daemon” at the end if everything goes perfect.

That should start monit as deamon and check the ports on every 60 seconds.

Extending monit

You can monitor other services using monit such as mysql, exim etc. To do so, create a new file at /etc/monit.d/ and save it with the appropriate commands to check and restart code.

For example, if you want to monitor mysql, you can use the below commands

[bash]
touch /etc/monit.d/mysql
echo "check process mysqld with pidfile /var/lib/mysql/`hostname`.pid" > /etc/monit.d/mysql
echo "start program = \"/etc/init.d/mysql start\"" >> /etc/monit.d/mysql
echo "stop program = \"/etc/init.d/mysql stop\"" >> /etc/monit.d/mysql
echo "if failed host 127.0.0.1 port 3306 then restart" >> /etc/monit.d/mysql
echo "if 5 restarts within 5 cycles then timeout" >> /etc/monit.d/mysql
[/bash]

Those commands will add the following lines to /etc/monit.d/mysql

[code]
check process mysqld with pidfile /var/lib/mysql/server.vivekv.com.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout
[/code]

Be sure to re-execute monit after adding this.

Hope this helps

2 Replies to “Installing Monit & Restart Apache Automatically”

  1. Hi,

    let me thank you for this great post, I have installed monit using your instructions and can browse it now, but only apache status is showing there, although I have added mysql monitoring as you written but still its only showing httpd in process area. BTW, server is cPanel/WHM.

    Thanks in advance.

  2. Not working?

    root@server [~]# /usr/local/bin/monit -d 60 -v -c /etc/monitrc  -p /var/run/monit.pid -l /var/log/monit.log
    monit: Cannot stat the control file ‘/etc/monitrc ‘ — No such file or directory

Leave a Reply

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