# Nagios + nginx (php-fpm) + fcgiwrap



## leebrown66 (Apr 16, 2018)

NOTES - General
This is NOT a guide to configure Nagios.  Rather it shows how to configure nginx, serving PHP via php-fpm and CGI via fcgiwrap for the Nagios server.

Notes - php-fpm
The default is to use a tcp socket.  This configuration instead uses a unix socket for better performance/lower overhead.

`pkg install net-mgmt/nagios4 www/nginx www/fcgiwrap`
Using the quarterly packages on FreeBSD 11.1-RELEASE, this resulted in:
www/fcgiwrap-1.1.0_8
net-mgmt/nagios4-4.3.4_1,1
www/nginx-1.12.2_11,2

_*EDIT: I discovered later that in order for the map functionality to work I also needed to install php56-filter.  However this does not prevent Nagios from being usable.  You should check which version of php is installed though.*_

Edit these files accordingly:
/usr/local/etc/php-fpm.conf

```
#listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
```

/etc/rc.conf

```
nagios_enable="YES"
fcgiwrap_enable="YES"
fcgiwrap_user="www"
fcgiwrap_group="www"
fcgiwrap_socket_mode="0660"
fcgiwrap_socket_owner="www"
fcgiwrap_socket_group="www"
nginx_enable="YES"
php_fpm_enable="YES"
```

/usr/local/etc/nginx.conf
Inside the server declaration

```
location /nagios {
auth_basic "Restricted";
auth_basic_user_file /usr/local/www/nagios/.htpasswd;

alias /usr/local/www/nagios;
index index.php;
location ~ \.php$ {
  fastcgi_index index.php;
  fastcgi_pass unix:/var/run/php-fpm.sock;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  include fastcgi_params;
}
location ~ \.cgi$ {
  try_files $uri =404;
  include fastcgi_params;
  fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock;
  fastcgi_param SCRIPT_FILENAME $request_filename;
  fastcgi_param REMOTE_USER $remote_user;
}
}
```

`( echo -n "[B]nagiosadmin[/B]:" ; openssl passwd -salt SALT -crypt [B]admin[/B] ) > /usr/local/www/nagios/.htpasswd;
service php-fpm start
service fcgiwrap start
service nginx start
service nagios start`

visit the /nagios folder on the server, you should be asked to authenticate (user: *nagiosadmin*, passwd: *admin*).

If everything is successful, you should be seeing this.  The column on the left was generated with PHP, while the text was from CGI.






Problems
These are the files to look in:

/var/log/nginx/*.log
/var/log/php-fpm.log

You may also want to add to /etc/rc.conf
fcgiwrap_flags="-f"
which redirects errors into the web server log (/dev/null is the default).

`sockstat -l`

```
USER CMD      PROTO  LOCAL ADDRESS
www  fcgiwrap stream /var/run/fcgiwrap/fcgiwrap.sock
www  nginx    tcp4   *:80
www  php-fpm  stream /var/run/php-fpm.sock
```

`ls -l /var/run/fcgiwrap/fcgiwrap.sock /var/run/pgp-fpm.sock`

```
srwxr-xr-x www wheel /var/run/fcgiwrap/fcgiwrap.sock
srw-rw---- www www   /var/run/php-fpm.sock
```


----------



## gutiersa (May 3, 2020)

Hi, thanks so much for posting this tutorial. I actually tried this, but I am getting this error:
"nagios assertion failed: (!argv[argc]), function perl_parse, file perl.c line 1696"
I am kind of lost. I would appreciate some insight.
Thanks.


----------



## gutiersa (May 18, 2020)

The problem was that I had installed nagios version 3 rather than version 4 which was compatible with my version of FreeBSD 12.1


----------

