Nginx monitoring with Monit

Monit is a powerful open-source tool used for monitoring and managing Unix-like systems. It can help you ensure the availability…

Nginx monitoring with Monit
Photo by Nicolas Gonzalez on Unsplash
Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

Monit is a powerful open-source tool used for monitoring and managing Unix-like systems. It can help you ensure the availability, performance, and security of your servers by monitoring various system resources and services. In this tutorial, we will walk through the process of installing and configuring Monit to monitor a sample service (Nginx) on a Linux system.

Step 1: Installation

  1. Open a terminal on your Linux system.
  2. Install Monit using your package manager. For example, on Debian-based systems, you can use the following command:
sudo apt-get install monit

3. Start and enable Monit to run on system boot:

sudo systemctl start monit 
sudo systemctl enable monit

Step 2: Nginx monitoring Configuration

  1. Navigate to the Monit configuration directory:
cd /etc/monit/conf-enabled

2.Create a new configuration file for the service you want to monitor. In this example, we’ll monitor Nginx:

sudo nano nginx-monit.conf

3. Add the following configuration to the nginx-monit.conf file. This example assumes Nginx is running on port 80. Adjust the configuration according to your needs.

check process nginx with pidfile /var/run/nginx.pid 
  start program = "/etc/init.d/nginx start" 
  stop program = "/etc/init.d/nginx stop" 
  if failed host 127.0.0.1 port 80 then restart 
  if 5 restarts within 5 cycles then timeout

4.Save and exit the text editor.

Step 3: Verification

  1. Verify the Monit configuration syntax:
sudo monit -t

2. If the syntax is valid, restart Monit to apply the configuration changes:

sudo systemctl restart monit

Step 4: Monitoring

  1. Access the Monit web interface by opening your web browser and navigating to http://localhost:2812.
  2. Log in using the Monit credentials configured in the /etc/monit/monitrc file.
  3. In the web interface, you should see the Nginx service listed. Monit will automatically start monitoring the service based on the configuration.
  4. You can manually trigger actions like starting or stopping the service directly from the web interface.

Step 5: Alerts

  1. Configure Monit to send alerts. Edit the /etc/monit/monitrc file and locate the set alert section.
  2. Add your email address to receive alerts:
set alert your_email@example.com

3. Adjust your SMTP server configuration

set mailserver smtp.example.com port 587 
    username "USERNAME" password "PASSWORD" 
    using TLSV12 with timeout 30 seconds

3. Save and exit the file.

Step 6: Testing

  1. To test Monit’s functionality, stop the Nginx service manually:
sudo systemctl stop nginx

2. Wait for a few moments, and Monit will detect the service failure and attempt to restart it.

Conclusion

Congratulations! You’ve successfully configured Monit to monitor and manage the Nginx service. Monit can be further configured to monitor other services, system resources, and perform more complex tasks. Remember that this tutorial provides a basic example, and Monit offers a wide range of features to help you ensure the stability and health of your server infrastructure.

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…