3

Our server is Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-135-generic x86_64).

We followed the directions from https://tecadmin.net/install-multiple-php-version-apache-ubuntu/ a while back and got PHP 5.6 and 7.2 running back when 7.2 was new, and have upgraded and added 7.3 and 7.4 with no problems until this morning. Yesterday it worked fine but today it is not. We package update early yesterday but it didn't require a reboot and don't believe it rebooted overnight on its own as uptime says we're close to 15 days.

So today we set about going through the process again and have done this, restarting apache2 after every step:

Tried: a2enmod actions fcgid alias proxy_fcgi and got: ERROR: Module fcgid does not exist!

Did apt install apache2 libapache2-mod-fcgid and that worked and added the package.

Did a2enmod actions fcgid alias proxy_fcgi and it said everything already enabled.

Checked conf file in /etc/apache2/sites-enabled:

<FilesMatch \.php$>
    SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>

Tried phpinfo() page and got PHP 7.4.15 instead of expected 5.6.40. We did the same with a 7.3 and got the same 7.4 on phpinfo() using SetHandler of php7.3 as well.

Then tried systemctl status php5.6-fpm and php7.4-fpm and both active (running)

My /etc/apache2/conf/php5.6-fpm.conf file is here for starters, and the one for php7.4-fpm.conf is the same except for mod_php5/7 and the SetHandler version:

# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php5.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
        </If>
    </FilesMatch>
    <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
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

Is there an error here or somewhere else I should look?

FWIW, we have another server with a similar setup and it is having the same issue, but we only need the 5.6 legacy application on that server at this time so we just disabled 7.4 by removing the pointer file in /etc/apache2/conf-enabled/php7.4-fpm.conf and restarting apache2. However, this server needs multiple versions.

6
  • 3
    Can confirm, across 6 instances of Ubuntu 20.04 as of this week only the latest PHP-FPM is used. SetHandler directives to set a VHOST to a specific version is completely ignored. I currently have to a2disconf php8.0-fpm and restart Apache to prevent PHP 8.0 being used at all.
    – Adambean
    Feb 17, 2021 at 13:17
  • 2
    Interestingly this problem only seems to impact Ubuntu, and likely its derivatives too. Our company's dozen or so Debian 10 instances using the same Apache and PHP-FPM configurations work completely as expected, as do my half dozen personal Debian 10 instances. I've double checked the same Apache modules are enabled, literally identical.
    – Adambean
    Feb 17, 2021 at 13:31
  • 1
    Interesting, I guess that makes it a Ubuntu issue over a PHP issue. I know our 5.6 instance has a "+ubuntu18.04.1+deb.sury.org+1" after the version. I just wish I knew where to go digging with that but seems we're at the mercy of the Ubuntu updates for now.
    – Andrew T
    Feb 17, 2021 at 15:02
  • I was going to ask if many of you use the (excellent) Sury repository, and did look at its issue board for reports of this fault. To me it appears that the issue is with Apache not routing to the correct PHP-FPM as configured rather than PHP (or PHP-FPM). As far as I know Sury distribute the same packages to both Debian and Apache so I would currently be inclined to think Sury's packages are not the problem. (Though someone more expert on the repository is very welcome to correct me on this.)
    – Adambean
    Feb 17, 2021 at 15:50
  • 1
    I've filed this with Ubuntu officially here: answers.launchpad.net/ubuntu/+question/695605
    – Adambean
    Feb 17, 2021 at 18:50

5 Answers 5

5

Change:

<FilesMatch \.php$>
    SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>

for:

<FilesMatch \.php$>
   <If "-f %{REQUEST_FILENAME}">
    SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost"
   </If>
