Ubuntu 20.10 (Pop OS) with Apache and multiple versions of PHP-FPM FilesMatch directive not working

Tags: , , ,

I have a fresh Pop!OS 20.10 (Basically Ubuntu for those not familiar with Pop!OS). I’ve installed Apache, php 7.2, 7.4 & 8.0. Each version is running as FPM and each service is up and running. I have several vhosts setup and using FilesMatch to indicate which version of php should run on each host.

The problem is they all show php 8.0 when running phpinfo() no matter what the vhost config says. Anyone have any ideas?

Here’s one of the host files:

<VirtualHost zr1.local:80>
<FilesMatch .php$> # Apache 2.4.10+ can proxy to unix socket 
    SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost/" 
DocumentRoot "/var/www/zr1/web"
ServerName zr1.local
<Directory "/var/www/zr1/web">
    AllowOverride All
    Options +FollowSymLinks
    Order allow,deny
    Allow from all
    <IfModule mod_rewrite> 
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /app.php [QSA,L]

FPM service:

php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-02-15 14:38:26 EST; 1h 28min ago
       Docs: man:php-fpm7.4(8)
    Process: 27667 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/>
   Main PID: 27664 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 38267)
     Memory: 11.5M
     CGroup: /system.slice/php7.4-fpm.service
             ├─27664 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─27665 php-fpm: pool www
             └─27666 php-fpm: pool www

From /etc/php/7.4/fpm/pool.d/www.conf

listen = /run/php/php7.4-fpm.sock
apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-02-15 14:54:05 EST; 1h 25min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 29642 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 29646 (/usr/sbin/apach)
      Tasks: 8 (limit: 38267)
     Memory: 15.3M
     CGroup: /system.slice/apache2.service
             ├─29646 /usr/sbin/apache2 -k start
             ├─29647 /usr/sbin/apache2 -k start
             ├─29648 /usr/sbin/apache2 -k start
             ├─29649 /usr/sbin/apache2 -k start
             ├─29650 /usr/sbin/apache2 -k start
             ├─29651 /usr/sbin/apache2 -k start
             ├─29652 /usr/sbin/apache2 -k start
             └─29705 /usr/sbin/apache2 -k start

Let me know if you need to see anything else.



Thanks to Alfredo who determined this simplified solution:

<FilesMatch .php$> # Apache 2.4.10+ can proxy to unix socket 
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" 

Run updates. I had the same problem, now realized it started after the last set of php updates. Just ran the latest updates again and now it works again.

Okay, spoke to soon, the updates didn’t fix it.

Here’s what did work for me this was added to the vhost config file in sites-enabled:

    <IfModule proxy_fcgi_module>
        # Enable http authorization headers
        <IfModule setenvif_module>
        SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

        <FilesMatch ".+.ph(ar|p|tml)$">
            <If "-f %{REQUEST_FILENAME}">
                SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost/"
        <FilesMatch ".+.phps$">
            # Deny access to raw php sources by default
            # To re-enable it's recommended to enable access to the files
            # only in specific virtual host or directory
            Require all denied
        # Deny access to files without filename (e.g. '.php')
        <FilesMatch "^.ph(ar|p|ps|tml)$">
            Require all denied

Essentially this is the code copied from the /etc/conf-available/php7.2-fpm.conf file. As I’m not an expert on Apache I can’t explain why it worked when the simpler statement didn’t. But I’ve edited three different vhosts and they are now getting the correct version of PHP.

FYI: this issue is also being discussed here: https://askubuntu.com/questions/1316859/php-fpm-working-but-ignoring-version-from-sethandler/1317014#1317014

Source: stackoverflow