Configure Monit For MySQL, Apache, NGINX

By bhagwatchouhan
Configure Monit For MySQL, Apache, NGINX

In the previous tutorial, we have discussed installing Monit and how to update the default configuration. We have also discussed configuring Monit to trigger alert emails. In this tutorial, we will discuss confugring Monit for MySQL, Apache, and NGINX. This tutorial provides the steps required to configure Monit on the popular Linux distribution Ubuntu. It provides all the steps required to configure Monit on Ubuntu 18.04 LTS. The steps should be similar for other Linux systems and Ubuntu versions.

 

MySQL

Monit provides the default configurations for the MySQL server as shown below. You can also follow How To Install MySQL 8 on Ubuntu to install the latest MySQL server on Ubuntu.

# Monit - MySQL - Default configuration
cat /etc/monit/conf-available/mysql
# The confuration content check process mysqld with pidfile /var/run/mysqld/mysqld.pid group database group mysql start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart if 5 restarts with 5 cycles then timeout depend mysql_bin depend mysql_rc check file mysql_bin with path /usr/sbin/mysqld group mysql include /etc/monit/templates/rootbin
check file mysql_rc with path /etc/init.d/mysql
group mysql include /etc/monit/templates/rootbin

We can either update the default configuration or just enable it using the commands as shown below.

# Enable conf
sudo ln -s /etc/monit/conf-available/mysql /etc/monit/conf-enabled/
# Disable conf - if saved in conf-available sudo rm /etc/monit/conf-enabled/mysql # OR sudo unlink /etc/monit/conf-enabled/mysql
# Check syntax
monit -t
# Output Control file syntax OK
# Reload Monit sudo /etc/init.d/monit reload # OR sudo systemctl reload monit # OR sudo service monit reload

This is how we can use the default MySQL configuration provided by Monit.

 

Apache

Monit provides the default configurations for the Apache Web Server as shown below. You can also follow How To Install Apache 2 On Ubuntu 18.04 LTS to install the latest Apache server on Ubuntu.

# Monit - Apache - Default configuration
cat /etc/monit/conf-available/apache

# The confuration content check process apache with pidfile /var/run/apache2/apache2.pid group www group apache start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if 4 restarts within 20 cycles then timeout if failed host localhost port 80 with protocol http and request "/server-status" with timeout 25 seconds for 4 times within 5 cycles then restart depend apache_bin depend apache_rc
check file apache_bin with path /usr/sbin/apache2 group apache include /etc/monit/templates/rootbin
check file apache_rc with path /etc/init.d/apache2 group apache include /etc/monit/templates/rootbin

We can either update the default configuration or just enable it using the commands as shown below.

# Enable conf
sudo ln -s /etc/monit/conf-available/apache /etc/monit/conf-enabled/
# Disable conf - if saved in conf-available sudo rm /etc/monit/conf-enabled/apache # OR sudo unlink /etc/monit/conf-enabled/apache
# Check syntax monit -t
# Output Control file syntax OK
# Reload Monit
sudo /etc/init.d/monit reload
# OR
sudo systemctl reload monit
# OR
sudo service monit reload

This is how we can use the default Apache configuration provided by Monit.

 

Nginx

Monit provides the default configurations for the Nginx Web Server as shown below. You can also follow How To Install And Configure Nginx on Ubuntu 18.04 LTS to install the latest Nginx server on Ubuntu.

# Monit - Nginx - Default configuration
cat /etc/monit/conf-available/nginx
# The confuration content check process nginx with pidfile /var/run/nginx.pid group www group nginx start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop"
# if failed port 80 protocol http request "/" then restart if 5 restarts with 5 cycles then timeout depend nginx_bin depend nginx_rc
check file nginx_bin with path /usr/sbin/nginx group nginx include /etc/monit/templates/rootbin
check file nginx_rc with path /etc/init.d/nginx group nginx include /etc/monit/templates/rootbin

We can either update the default configuration or just enable it using the commands as shown below.

# Enable conf
sudo ln -s /etc/monit/conf-available/nginx /etc/monit/conf-enabled/
# Disable conf - if saved in conf-available
sudo rm /etc/monit/conf-enabled/nginx # OR sudo unlink /etc/monit/conf-enabled/nginx
# Check syntax
monit -t
# Output
Control file syntax OK
# Reload Monit sudo /etc/init.d/monit reload # OR sudo systemctl reload monit # OR sudo service monit reload

This is how we can use the default Nginx configuration provided by Monit.

 

Custom Process

This section shows how to configure Monit for own process. Create the configuration file for the process as shown below. I have used the process name myprocess for demonstration purposes. We can create the configuration file directly in the directory conf.d or add it to the directory conf-available. The advantage of the second option is to enable/disable the configuration when required.

# Create the configuration file
# Direct
sudo nano /etc/monit/conf.d/myprocess
# OR - Preferred
sudo nano /etc/monit/conf-available/myprocess
# Update the file check process myprocess matching "myprocess" start program = "/etc/init.d/myprocess start" stop program = "/usr/bin/killall myprocess"
# Save and close the editor
# Enable conf - if saved in conf-available
sudo ln -s /etc/monit/conf-available/myprocess /etc/monit/conf-enabled/
# Disable conf - if saved in conf-available
sudo rm /etc/monit/conf-enabled/myprocess
# OR
sudo unlink /etc/monit/conf-enabled/myprocess
# Check syntax
monit -t
# Output
Control file syntax OK
# Reload Monit
sudo /etc/init.d/monit reload
# OR
sudo systemctl reload monit
# OR
sudo service monit reload

This is the simplest way to configure own or custom processes to be monitored by Monit.

 

Summary

In this tutorial, we have configured Monit for MySQL, Apache, and NGINX. The last section also explained how to configure Monit for own process.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan