Install Monit on Ubuntu
Monit can be used to monitor services, restart them if required. To install monit, run
1 |
apt -y install monit |
To start monit, run
1 |
systemctl start monit |
Monitor Apache
1 |
vi /etc/monit/conf-available/apache2-custom |
Add
1 2 3 4 5 6 |
check process apache with pidfile /var/run/apache2/apache2.pid group www-data start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if failed host localhost port 80 protocol http then restart if 5 restarts within 5 cycles then timeout |
Activate the rule with
1 |
ln -s /etc/monit/conf-available/apache2-custom /etc/monit/conf-enabled/apache2-custom |
Restart monit with
1 |
systemctl restart monit |
Testing If Monit works
Open 2 terminals. On one, tail the monit logs with
1 |
tail -f /var/log/monit.log |
On other terminal, stop Apache
1 |
service apache2 stop |
Within 2 minutes, monit will restart apache web server.