</FilesMatch>
5
  • 2
    Could you explain your proposition a little bit? It is better to know why such a change is necessary than to blindly copy and paste a piece of code. Feb 18, 2021 at 11:17
  • That line just check that the file exists. Not sure why, but it works. It's the way the default /etc/apache2/conf/php5.6-fpm.conf file configure the SetHandler.
    – Alfredo
    Feb 18, 2021 at 11:37
  • It is the way the default php5.6-fpm.conf file configures the SetHandler, but we are having this problem with php5.6 as well. That is in my original php5.6-fpm.conf file posted in the original question, so unfortunately this solution does not work with our setup, and I'm still not sure what is different.
    – Andrew T
    Feb 18, 2021 at 13:55
  • @Alfredo, I changed it in the sites-available conf file for the individual site as that had the same line in our setup, and that did the trick for us. I'm curious to see what the Ubuntu team says!
    – Andrew T
    Feb 18, 2021 at 16:05
  • Can confirm that wrapping SetHandler with <If "-f %{REQUEST_FILENAME}"> resolves this. Outrageous! But thank you kindly @Alfredo.
    – Adambean
    Feb 19, 2021 at 7:46
3

Since I update my OS (ubuntu 20.04 LTS) on 15 feb 2021, I started to have this PROBLEM. Now php 8.0 is used for all my sites, instead of SetHandler configs

0

I'm experiencing the exact same on my Ubuntu WSL setup. However the following article was helpfull for a co-worker of mine so it might help you out aswell.

Avoid using ProxyPassMatch

# don't
<IfModule mod_proxy.c>
  ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/
</IfModule>

Since ProxyPassMatch takes precedence over FilesMatch which u use for SetHandler

source: https://ma.ttias.be/apache-2-4-proxypass-for-php-taking-precedence-over-filesfilesmatch-in-htaccess/

2
  • he is not using ProxyPassMatch
    – francogp
    Feb 17, 2021 at 11:31
  • Thanks for the idea but couldn't find ProxyPassMatch anywhere in my config files.
    – Andrew T
    Feb 17, 2021 at 15:09
0

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:

EDIT: This was put in the /etc/apache2/sites-available/site1.local.conf. Here's the entire file:

<VirtualHost site1.local:80>
    <IfModule !mod_php7.c>
        <IfModule proxy_fcgi_module>
            # Enable http authorization headers
            <IfModule setenvif_module>
            SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
            </IfModule>

            <FilesMatch ".+\.ph(ar|p|tml)$">
                <If "-f %{REQUEST_FILENAME}">
                    SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost/"
                </If>
            </FilesMatch>
            <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
            </FilesMatch>
            # Deny access to files without filename (e.g. '.php')
            <FilesMatch "^\.ph(ar|p|ps|tml)$">
                Require all denied
            </FilesMatch>
        </IfModule>
    </IfModule>
    DocumentRoot "/var/www/site1/docroot"
    ServerName site1.local
    <Directory "/var/www/site1/docroot">
        AllowOverride All
        Options +FollowSymLinks
        Order allow,deny
        Allow from all
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ /app.php [QSA,L]
        </IfModule> 
    </Directory>
</VirtualHost>

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.

5
  • I have the same problem. There's no more updates
    – francogp
    Feb 17, 2021 at 10:15
  • This is what I had in my php5.6-fpm.conf except wrapped around an <IfModule !mod_php5.c> so I removed that and restarted php5.6-fpm and apache2 but still don't have any change. For kicks, I removed the <IfModule proxy_fcgi_module> as well and that didn't seem to do it for me. Thanks for the update and code sample and glad it worked for you. Is your conf file still just like <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/" </FilesMatch>
    – Andrew T
    Feb 17, 2021 at 15:07
  • I replaced the <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" </FilesMatch> with the above code. Feb 17, 2021 at 15:33
  • so, the trick is this: ``` <FilesMatch \".+\.ph(ar|p|tml)$\"> # Apache 2.4.10+ can proxy to unix socket <If \"-f %{REQUEST_FILENAME}\"> SetHandler \"proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost\" </If> </FilesMatch>```
    – francogp
    Feb 18, 2021 at 9:41
  • Thanks @joekrukosky ... turns out it was the <If "-f %{REQUEST_FILENAME}"> line that helps it, which you have here. We had to have it in the phpX.X-fpm.conf files as well as in the individual site.conf file in our case.
    – Andrew T
    Feb 19, 2021 at 13:26
0

I'm using Ubuntu 20.04, I've experienced exactly same problem - only one version of PHP was showing up, the latest one. I tried to setup three PHP versions to run with the same Apache simultaneously.

