How To Install Monit on Ubuntu 24.04 LTS
In this tutorial, we will show you how to install Monit on Ubuntu 24.04 LTS. Monit is a lightweight, cross-platform monitoring tool that provides a simple and efficient way to manage and monitor processes, files, directories, and filesystems on Unix-like operating systems. It can automatically restart services that have crashed or failed, send alerts via email or SMS, and generate detailed reports on system health and performance.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Monit monitoring tool on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
- An Ubuntu 24.04 system with root access or a user with sudo privileges.
Install Monit on Ubuntu 24.04 LTS
Step 1. Updating the Package Repository.
Updating your system ensures that all existing packages are up-to-date, which can prevent potential conflicts during the installation process.
sudo apt update sudo apt upgrade
The apt update
command fetches the latest package information from the configured sources while apt upgrade
installing available upgrades for all packages currently installed on the system.
Step 2. Installing Monit on Ubuntu 24.04.
Install Monit from the official Ubuntu repository:
sudo apt install monit
Once the installation is complete, verify that Monit was installed correctly by checking its version:
monit -V
This command will display the version of Monit installed on your system.
Step 3. Configuring Monit.
With Monit installed, it’s time to configure it to suit your specific monitoring needs. The main configuration file for Monit is located at /etc/monit/monitrc
. Let’s make some basic configurations to get you started.
Open the configuration file using your preferred text editor:
sudo nano /etc/monit/monitrc
First, let’s enable the web interface for easy management. Find the following lines and uncomment them (remove the # at the beginning of each line):
set httpd port 2812 and use address localhost allow localhost
To secure the web interface, add authentication by including these lines:
allow admin:your_password
Configure email alerts by adding or modifying the following lines:
set mailserver smtp.gmail.com port 587 username "your_email@gmail.com" password "your_email_password" using tlsv12 set alert your_email@gmail.com
Save the file and exit the text editor, then test your configuration for syntax errors:
sudo monit -t
Now that we have Monit configured, let’s start the service and enable it to run on system boot.
sudo systemctl start monit
Enable Monit to start automatically on system boot:
sudo systemctl enable monit
Step 4. Configure Firewall.
If you’re using a firewall, ensure that port 2812 is open:
sudo ufw allow 2812/tcp
Step 5. Accessing the Monit web interface.
Now open your web browser to access the Monit web interface via the URL http://your-server-ip-2812
. You will be prompted to enter a Monit admin username and password (see below):
Step 6. Monitoring Services with Monit.
Monit excels at monitoring and managing services. Let’s set up monitoring for some common services.
Monitoring Apache Web Server.
Monitoring Apache Web Server
Create a configuration file for Apache:
sudo nano /etc/monit/conf.d/apache2
Add the following content:
check process apache2 with pidfile /var/run/apache2/apache2.pid start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if cpu usage > 95% for 3 cycles then restart if memory usage > 200 MB for 5 cycles then restart if failed host localhost port 80 protocol http then restart
Monitoring MySQL Database
Create a configuration file for MySQL:
sudo nano /etc/monit/conf.d/mysql
Add the following content:
check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if cpu usage > 90% for 5 cycles then restart if memory usage > 500 MB for 5 cycles then restart if failed host localhost port 3306 then restart
Save the files and reload Monit:
sudo monit reload
Step 7. Troubleshooting Common Issues.
Even with careful setup, you might encounter some issues. Here are solutions to common problems:
-
- Monit fails to start:
- Check the Monit log file:
sudo tail -f /var/log/monit.log
- Verify the syntax of your configuration files:
sudo monit -t
- Check the Monit log file:
- Web interface access problems:
- Ensure the web interface is enabled in the configuration.
- Check firewall settings to allow access to port 2812.
- Email alert configuration issues:
- Verify your SMTP server settings.
- Check your email credentials and ensure they’re correctly entered in the configuration.
- Resource monitoring inaccuracies:
- Adjust the monitoring thresholds based on your system’s typical performance.
- Increase the number of cycles before an alert is triggered to avoid false positives.
- Monit fails to start:
Congratulations! You have successfully installed Monit. Thanks for using this tutorial for installing the Monit monitoring tool on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the Monit website.