Use Monit to Monitor + Restart php7-fpm Service Automatically

Monit can be used to monitor your services on your VPS or dedicated server. You can use Monit to make sure php7-fpm is always running in case it halts or freezes up – this is very rare but it is better to be safe than sorry.

Monit will check the php7-fpm process by looking at the socket it is listening on. If the socket cannot be detected Monit will restart the php7-fpm service. This way you ensure your web site, perhaps running WordPress, is always running essential processes. Cloudways uses this monitoring technique with Monit on all of its managed VPS.

If you need to install Monit on Ubuntu or Debian then follow this guide.

Use Monit to Monitor + Restart PHP7-FPM Service Automatically

Monit needs some information to check to see if php7-fpm is healthy.

We will use the socket php7-fpm is using and its pid file.

Find php7-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.0/fpm/pool.d/www.conf

If you see this output then php7-fpm is using unix sockets

listen = /run/php/php7.0-fpm.sock

If you see this output then php7-fpm is using TCP sockets, the loopback interface 127.0.0.1 on port 9000

listen = 127.0.0.1:9000

Find php7-fpm pid file

Monit needs the pid file as well

sudo find /run -iname php*.pid

You should see some output like this

/run/php/php7.0-fpm.pid

Now we have the information necessary to configure Monit for php7-fpm monitoring.

Configure Monit to Monitor php7-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.

sudo nano /etc/monit/conf.d/php7-fpm

This Monit php7-fpm configuration is for unix sockets.

It checks for the php7-fpm.pid file and if it doesn’t exist Monit will try to restart it.

Monit is also going to check that the php7-fpm unix socket exists: /var/run/php7.0-fpm.sock

check process php7-fpm with pidfile /run/php/7.0/php7-fpm.pid
    start program = "/usr/sbin/service php7.0-fpm start" with timeout 60 seconds
    stop program  = "/usr/sbin/service php7.0-fpm stop"
    if failed unixsocket /var/run/php7.0-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 9000, if not found Monit will restart php7-fpm after 3 CPU cycles.

Change 127.0.0.1 to match your interface and 9000 if it was different when you checked earlier.

check process php7-fpm with pidfile /run/php/7.0/php7-fpm.pid
    start program = "/usr/sbin/service php7.0-fpm start" with timeout 60 seconds
    stop program  = "/usr/sbin/service php7.0-fpm stop"
    if failed host 127.0.0.1 port 9000 type tcp for 3 cycles then restart

Check the Monit configuration has valid syntax

sudo 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 service monit reload

You can check your php7-fpm Monit status on port 2812 or whichever port you have specified Monit should run on.

If you want email alerts from Monit via Mailgun alerts follow this guide. For Monit emails via Sendgrid follow this guide.

Now your php7-fpm service will automatically restart if it ever fails.

1 thought on “Use Monit to Monitor + Restart php7-fpm Service Automatically”

Comments are closed.