How to Install & Configure Monit on Ubuntu

The administration of each process in Linux can be a tedious task, but with the right tools it becomes something elementary and nowadays we find thousands of applications in the network developed for correct and optimal management of each Linux distribution.

What is Monit?

Monit is a small open source utility that has been developed to manage and monitor Unix systems. When using the Monit application, it is responsible for carrying out maintenance and automatic repair of failed processes and is in the ability to execute management actions in error situations to maintain the operability of the operating system.

Step 1: Install Monit on Ubuntu

To install Monit in Ubuntu, we will execute the following command:

sudo apt install monit

We can run the next line to enable Monit at startup:

sudo systemctl enable monit

Some other options to control Monit are:

Check the status of Monit

sudo systemctl status monit

Stop the Monit service

sudo systemctl stop monit

Restart the Monit service

sudo systemctl restart monit

Start the Monit service

sudo systemctl start monit

Step 2: Configure Monit in Ubuntu

Monit can be managed from two main directories that are:

  • Main configuration file:  /etc/monit/monitrc
  • Directories for specific process or server files: /etc/monit/conf-available and /etc/monit/conf-enabled/

We will access the main configuration directory using any of the desired editors:

sudo nano /etc/monit/monitrc

There we will see the following:

In this file, it will be possible to add more process options to manage, create email alerts or enable HTTPD.

To enable the integrated HTTP interface through which you can see the status of monitored services and manage services from a web interface, we must locate the following line:

set httpd port 2812 and

There we uncomment (remove the sign #) to the following lines:

set httpd port 2812 and 
	 use address localhost   # only accept connection from localhost
	 allow localhost	     # allow localhost to connect to the server and
	 allow admin : monit	   # require user 'admin' with password 'monit'

Save the changes using the Ctrl + O keys and leave it using the Ctrl + X keys. Now, we are going to configure Monit to monitor some Linux applications.

For example, to manage Apache, we will execute the following line:

cat /etc/monit/conf-available/apache2

We can see details of the parameters that will be analyzed.

If we want to monitor OpenVPN, we will execute the following line:

sudo nano /etc/monit/conf-available/openvpn

This will create a new file where we will paste the following:

check process openvpn with pidfile /var/run/openvpn/server.pid
   group nogroup
   start program = "/etc/init.d/openvpn start"
   stop program = "/etc/init.d/openvpn stop"
   if failed host localhost port 1194 then restart
   if 5 restarts with 5 cycles then timeout
   depend on openvpn_bin

check file openvpn_bin with path /usr/sbin/openvpn
   group nogroup
   include /etc/monit/templates/rootbin

Save the changes in the file with the Ctrl + O keys and exit with Ctrl + X. Now, let's enable it by running:

sudo ln -s /etc/monit/conf-available/openvpn /etc/monit/conf-enabled/

Whenever we perform a new task, we must execute the following command to verify that everything works correctly:

monit -t

We proceed to restart the service by running one of the following lines:

sudo /etc/init.d/monit reload
sudo systemctl reload monit

To know the current state of a process, in this case, OpenVPN, we will run the following:

sudo monit status openvpn

At the moment we want to access the Monit logs, simply execute the following line:

sudo tail -f /var/log/monit.log

Monit is a practical alternative for the correct management of each application that we install in Linux and thus have the capacity of a centralized administration.

Similar Posts

Leave a Reply

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