Install and Configure Monit Manager on Ubuntu 20.04 with Easy Steps
This guide is designed to walk you through the process of Install and Configure Monit Manager on Ubuntu 20.04. Monit is a powerful open-source utility for managing and monitoring Unix-like systems. With Monit, you can keep a close watch on daemon processes and other programs running on your local host, including those launched during system boot from directories like /etc/init/
. This allows you to monitor crucial services such as sendmail, sshd, Apache, and MySQL, ensuring they remain operational and responsive.
Let’s dive into the steps needed to Install and Configure Monit Manager on Ubuntu 20.04 for robust Unix system monitoring.
To begin setting up Monit Manager for monitoring your Unix systems, you’ll need to log in to your server as a non-root user with sudo privileges. It’s also recommended to set up a basic firewall for enhanced security. If you haven’t already done so, you can refer to our guide on Initial Server Setup with Ubuntu 20.04 for detailed instructions.
1. Install Monit on Ubuntu 20.04
Monit packages are readily available in the default Ubuntu 20.04 repository. First, update your local package index to ensure you’re working with the latest package information:
# sudo apt update
# sudo apt upgrade -y
Next, use the following command to install Monit:
sudo apt install monit -y
2. Check Monit Service Status
After the installation, the Monit service should be automatically activated on your server. To verify its status, run the following command:
sudo systemctl status monit --no-pager -l

