Skip to content

Installation

Ramil Valitov edited this page Aug 5, 2020 · 12 revisions

Please follow the instructions below to install and configure the template. Note: the commands that require special privileges use sudo in the examples below. You can run them with sudo under a normal user or under root.

1. On Zabbix agents

Perform the following operations on all servers with Zabbix agent and PHP-FPM from which you want to capture the data.

1.1. Install Prerequisites

Install required packages.

For apt-get based environments (Debian, Ubuntu, etc.):
user@server:~$ sudo apt-get update
user@server:~$ sudo apt-get -y install grep gawk lsof jq unzip bc

Additionally, for Debian Jessie 8.x and older (or for Ubuntu 16 Xenial and older):

user@server:~$ sudo apt-get -y install libfcgi0ldbl

Additionally, for Debian Stretch 9.x and newer (or for Ubuntu 18 Bionic and newer):

user@server:~$ sudo apt-get -y install libfcgi-bin
For yum based environments (CentOS):
user@server:~$ sudo yum check-update
user@server:~$ sudo yum install grep gawk lsof jq fcgi unzip bc

If some utilities not found and failed to install, then add EPEL repository:

user@server:~$ sudo yum install epel-release

Then try to install the required utilities again.

1.2. Install Zabbix PHP-FPM template

First, please, download the template archive: you can use either the latest published release (the latest stable version, I hope 😅) or use the active development version (that contains all the latest features and updates). Below we will download the archive to a temporary directory /tmp that usually presents in all OS. If you don't have such a directory, please, create it first.

1.2.1. To use stable release

To download a stable release, run command:

