Monit: Monitor web server status (Apache, NGINX, and MySQL)

Monit allows you to monitor web server status. If you are running a home server or a large-scale Linux server, you may also be using Apache and MySQL for serving websites. Or you may just be using Apache for reverse proxies and MySQL as Kodi library. Setting up reverse proxies for apps such CouchPotato and Transmission, make it easy to access them. MySQL, apart from storing data from dynamic websites, can also keep you media library synced on all Kodi devices. What if your Apache fails and you are unable to access your apps or MySQL fails and your Kodi media don't play? Monit is an automatic monitoring, maintenance, and repair utility for Unix systems. If your Apache or MySQL server fails, Monit can send you an email alert. It can even try to restart Apache and MySQL. In this Monit tutorial, I will describe how to 1) monitor Apache server with Monit, 2), monitor NGINX server with Monit, and 3) monitor MySQL server with Monit. I am assuming that you have already installed and configured Monit following my previous guide.

Monitor Web Server Status with Monit

On my Ubuntu home server, I run Apache server and MySQL server for reasons described above. On my Ubuntu VPS that runs this website, I run NGINX. If one of them fails, I would not know until I try something (say play a video or access CouchPotato) and it fails. If my NGINX server is down, I won't know until I run a web site status check. Monit can notify server failures as soon as it happens. When I recently setup my new home server, I decided to use Monit for system monitoring. I use Monit to monitor several services listed below:

Monitor your home server with Monit:

Since setting it up I have never had one Kodi streaming problem. So let us see how to automatically manage web server using Monit server monitor software.

Before enabling web server monitoring, it is required that you have a working Monit instance with a proper /etc/monit/monitrc file. Monit configurations for various services are loaded from /etc/monit/conf.d folder.

Monitor Apache Server with Monit

Setting up Apache server monitoring with Monit is with Monit's pre-made configuration templates. All you have to do is copy the existing template from /etc/monit/monitrc.d to /etc/monit/conf.d folder.

sudo cp /etc/monit/monitrc.d/apache2 /etc/monit/conf.d/

Instead of copying, you may also create a symbolic link. Apache web server process creates apache2.pid. The above code monitor apache2.pid file and if does not exist, Monit will try to restart Apache. A restart will trigger an email alert as shown in the example below. If restart fails multiple times then Monit stops monitoring Apache server.

Monit System Monitoring Email Alert
Monit System Monitoring Email Alert Example

Monitor NGINX Server with Monit

Monit also provides templates for for NGINX server monitoring. Once again, all you have to do is copy the existing template from /etc/monit/monitrc.d to /etc/monit/conf.d folder.

sudo cp /etc/monit/monitrc.d/nginx /etc/monit/conf.d/

Instead of copying, you may also create a symbolic link. NGINX web server process creates nginx.pid. The above code monitor nginx.pid file and if does not exist, Monit will try to restart NGINX Server. A restart will trigger an email alert as shown in the example above. If restart fails multiple times then Monit stops monitoring NGINX server.

Monitor MySQL Server with Monit

Monit also provides templates for MySQL server monitoring. Once again, all you have to do is copy the existing template from /etc/monit/monitrc.d to /etc/monit/conf.d folder.

sudo cp /etc/monit/monitrc.d/mysql /etc/monit/conf.d/

Instead of copying, you may also create a symbolic link. MySQL database server process creates mysqld.pid. The above code monitor mysqld.pid file and if does not exist, Monit will try to restart MySQL Server. A restart will trigger an email alert as shown in the example above. If restart fails multiple times then Monit stops monitoring MySQL server.

Test and Reload Monit

Once you make any changes you have to test Monit configuration:

sudo monit -t

You should see the following message: Control File Syntax OK. Then, check to see if Monit is already running using the following command:

sudo /etc/init.d/monit status

If Monit is running, reload configurations using the following command to monitor web server status with Monit:

sudo /etc/init.d/monit reload

If Monit is not running, then start it using sudo monit command instead. The whole sequence of commands for testing and reloading Monit is shown in the picture below.

Monit Test And Reload
Monit Test And Reload

Now, fire up your web browser and visit one of the following URLs depending on how your Monit is configured (be sure to use the correct port number):

  • http://localhost:2812
  • http://IPADDRESS:2812 (local network IP)
  • http://domain.com:2812 (if you have domain name pointing to your server)

You should see Apache and MySQL web server statuses as shown in the picture below (NGINX is not shown in this example picture).

Monitor Web Server Status With Monit - Apache And Mysql
Monitor Apache And Mysql With Monit

That is it to monitor web server status with Monit. As you can see Monit allows for automatic web server status monitoring, which can be a big help for system administrators. Monit Wiki page has several examples. More home server specific Monit examples to follow, so keep checking back.

Be the 1 in 200,000. Help us sustain what we do.
110 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.

Try Deployarr