If the service is not running, you can start it with the following command:
sudo systemctl start monit
You can also check the installed Monit version:
sudo monit --version
**Output**
This is Monit version 5.26.0
Built with ssl, with ipv6, with compression, with pam and with large files
Copyright (C) 2001-2019 Tildeslash Ltd. All Rights Reserved.
3. Configure Monit Service Manager on Ubuntu 20.04
The primary configuration file for Monit is located at /etc/monit/monitrc
. While you can directly edit this file, it’s best practice to create separate configuration files for your specific settings.
Enable Monit httpd port
By default, port 2812
, which is used for communicating with Monit’s web interface, is disabled. To enable it, open the Monit configuration file using your favorite text editor (e.g., vi):
sudo vi /etc/monit/monitrc
Locate the line that begins with set httpd port 2812
. Remove the #
symbol from the beginning of the following lines to uncomment them. If you want to access the Monit web interface remotely, replace localhost
with 0.0.0.0
for both use address
and allow
.
You can also change the default username and password ("admin" and "monit" respectively) for the web interface.
set httpd port 2812 and
use address 0.0.0.0 # only accept connection from localhost (drop if you use M/M)
allow 0.0.0.0/0 # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
Save the changes and close the file.
To verify the Monit configuration, use the following command:
sudo monit -t
**Output**
Control file syntax OK
4. Enable Monit Service Manager
By default, Monit is not enabled to start automatically on boot. To enable this, run the following command:
sudo /lib/systemd/systemd-sysv-install enable monit
To apply the changes, restart the Monit service:
sudo systemctl restart monit
You can verify that everything is working correctly with:
sudo monit status
5. Configure Firewall for Monit
You need to allow traffic on port 2812
through the UFW firewall. Run the following command:
sudo ufw allow 2812
Reload the firewall to apply the new rules:
sudo ufw reload
6. Access Monit Service Manager Web Interface
You can now access the Monit web interface by navigating to your server’s IP address followed by port 2812
in your web browser:
http://<your-server-ip-address>:2812
You will be presented with a sign-in screen. Enter the username and password you configured in the Monit configuration file and click Sign in.
After successfully logging in, you’ll see the Monit dashboard.
7. Add Services In Monit on Ubuntu 20.04
Now, you can create service files for the applications you want to monitor, or utilize the pre-configured files available.
The /etc/monit/conf-available/
directory contains ready-made configuration files for common server services, including:
acpid, at, mdadm, mysql, openntpd, pdns-recursor, rsyslog, snmpd,
apache2, cron, memcached, nginx, openssh-server, postfix and smartmontools.
To enable monitoring for a specific service, create a symbolic link from its configuration file in /etc/monit/conf-available/
to /etc/monit/conf-enabled/
.
For example, to monitor Nginx using its pre-configured file, use the following command:
sudo ln -s /etc/monit/conf-available/nginx /etc/monit/conf-enabled/
Then, reload the Monit service:
sudo monit reload
The Nginx service should now appear in your Monit service manager.
If you want to monitor a service that doesn’t have a pre-configured file, you can create one manually.
For example, to monitor system hardware sensors, you can install the lm-sensors
package:
sudo apt install lm-sensors -y
Then, create a configuration file for it:
sudo vi /etc/monit/conf-available/sensors
Add the following content to the file:
check program sensors with path /usr/bin/sensors
if status != 0 then alert
Save the changes and close the file.
Next, enable it by using the command below:
sudo ln -s /etc/monit/conf-available/sensors /etc/monit/conf-enabled/
Finally, reload the Monit service on Ubuntu 20.04:
sudo monit reload
That’s it, you are done.
Conclusion
You have now successfully learned to Install and Configure Monit Manager on Ubuntu 20.04. You’ve also learned how to add services for monitoring using Monit. This will allow you to effectively monitor your Unix systems and ensure their stability and performance.
Hope you enjoy it. Please subscribe to us on Facebook and Twitter.
Also, you may like to read the following articles:
Install Django Framework on Ubuntu 20.04
WireGuard VPN Setup Ubuntu 20.04
Install Nextcloud Ubuntu 20.04
Plesk Hosting Platform Ubuntu 20.04
Alternative Solutions for Service Monitoring on Ubuntu 20.04
While Monit is a solid choice, let’s explore two alternative solutions for service monitoring on Ubuntu 20.04. These offer different approaches and may be better suited for specific needs.
1. Using Systemd’s Built-in Monitoring Capabilities
Systemd, the system and service manager for Linux, includes built-in monitoring and restart capabilities that can be leveraged for basic service management. This approach eliminates the need for an external tool like Monit for simple use cases.
Explanation:
Systemd service units can be configured to automatically restart a service if it crashes or exits unexpectedly. This is achieved by using the Restart
and RestartSec
directives within the service unit file. Additionally, WatchdogSec
allows systemd to proactively check the health of a service.
Example:
Let’s say you want to monitor and automatically restart a service called my-app.service
. Edit the service unit file (usually located in /etc/systemd/system/
) with the following:
sudo vi /etc/systemd/system/my-app.service
Add or modify the following lines within the [Service]
section:
[Service]
# ... other directives ...
Restart=on-failure
RestartSec=5
WatchdogSec=30 # Service must notify systemd it's alive within 30 seconds
Type=notify # Needed for WatchdogSec to work properly
# Add an ExecStartPre command to set up the environment or dependencies
ExecStartPre=/path/to/your/setup_script.sh
# Add an ExecStopPost command to clean up after the service is stopped
ExecStopPost=/path/to/your/cleanup_script.sh
Explanation of Directives:
Restart=on-failure
: Specifies that the service should be restarted if it exits with a non-zero exit code. Other options includeon-success
,on-abnormal
,always
, andno
.RestartSec=5
: Specifies a 5-second delay before attempting to restart the service.WatchdogSec=30
: Enables the watchdog timer. The service must callsd_notify()
within 30 seconds to indicate it’s alive. If not, systemd will consider the service unresponsive and restart it.Type=notify
: Informs systemd to expect a notification signal from the service. This is required forWatchdogSec
to work correctly.ExecStartPre
: Commands to execute before the service starts. Useful for setting up dependencies.ExecStopPost
: Commands to execute after the service stops. Useful for cleanup.
After making these changes, reload the systemd daemon and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart my-app.service
sudo systemctl enable my-app.service #Ensure it starts on boot
Advantages:
- No additional software to install.
- Leverages existing systemd infrastructure.
- Simple to configure for basic restart scenarios.
Disadvantages:
- Limited monitoring capabilities compared to Monit.
- No web interface for monitoring status.
- Requires service to be compatible with
sd_notify()
for watchdog functionality.
2. Using Prometheus and Grafana for Comprehensive Monitoring
For a more sophisticated and scalable monitoring solution, consider using Prometheus and Grafana. Prometheus is a time-series database that collects metrics from your systems and services, while Grafana provides a powerful visualization interface for analyzing those metrics.
Explanation:
This approach involves installing Prometheus, configuring it to scrape metrics from your services, and then using Grafana to create dashboards that visualize the data. You would typically use exporters (like node_exporter
for system metrics or specific exporters for applications like MySQL or Apache) to expose the metrics in a format that Prometheus can understand.
Steps:
-
Install Prometheus and Grafana: Follow the official documentation to install Prometheus and Grafana on your Ubuntu 20.04 server.
-
Install and Configure Exporters: Install relevant exporters for the services you want to monitor (e.g.,
node_exporter
for CPU, memory, disk usage). Configure these exporters to expose metrics on specific ports. -
Configure Prometheus to Scrape Metrics: Edit the
prometheus.yml
configuration file (usually located in/etc/prometheus/
) to add scrape configurations for each exporter. For example:scrape_configs: - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] # Assuming node_exporter is running on localhost:9100 - job_name: 'my-app' static_configs: - targets: ['localhost:8080'] # Assuming your application exposes metrics on localhost:8080 and is named my-app.
-
Create Grafana Dashboards: Import pre-built dashboards or create custom dashboards in Grafana to visualize the metrics collected by Prometheus. Grafana dashboards can display graphs, charts, and alerts based on the data.
Code Example (Simplified Exporter – Application Metrics)
Assuming your application is a simple Python web server:
from prometheus_client import start_http_server, Summary
import random
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
"""A dummy function that takes some time."""
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8080)
# Generate some requests.
while True:
process_request(random.random())
This simple Python application exposes metrics that Prometheus can scrape on port 8080.
Advantages:
- Highly scalable and flexible.
- Powerful visualization capabilities with Grafana.
- Supports a wide range of exporters for various services and systems.
- Advanced alerting capabilities.
Disadvantages:
- More complex to set up and configure than Monit or systemd.
- Requires more resources than simpler solutions.
- Overkill for very basic monitoring needs.
The choice between Monit, systemd’s built-in capabilities, and Prometheus/Grafana depends on the complexity of your monitoring requirements and the resources you are willing to dedicate to the task. Install and Configure Monit Manager on Ubuntu 20.04 provides a great starting point, but these alternatives offer different trade-offs that might be more suitable for specific scenarios. Remember to choose the tool that best fits your needs and technical expertise.