SDB:Apache FastCGI and PHP-FPM configuration

Jump to: navigation, search

Why PHP-FPM

php-fpm is harder to configure than apache2-mod_php, but it provides some advantages:

  1. Support event mode (mod_php only supports prefork mode)
  2. Support HTTP2
  3. Better performance

How to

Disable apache2-mod_php7:

sudo a2dismod php7

Install and enable apache2-mod_fcgid and php7-fpm:

sudo zypper install apache2-mod_fcgid php7-fpm
sudo zypper install apache2-event (optional but recommended; prefork is only good for mod_php)
sudo a2enmod proxy
sudo a2enmod proxy_fcgi
sudo a2enmod setenvif 
sudo a2enmod fcgid

Create php7-fpm configuration:

sudo cp /etc/php7/fpm/php-fpm.conf.default  /etc/php7/fpm/php-fpm.conf
sudo cp /etc/php7/fpm/php-fpm.d/www.conf.default /etc/php7/fpm/php-fpm.d/www.conf

For feature parity with mod_php add PEAR to the environment:

In /etc/php7/fpm/php-fpm.d/www.conf add at the end:

php_admin_value[include_path] = /usr/share/php7/PEAR

Start and enable PHP-FPM deamon:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Edit Apache FastCGI configuration:

sudo vi /etc/apache2/conf.d/mod_fcgid.conf

At the end of file, change:

#<FilesMatch "\.php$">
#    AddHandler fcgid-script .php
#    Options +ExecCGI
#    FcgidWrapper /srv/www/cgi-bin/php7 .php
#</FilesMatch>

to:

DirectoryIndex index.php
<FilesMatch "\.php$">
    SetHandler "proxy:fcgi://127.0.0.1:9000/"
    #CGIPassAuth on
</FilesMatch>

For something like Sabre, iRony oder APCu you should enable CGIPassAuth.


Set Apache MPM to event (optional but recommended):

In /etc/sysconfig/apache2 make sure to set

 APACHE_MPM="event"

Finally restart Apache via

sudo systemctl restart apache2