user@server:~$ curl -L $(curl -s https://api.github.com/repos/rvalitov/zabbix-php-fpm/releases/latest | grep 'zipball_' | cut -d\" -f4) --output /tmp/zabbix-php-fpm.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 33138    0 33138    0     0  49607      0 --:--:-- --:--:-- --:--:-- 18.3M

1.2.2. To use development version

To download a development version, run command:

user@server:~$ wget https://github.com/rvalitov/zabbix-php-fpm/archive/master.zip -O /tmp/zabbix-php-fpm.zip
--2020-07-03 09:15:23--  https://github.com/rvalitov/zabbix-php-fpm/archive/master.zip
Resolving github.com (github.com)... 140.82.118.4
Connecting to github.com (github.com)|140.82.118.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/rvalitov/zabbix-php-fpm/zip/master [following]
--2020-07-03 09:15:24--  https://codeload.github.com/rvalitov/zabbix-php-fpm/zip/master
Resolving codeload.github.com (codeload.github.com)... 140.82.113.9
Connecting to codeload.github.com (codeload.github.com)|140.82.113.9|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘/tmp/zabbix-php-fpm.zip’

/tmp/zabbix-php-fpm.zip      [ <=>                               ]  33.23K  --.-KB/s    in 0.09s

2020-07-03 09:15:24 (384 KB/s) - ‘/tmp/zabbix-php-fpm.zip’ saved [34030]

1.2.3. Unzip and configure

Unzip the downloaded archive:

user@server:~$ unzip -j /tmp/zabbix-php-fpm.zip "*/zabbix/*" "*/ispconfig/*" -d /tmp/zabbix-php-fpm
Archive:  /tmp/zabbix-php-fpm.zip
3f42afd59a8dd84d9ef4bcf0de5f5d789784516a
  inflating: /tmp/zabbix-php-fpm/ispconfig.patch
  inflating: /tmp/zabbix-php-fpm/php_fpm_pool.conf.master
  inflating: /tmp/zabbix-php-fpm/userparameter_php_fpm.conf
  inflating: /tmp/zabbix-php-fpm/zabbix_php_fpm_discovery.sh
  inflating: /tmp/zabbix-php-fpm/zabbix_php_fpm_status.sh
  inflating: /tmp/zabbix-php-fpm/zabbix_php_fpm_template_4.0.xml

Copy the required files to the Zabbix agent configuration directory:

user@server:~$ sudo cp /tmp/zabbix-php-fpm/userparameter_php_fpm.conf $(find /etc/zabbix/ -name zabbix_agentd*.d -type d | head -n1)
user@server:~$ sudo cp /tmp/zabbix-php-fpm/zabbix_php_fpm_discovery.sh /etc/zabbix/
user@server:~$ sudo cp /tmp/zabbix-php-fpm/zabbix_php_fpm_status.sh /etc/zabbix/

Configure access rights:

user@server:~$ sudo chmod +x /etc/zabbix/zabbix_php_fpm_discovery.sh
user@server:~$ sudo chmod +x /etc/zabbix/zabbix_php_fpm_status.sh

1.3. Root privileges

Automatic detection of pools and accessing sockets require root privileges. We need to allow Zabbix use sudo for our scripts.

Edit the /etc/sudoers file by running command:

user@server:~$ sudo visudo

Add the following line to this file:

zabbix ALL = NOPASSWD: /etc/zabbix/zabbix_php_fpm_discovery.sh,/etc/zabbix/zabbix_php_fpm_status.sh

Here we specified zabbix as the user in the system used to run the Zabbix Agent. zabbix is the default name, but if you have a custom installation with a different name, then please, change it accordingly. Save and exit the editor. Your modifications will be applied.

If you have SELinux enabled, please disable it, otherwise everything we've done with visudo will be ignored. You can disable SELinux using the following command:

user@server:~$ sudo setenforce 0

Alternatively, you can configure SELinux to handle our modifications to sudoers, but this is beyond the scope of this manual. If you achieve success in this area, please, share it to update this manual.

That's all.

1.4. Linux Tuning (optional)

Usually PHP-FPM backlog option is limited by Linux kernel settings and equals to 128 by default. In most cases you want to increase this value (latest PHP use 511 by default). The main option that limits the PHP-FPM backlog option is net.core.somaxconn. See the current setting, usually it's 128:

user@server:~$ cat /proc/sys/net/core/somaxconn
128

Let's increase it to 1024:

user@server:~$ echo "net.core.somaxconn=1024" | sudo tee -a /etc/sysctl.conf

Now we can cause the settings to be loaded by running:

user@server:~$ sudo sysctl -p

1.5. Adjust ISPConfig

Follow this step only if you use ISPConfig. ISPConfig does not enable PHP-FPM status page by default. We will enable it by making an override of original template of ISPConfig and add a custom directive there. Please, check the installation path of ISPConfig in your system. Below we use default paths as used in Debian 9/10. Please, use one of the methods below to adjust the settings of ISPConfig.

Note: every time you upgrade the ISPConfig you may want to perform the operations below again to use the latest PHP-FPM template shipped with ISPConfig.

1.5.1. Method #1. Apply a patch

Caution: don't use this method if you already have your own customizations of the PHP-FPM template in ISPConfig.

Apply the patch for PHP-FPM the php_fpm_pool.conf.master file using the following command:

user@server:~$ sudo patch /usr/local/ispconfig/server/conf/php_fpm_pool.conf.master --input=/tmp/zabbix-php-fpm/php-fpm.patch --output=/usr/local/ispconfig/server/conf-custom/php_fpm_pool.conf.master --reject-file=-

Apply the second patch for NGINX for the nginx_vhost.conf.master file using the following command:

user@server:~$ sudo patch /usr/local/ispconfig/server/conf/nginx_vhost.conf.master --input=/tmp/zabbix-php-fpm/nginx.patch --output=/usr/local/ispconfig/server/conf-custom/nginx_vhost.conf.master --reject-file=-

Patch for Apache is not required.

1.5.2. Method #2. Manually adjust the template

Use this method if any of the statements below are true:

  • the patch above does not work
  • you already have your own customizations of the PHP-FPM or NGINX template in ISPConfig
  • you prefer to have a full control of what happens on your server.

First we need to copy the original PHP-FPM template file php_fpm_pool.conf.master of ISPConfig to the override directory (don't do that if you already have your own customizations of the PHP-FPM template in ISPConfig - in this case you should already have the specified configuration file in the specified location):

user@server:~$ sudo cp /usr/local/ispconfig/server/conf/php_fpm_pool.conf.master /usr/local/ispconfig/server/conf-custom/

Edit the copied file /usr/local/ispconfig/server/conf-custom/php_fpm_pool.conf.master and add there the following line after the last pm setting:

pm.status_path = /php-fpm-status

In our version of ISPConfig the last pm setting is pm.max_requests, so the resulting part of the file will have the following contents (the new line is bold):

<tmpl_if name='pm' op='==' value='ondemand'>
pm.process_idle_timeout = <tmpl_var name='pm_process_idle_timeout'>s;
</tmpl_if>
pm.max_requests = <tmpl_var name='pm_max_requests'>
pm.status_path = /php-fpm-status

chdir = /
<tmpl_if name='php_fpm_chroot'>

Finally, we should update the NGINX template: copy the original template file nginx_vhost.conf.master of ISPConfig to the override directory (don't do that if you already have your own customizations of the NGINX template in ISPConfig - in this case you should already have the specified configuration file in the specified location):

user@server:~$ sudo cp /usr/local/ispconfig/server/conf/nginx_vhost.conf.master /usr/local/ispconfig/server/conf-custom/

Edit the copied file /usr/local/ispconfig/server/conf-custom/nginx_vhost.conf.master and find the following line:

location ~ \.php$ {
    try_files <tmpl_var name='rnd_php_dummy_file'> @php;
}

Replace it with the following line:

location ~ (\.php|^/php-fpm-status)$ {
    try_files <tmpl_var name='rnd_php_dummy_file'> @php;
}

The value /php-fpm-status used above is the default (recommended) value used for PHP-FPM status path. If you use a different status path, please set it here.

1.5.3. Final adjustments for ISPConfig

Set correct access rights:

user@server:~$ sudo chmod +x /usr/local/ispconfig/server/conf-custom/php_fpm_pool.conf.master
user@server:~$ sudo chmod +x /usr/local/ispconfig/server/conf-custom/nginx_vhost.conf.master

Now resync the websites using ISPConfig control panel: go to "Tools"->"Sync Tools"->"Resync". Check "Websites" only and click "Start":

ISPConfig resync interface

1.6. Adjust PHP-FPM pools and web server configuration

Follow this step if you don't use ISPConfig. First you need to enable the PHP-FPM status page for all of your pools manually. Each pool must have the same status path, recommended value is /php-fpm-status. Please, edit all the pools' configuration files (for example for PHP 7.3 they are located in directory /etc/php/7.3/fpm/pool.d) by adding the following line:

pm.status_path = /php-fpm-status

You can set another path here if needed. Finally, restart the PHP-FPM, for example:

user@server:~$ sudo service php7.3-fpm restart

If you use NGINX as your web server, then you need to update the configuration of your websites, because NGINX does handle the PHP-FPM status page by default. You need to add the PHP-FPM status path (/php-fpm-status by default) to the location that matches the PHP. NGINX is very flexible and there are different ways to achieve the same results. So below is a possible example of solution, it's not a strict "the one and only" way. A location for processing PHP files in NGINX may look as follows:

location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/lib/php7.3-fpm/web1.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
}

In the example above fastcgi_pass points to a unix socket used to communicate with PHP-FPM for this website. It looks differently if you call PHP-FPM by IP address. The inner contents of the NGINX location above may look differently in your case. What is important: the location that you have provides correct functioning of PHP-FPM with NGINX on your server. What you need to do is to modify the regexp location condition to match the PHP-FPM status path; for the example above change:

location ~ \.php$ {

to

location ~ (\.php|^/php-fpm-status)$ {

Finally, restart NGINX service:

user@server:~$ sudo service nginx restart

Note: no special modifications required for Apache web server.

1.7. Clean up

Delete temporary files:

user@server:~$ rm /tmp/zabbix-php-fpm.zip
user@server:~$ rm -rf /tmp/zabbix-php-fpm/

2. On Zabbix Server

2.1. Import Zabbix PHP-FPM template

Download this project's archive to your computer (the release must be the same you selected when installing template archive at Zabbix agent):

  • To use a stable release, open the latest release page and click on "Source code (zip)" button at the end of the page.
  • To use a development version, download this archive.

Extract the XML template file from the archive that corresponds to your version of Zabbix server. For example, use file /zabbix/zabbix_php_fpm_template_4.0.xml for Zabbix server 4.0. If there's no version of the template that matches your version of Zabbix server, then try to use the nearest version of the template that is not higher than your version of Zabbix server. For example, template version 4.0 also works for higher versions of Zabbix server, such as 4.2 and 4.4. However, template version 4.0 will not work for Zabbix 3.x. Upload the extracted file to your Zabbix server. To do so go to "Configuration"->"Templates"->"Import" in Zabbix frontend: Zabbix template import interface

2.2. Add the template to your hosts

Add the template "Template App PHP-FPM" to the desired hosts. If you use a custom status path (the default is /php-fpm-status), then configure it in the macros section of the host by adding value:

{$PHP_FPM_STATUS_URL}=your status path

The setup is complete, just wait a couple of minutes till Zabbix discovers all your pools and captures the data.