PHP-FPM – Kurzanleitung

Veröffentlichung 31. Juli 2018 @ 16:01
Letzte Änderung 3. März 2024 @ 13:00

Übersicht

  • Apache Multi-Processing-Modul MPM event / worker mit mod_fast_fcgi im Allgemeinen performanter als MPM prefork mit mod_php
  • Individuelle PHP-Einstellungen für einzelne Websites durch unterschiedliche FPM-Pool-Konfigurationen möglich
  • Keine „Nebenwirkungen“ auf Sicherheit durch für andere Websites erforderliche PHP-Einstellungen, die nur global in php.ini gesetzt werden können, z. B. disable_functions

Voraussetzungen

Software

  • Apache 2.4.18 #apache2 -v
  • PHP 7.0.30 #php –version
  • Ubuntu Server 16.04.04 LTS #lsb_release -a

Ordner und Dateien

  • Konfiguration /etc/apache2/sites-available/example.conf, /etc/php/7.0/apache2/php.ini, /etc/php/7.0/fpm/pool.d/example.conf
  • Log /var/log/apache2/error.log, /var/log/apache2/error-example.log, /var/log/php7.0-fpm.log
  • Systemd /lib/systemd/system/php7.0-fpm.service

Apache / PHP von Multi-Processing-Modul MPM prefork mit mod_php auf MPM event / worker mit mod_fast_fcgi umstellen

FastCGI Process Manager PHP-FPM und Apache-Modul FastCGI installieren

apt install php7.0-fpm
#systemctl status php7.0-fpm.service
apt install libapache2-mod-fastcgi

Eigene PHP-Einstellungen in Apache-Konfiguration und php.ini-Datei finden

...
php_admin_flag xxx xxx
...
php_admin_value xxx xxx
...
php_flag xxx xxx
...
php_value xxx xxx
...
...
xxx = xxx
...

FPM-Pool-Konfigurationsdatei mit eigenem Socket anlegen, Konfiguration testen und PHP-FPM neustarten

[example]
...
listen = /run/php/php7.0-fpm-example.sock
...
php_admin_flag[xxx] = xxx
...
php_admin_value[xxx] = xxx
...
php_flag[xxx] = xxx
...
php_value[xxx] = xxx
...
php-fpm7.0 --test
systemctl reload php7.0-fpm
#systemctl status php7.0-fpm.service

Apache-Konfigurationsdatei anpassen

...
#PHP-Konfigurationsdirektiven deaktivieren:
#php_admin_flag xxx xxx
...
#php_admin_value xxx xxx
...
#php_flag xxx xxx
...
#php_value xxx xxx
...
#über Reverse-Proxy mit PHP-FPM-Pool verbinden:
<FilesMatch "\.php$">
  <If "-f %{REQUEST_FILENAME}">
    SetHandler "proxy:unix:/run/php/php7.0-fpm-example.sock|fcgi://localhost/"
  </If>
</FilesMatch>
#https://serverfault.com/questions/717481/how-can-i-fix-recurring-php-503-errors-on-my-apache-mod-proxy-fcgi-php-fpm-serve UDS does not currently support connection reuse:
#<Proxy "fcgi://localhost/" enablereuse=on max=10>
<Proxy "fcgi://localhost/">
</Proxy>
...

Apache-Module mpm_prefork und mod_php deaktivieren, mpm_event / mpm_worker, mod_proxy und mod_proxy_fcgi aktivieren

a2dismod mpm_prefork php7.0
a2enmod mpm_event proxy proxy_fcgi
#a2enmod mpm_worker proxy proxy_fcgi

Konfiguration testen und Apache neustarten

apache2ctl configtest
systemctl reload apache2 #nicht systemctl reload apache2, da MPM-Änderung
#systemctl status apache2.service

Logdateien analysieren

...
...
...

Apache / PHP von Multi-Processing-Modul MPM event / worker mit mod_fast_fcgi auf MPM prefork mit mod_php umstellen

Apache-Modul PHP installieren

apt install libapache2-mod-php7.0

Eigene PHP-Einstellungen in FPM-Pool-Konfigurationsdatei finden

...
php_admin_flag[xxx] = xxx
...
php_admin_value[xxx] = xxx
...
php_flag[xxx] = xxx
...
php_value[xxx] = xxx
...

Apache-Konfigurationsdatei anpassen

...
#PHP-Konfigurationsdirektiven (re)aktivieren:
php_admin_flag xxx xxx
...
php_admin_value xxx xxx
...
php_flag xxx xxx
...
php_value xxx xxx
...
#Verbinden mit PHP-FPM-Pool über Reverse-Proxy deaktivieren:
#<FilesMatch "\.php$">
#  <If "-f %{REQUEST_FILENAME}">
#    SetHandler "proxy:unix:/run/php/php7.0-fpm-example.sock|fcgi://localhost/"
#  </If>
#</FilesMatch>
##https://serverfault.com/questions/717481/how-can-i-fix-recurring-php-503-errors-on-my-apache-mod-proxy-fcgi-php-fpm-serve UDS does not currently support connection reuse:
##<Proxy "fcgi://localhost/" enablereuse=on max=10>
#<Proxy "fcgi://localhost/">
#</Proxy>
...

Apache-Module mpm_event / mpm_worker, mod_proxy und mod_proxy_fcgi deaktivieren, mpm_prefork und mod_php aktivieren

a2dismod mpm_event proxy proxy_fcgi
#a2dismod mpm_worker proxy proxy_fcgi
a2enmod mpm_prefork php7.0

Konfiguration testen und Apache neustarten

apache2ctl configtest
systemctl reload apache2 #nicht systemctl reload apache2, da MPM-Änderung
#systemctl status apache2.service

Logdateien analysieren

...
...

Quellen

https://httpd.apache.org/
https://httpd.apache.org/docs/2.4/mod/event.html
https://httpd.apache.org/docs/2.4/mod/mpm_common.html
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html
https://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html
https://httpd.apache.org/docs/2.4/mod/prefork.html
https://httpd.apache.org/docs/2.4/mod/worker.html
http://manpages.ubuntu.com/manpages/xenial/man8/php-fpm7.0.8.html
https://secure.php.net/manual/de/configuration.changes.modes.php
https://secure.php.net/manual/de/install.fpm.php
https://serverfault.com/questions/717481/how-can-i-fix-recurring-php-503-errors-on-my-apache-mod-proxy-fcgi-php-fpm-serve
https://wiki.apache.org/httpd/PHP-FPM