It looked to me as if SetHandler line for all three configs was set to the same value of last loaded config file. Config files was listed in following order in directory:

  • /etc/apache2/conf-enabled/php5.6-fpm.conf
  • /etc/apache2/conf-enabled/php7.4-fpm.conf
  • /etc/apache2/conf-enabled/php8.1-fpm.conf

Apache logs showed, every virtualhost I've created, which was supposed to run different version of PHP, was trying to connect to the same unix socket of php8.1.

Alfredos answer didn't solve the problem on its own for me.

Solution which worked for me was switching from using unix sockets to TCP sockets, as form of communication between apache2 and php-pfm. To do that, I've modified those three files to set ip and listening port of php-pfm.

in file: /etc/php/5.6/fpm/pool.d/www.conf

listen = 127.0.0.1:9056

in file: /etc/php/7.4/fpm/pool.d/www.conf

listen = 127.0.0.1:9074

in file: /etc/php/8.1/fpm/pool.d/www.conf

listen = 127.0.0.1:9081

To reflect above changes in Apache, I've modified files: /etc/apache2/conf-enabled/php*-fpm.conf and /etc/apache2/sites-enabled/php*.mydomain.conf so Apache can connect to proper ip/port. Config files incorporates Alfredos answer.

file /etc/apache2/conf-enabled/php5.6-fpm.conf looks like this:

<IfModule !mod_php5.c>
<IfModule proxy_fcgi_module>
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>
    <FilesMatch ".+\.ph(ar|p|tml)$">
        <If "-f %{REQUEST_FILENAME}">
        SetHandler "proxy:fcgi://localhost:9056"
        </If>
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        Require all denied
    </FilesMatch>
    <FilesMatch "^\.ph(p[3457]?|t|tml|ps)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

file /etc/apache2/conf-enabled/php7.4-fpm.conf looks like this:

<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>
    <FilesMatch ".+\.ph(ar|p|tml)$">
        <If "-f %{REQUEST_FILENAME}">
        SetHandler "proxy:fcgi://localhost:9074"
        </If>
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        Require all denied
    </FilesMatch>
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

file /etc/apache2/conf-enabled/php8.1-fpm.conf looks like this:

<IfModule !mod_php8.c>
<IfModule proxy_fcgi_module>
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>
    <FilesMatch ".+\.ph(ar|p|tml)$">
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9081"
        </If>
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        Require all denied
    </FilesMatch>
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

My virtual hosts files looks like this:

/etc/apache2/sites-enabled/php56.mydomain.com.conf :

<VirtualHost *:80>
    ServerName php56.mydomain.com
    ServerAlias www.php56.mydomain.com
    DocumentRoot /var/www/php56
    <Directory /var/www/php56>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9056"
        </If>
    </FilesMatch>

    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/php56.error.log
    CustomLog ${APACHE_LOG_DIR}/php56.access.log combined
</VirtualHost>

/etc/apache2/sites-enabled/php74.mydomain.com.conf :

<VirtualHost *:80>
    ServerName php74.mydomain.com
    ServerAlias www.php74.mydomain.com
    DocumentRoot /var/www/php74
    <Directory /var/www/php74>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9074"
        </If>
    </FilesMatch>
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/php74.error.log
    CustomLog ${APACHE_LOG_DIR}/php74.access.log combined
</VirtualHost>

/etc/apache2/sites-enabled/php81.mydomain.com.conf :

<VirtualHost *:80>
    ServerName php81.mydomain.com
    ServerAlias www.php81.mydomain.com
    DocumentRoot /var/www/php81
    <Directory /var/www/php81>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9081"
        </If>
    </FilesMatch>
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/php81.error.log
    CustomLog ${APACHE_LOG_DIR}/php81.access.log combined
</VirtualHost>

For reference:

#apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2021-10-14T16:24:43

I hope it helps and spare You some time.

EDIT: To answer your question, is there an error in your config. Line with SetHandler may be a problem if you use TCP sockets, because port is not specified and that could cause Apache trying to connect to same PHP version.

Where else to look for: Check if you have proper SetHandler lines in those files as well: /etc/apache2/sites-enabled/php*.mydomain.conf

Check if SetHandler lines from above files match "listen" lines from following files: /etc/php/*/fpm/pool.d/www.conf

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.