Nginx – Configure Monit to monitor PHP5-FPM on Ubuntu/Nginx setup

monitmonitoringnginxphp-fpmUbuntu

System: LEMP running on Ubuntu 14.04

I'm trying to configure the tool Monit to restart Nginx or PHP-FPM if ever there is a problem. It is correctly monitoring Nginx however under status for PHP-FPM it says "Not Monitoring". Apparently I'm having it check the wrong location.

Here is my Nginx configuration for PHP-FPM that is running on a socket:

location ~ \.php$ { ## Execute PHP scripts
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

    expires off; ## Do not cache dynamic content
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params; ## See /etc/nginx/fastcgi_params

    fastcgi_keep_conn on; #hhvm param
}

Here is my Monit configuration for both Nginx and PHP-FPM:

## Check Nginx
check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"
## Check PHP-FPM
check process php-fpm with pidfile /var/run/php-fpm/php-fpm.pid
  group www-data #change accordingly
  start program = "/etc/init.d/php5-fpm start"
  stop program  = "/etc/init.d/php5-fpm stop"
  if failed unixsocket /var/run/php-fpm/php-fpm.sock then restart
  if 3 restarts within 5 cycles then timeout

I went with the suggested start and stop recommendations despite always restarting with "service php5-fpm restart".

My group – according to /etc/php5/fpm/pool.d/www.conf – is "group = www-data". I checked my php-fpm.conf and noticed that the PID file is being created at /var/run/php5-fpm.pid so I changed that. Then Monit said "Initializing" followed by "Connection failed". Any suggestions?

Best Answer

Ok, I was able to solve it myself. There were a number of problems. "php5" needed to replace almost every instance of "php". The bad connection I was receiving was referring to the unixsocket so I had to update that as well. Also the new PID location changed it looks like with PHP5-FPM. Here is the final configuration.

## Check PHP-FPM
check process php5-fpm with pidfile /var/run/php5-fpm.pid
  group www-data #change accordingly
  start program = "/etc/init.d/php5-fpm start"
  stop program  = "/etc/init.d/php5-fpm stop"
  if failed unixsocket /var/run/php5-fpm.sock then restart
  if 3 restarts within 5 cycles then timeout