This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies.
In order to follow this guide, you should have a server with Ubuntu 18.04.4 LTS installed with 1GB minimal memory and 10GB minimal hard drive, access to sudo privileges, php7.4, nginx and mysql.
Monit is an excellent tool to check your server’s services uptime, Monit is “Pro-active Monitoring” used on VPS or dedicated server. Monit will make sure php7.4-fpm and any server web services are always running in case it halts or freezes up.
Monit will check the php7.4-fpm, Nginx and MySQL processes by looking at the PID they are listening on and if the PID can not be found or detected Monit will find the services and restart them.
Install
apt-based:
apt-get install monit
Config
Monit Config File
nano /etc/monit/monitrc
Use Monit to Monitor + Restart PHP7.4-FPM Service Automatically
Monit needs some information to check to see if php7.4-fpm is healthy.
We will use the socket php7-fpm is using and its PID file.
Find php7.4-fpm Unix or TCP Socket
php7-fpm can listen on either Unix sockets or TCP sockets. To find out use this command
grep "listen =" /etc/php/7.4/fpm/pool.d/www.conf
If you see this output then php7.4-fpm is using Unix sockets
listen = /run/php/php7.4-fpm.sock
If you see this output then php7.4-fpm is using TCP sockets, the loopback interface 127.0.0.1 on port 9090
listen = 127.0.0.1:9090
Find php7-fpm pid file
Monit needs the PID file as well
find /run -iname php*.pid
You should see some output like this
/run/php/php7.4-fpm.pid
Now we have the information necessary to configure Monit for php7-fpm monitoring.
Configure Monit to Monitor php7.4-fpm
Create a Monit php7-fpm configuration, you may use the conf-enabled folder instead of conf.d, check your /etc/monit/monitrc file to verify.
nano /etc/monit/conf.d/php7.4-fpm
This Monit php7.4-fpm configuration is for Unix sockets.
It checks for the php7.4-fpm.pid file and if it doesn’t exist Monit will try to restart it.
Monit is also going to check that the php7.4-fpm unix socket exists: /var/run/php7.4-fpm.sock
check process php7.4-fpm with pidfile /run/php/php7.4-fpm.pid start program = "/usr/sbin/service php7.4-fpm start" with timeout 60 seconds stop program = "/usr/sbin/service php7.4-fpm stop" if failed unixsocket /var/run/php7.4-fpm.sock then restart
If you are using TCP sockets then this will be your Monit configuration.
Monit looks for a daemon listening on 127.0.0.1 on port 9090, if not found Monit will restart php7-fpm after 3 CPU cycles.
Change 127.0.0.1 to match your interface and 9090 if it was different when you checked earlier.
check process php7.4-fpm with pidfile /run/php/php7.4-fpm.pid start program = "/usr/sbin/service php7.4-fpm start" with timeout 60 seconds stop program = "/usr/sbin/service php7.4-fpm stop" if failed host 127.0.0.1 port 9090 type tcp for 3 cycles then restart
System Monitoring
#System Monitoring
Process 'mysqld' status Running monitoring status Monitored pid 1619 parent pid 1 uid 112 effective uid 112 gid 116 uptime 1h 29m threads 37 children 0 memory 148.0 MB memory total 148.0 MB memory percent 14.9 memory percent total 14.9 cpu percent 0.0 cpu percent total 0.0 data collected Tue, 20 Nov 2018 12:29:01<
Process 'nginx' status Running monitoring status Monitored pid 1570 parent pid 1 uid 0 effective uid 0 gid 0 uptime 1h 29m threads 1 children 2 memory 10.4 MB memory total 30.1 MB memory percent 1.1 memory percent total 3.0 cpu percent 0.0 cpu percent total 0.0 data collected Tue, 20 Nov 2018 12:29:01
System 'nuno-sarmento.com/' status Running monitoring status Monitored load average [0.02] [0.10] [0.20] memory usage 445.4 MB swap usage 0 data collected Tue, 20 Nov 2018 12:29:01
Process 'php7.4-fpm' status Running monitoring status Monitored pid 1241 parent pid 1 uid 0 effective uid 0 gid 0 uptime 1h 29m threads 1 children 0 memory 34.5 MB memory total 34.5 MB memory percent 3.5 memory percent total 3.5 cpu percent 0.0 cpu percent total 0.0 port response time 0.077 ms to [127.0.0.1]:9090 type TCP/IP protocol DEFAULT data collected Tue, 20 Nov 2018 12:29:01
Nginx
#Nginx check process nginx with pidfile "/var/run/nginx.pid" if cpu > 80 for 2 cycles then alert
MySqld
#MySQL Monitoring check process mysqld with pidfile "/var/run/mysqld/mysqld.pid" if cpu > 80 for 2 cycles then alert
Check the Monit configuration has valid syntax
monit -t
You should see this message indicating the Monit syntax is OK.
Control file syntax OK
Then reload Monit to activate the configuration
sudo monit reload
Verify
Run command monit status, it should something like:
The Monit daemon 5.13 uptime: 2m System 'example.com' status Running monitoring status Monitored load average [0.48] [0.40] [0.81] cpu 0.0 memory usage 8276900 kB swap usage 1192 kB data collected Thu, 03 Apr 2014 10:41:45 #many more lines...
You may also open example.com:2182 in your browser to use web-based interface.
Monit-Graph
You can use commercial version M/Monit for a lot many features or free script monit-graph
Install monit-graph
cd /var/www/example.com/htdocs git clone https://github.com/danschultzer/monit-graph mg cd mg chmod -R 0777 data chmod 0644 data/index.php
Config File
Open config.php and adjust following values:
"url" => "localhost:2812", "url_ssl" => false, "http_username" => "youruser", "http_password" => "yourpassword",
Setup Cron
Add following line to cron:
* * * * * php /var/www/example.com/htdocs/mg/cron.php >> /var/log/monit-graph.log
View Graphs
You can open http://example.com/mg in browser to see visual graphs!