Install Monit on Ubuntu
Monit can be used to monitor services, restart them if required. To install monit, run
apt -y install monit
To start monit, run
systemctl start monit
Monitor Apache
vi /etc/monit/conf-available/apache2-custom
Add
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
ln -s /etc/monit/conf-available/apache2-custom /etc/monit/conf-enabled/apache2-custom
Restart monit with
systemctl restart monit
Testing If Monit works
Open 2 terminals. On one, tail the monit logs with
tail -f /var/log/monit.log
On other terminal, stop Apache
service apache2 stop
Within 2 minutes, monit will restart apache web server.