Managing server is not an easy job, specially if you have multiple softwares like MySQL, Nginx or Apache running on your servers. Downtime happens and running softwares get killed due to many reasons. In this post, I will share how I use Monit to constantly monitor programs running on my servers and automatically get them restarted when they go down. Monit also enables me to remotely watch the system status.
Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute actions in error situations.
Additionally, Monit has a built-in lightweight HTTP(S) interface you can use to browse the Monit server and check the status of all monitored services. From the web-interface you can start, stop and restart processes and disable or enable monitoring of services.
Installing and Configuring Monit on Ubuntu
We can install monit by simply running the following command
It is very easy to configure Monit. I will show the very basic configurations required to enable the Monit Web interface and to monitor programs like MySQL, Nginx and Apache. Run the following command to open Monit configuration file in Vim editor:
Enabling Web Interface
The first thing we will do is enable the Web Interface for Monit, so that we can access it from anywhere. To configure the web interface, find and uncomment the section that begins with
set httpd port 2812. If you wish, you can change the default port at which Monit web interface will be accessible. We are going to keep it same. Additionally, we will set a username and password for the web interface. You can also decide to give Read-Write or Read Only permissions based on user groups.
Make sure to enable port 2812 on your server .
Monitoring and Automatically restarting MySQL with Monit
Add the following lines in Monit configuration file, for enabling monitoring of MySQL. Monit will restart MySQL whenever it is unable to find MySQL running.
Note that, you can add additional action and checks which I am not showing here. The above piece of configuration is sufficient to keep a watch on MySQL and restarting it in case of process termination.
Monitoring and Automatically restarting Nginx with Monit
Add the following lines in Monit configuration file, for enabling monitoring of Nginx. Monit will restart Nginx whenever it is unable to find a process for Nginx running.
Monitoring and Automatically restarting Apache with Monit
Add the following lines in Monit configuration file, for enabling monitoring of Apache. Monit will restart Apache whenever it is unable to find Apache running.
Note that, you can add additional check and actions for Apache too. Consult the Monit Documentation for full set of actions and checks possible.
Once you are done with editing the configuration file, all that is left is to start Monit/reload configuration and start monitoring of all services via the following commands.
That’s it. You should have Monit running and monitoring all the configured programs. You can check the status of Monit using the following command.
Or simply connect to your server on port 2812 to access the Web Interface.
Monit to the Rescue
I manage multiple servers, most of which are running WordPress which I have configured on Nginx and MySQL. Monit is a crucial tool for me to keep my websites and webapps running even when occasionally MySQL or some other service crashes. Of-course, this doesn’t mean that, you should not investigate the root cause of such crashes, but Monit will at-least help you keep your sanity while you are doing so. :)