How to Configure Monit to Send Email Alerts with Mailgun

Monit is an awesome tool for monitoring your WordPress server. It can ensure your web server like nginx or Apache is always running as well as other crucial services. Monit will also send you email alerts. For maximum security you should not host an email server by yourself. Especially when there are free services like Mailgun that allow you to send Monit email alerts when server load is high or a service crashes.

This guide assumes you have already installed and configured Monit.

How to Configure Monit to Send Email Alerts with Mailgun

You will need your Mailgun SMTP password for the postmaster user. It can be found under your Mailgun Domains -> Domain Name -> Domain Information.

Open up your Monit configuration

sudo nano /etc/monit/monitrc

Add the blue section below to your already working Monit configuration.

Replace postmaster@mg.yourdomain.com with your Mailgun domain and your-smtp-password with the postmaster’s password.

Set webmaster@wp-bullet.com to the email address you want to receive Monit email alerts from Mailgun.

set daemon 60 #check services ever 60 seconds
  set logfile /var/log/monit.log
  set idfile /var/lib/monit/id
  set statefile /var/lib/monit/state

#Event queue
  set eventqueue
      basedir /var/lib/monit/events # set the base directory where events will be stored
      slots 100                     # optionally limit the queue size

#Mail settings
set mail-format {
  from: monit@wp-bullet.com
  subject: monit alert --  $EVENT
  message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

           Your faithful employee,
           Monit }
set mailserver smtp.mailgun.org port 587
  username postmaster@mg.yourdomain.com password "your-smtp-password"
  using TLSV1 with timeout 30 seconds
set alert webmaster@wp-bullet.com #email address which will receive monit alerts

#http settings
set httpd port 2812 address 0.0.0.0  # allow connections on all adapters
    ssl enable
    pemfile  /var/certs/monit.pem
#    allow 0.0.0.0/0.0.0.0 # allow all IPs, can use local subnet too
#    allow wp-bullet.crabdance.com        # whitelist allow dynamicdns address to connect
    allow admin:wp-bullet      # require user 'admin' with password 'wp-bullet'

#allow modular structure
    include /etc/monit/conf.d/*

Check the Monit syntax is OK.

sudo monit -t

You should see this indicating Monit can restart successfully

Control file syntax OK

Then restart the Monit service

sudo service monit restart

To test the Monit email alerts are working, log in to Monit and restart a service.

You should get an email that the action completed.

If you have problems consider using a Mailgun SMTP relay, the monit configuration for mail then looks like this

set mailserver 127.0.0.1 port 25
set alert admin@wp-bullet.com #email address which will receive monit alerts

3 thoughts on “How to Configure Monit to Send Email Alerts with Mailgun”

Comments are closed.