Topic: PHP 8.0 CGI not working

Using PHP 7.4 and CGI works fine for me - CGI scripts run as CGI and module scripts run in module mode.

When I upgrade to PHP 8.0 the CGI scripts run in module mode rather than CGI mode.

If I disable module mode (i.e. do not load the libphp80.so module) the scripts happily run in CGI mode.

I don't believe I have changed any other Apache configuration.

I also tried with a minimal configuration (e.g. systemctl httpd) and had the same issue.

This happens for me on CentOS 7 and CentOS 8.

It works for PHP 5.6, 7.2, 7.3 and 7.4. I have not yet tried 8.1 or higher.

Any thoughts?

Re: PHP 8.0 CGI not working

Some more info:

Using prefork

Minimal Apache CGI config:

<Directory "/var/www/cgi-bin">
    AddHandler php-script .php
    Action php-script "/cgi-bin/php80-cgi"
</Directory>

Where /cgi-bin/php80-cgi is just:

#!/opt/remi/php80/root/usr/bin/php-cgi

Re: PHP 8.0 CGI not working

> If I disable module mode (i.e. do not load the libphp80.so module) the scripts happily run in CGI mode.

So it works ?

Or, I don't understand the problem

EL-7 still uses mod_php by default
EL-8 uses php-fpm by default

Why do you think you still need very old "CGI" way ?

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: PHP 8.0 CGI not working

Same issue with PHP 8.1

Re: PHP 8.0 CGI not working

On EL-7

The 8.0 and 8.1 php-cgi images work if invoked manually from a shell.

Using Apache 2.4, it should be possible have folder A use module (i.e. apache2handler) and folder B use CGI (php-cgi).

<Directory A>
  SetHandler application/x-httpd-php
</Directory>
<Directory B>
  AddHandler php-script .php
  Action php-script "/cgi-bin/php80-cgi"
</Directory>
Suexec...

However, loading the 8.0 or 8.1 module, stops php-cgi being executed and all requests to folder A and folder B are givedn to apache2handler.

With 7.4 and earlier, the expected split behaviour works.

We need CGI + Suexec for some folders for security purposes. While most of a virtual site can happily run using the normal module mode (i.e. apache2handler) we have a small number of scripts that must run under a specific username as they modify some data and we can't allow module scripts to make those changes.

Re: PHP 8.0 CGI not working

>We need CGI + Suexec for some folders for security purposes. While most of a virtual site can happily run using
> the normal module mode (i.e. apache2handler) we have a small number of scripts that must run under a specific
> username as they modify some data and we can't allow module scripts to make those changes.

Exactly what FPM is designed for.

See: https://blog.remirepo.net/post/2022/02/ … orkstation

Each vhost/directory/app can be served by a specific FPM pool running a specific  PHP version under a specific user, in a separate process.

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: PHP 8.0 CGI not working

Despite I consider CGI as deprecated, it works

Configuration used

  <Directory /var/www/html/php74>
    <FilesMatch \.php$>
      SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </Directory>
  <Directory /var/www/html/php80>
    <FilesMatch \.php$>
      SetHandler "proxy:unix:/var/opt/remi/php80/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </Directory>
  <Directory /var/www/html/phpcgi>
    AddHandler php-script .php
    Action php-script "/cgi-bin/phpcgi"
    <FilesMatch \.php$>
      SetHandler php-script
    </FilesMatch>
  </Directory>

* php74 directory is served by php-fpm 7.4, Server API = FPM/FastCGI
* php80 directory is served by php-fpm 8.0, Server API = FPM/FastCGI
* phpcgi directory is served by php-cgi 8.0, Server API = CGI/FastCGI
* other directories are served by default php-fpm 8.1

Notice the "SetHandler" used here which is missing in your config

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: PHP 8.0 CGI not working

Hi Remi,

OK I will try that (FPM + CGI)

The big issue for me is that we have a large (arguably "legacy") infrastructire using EL-7 + php_mod + and CGI. This design works up to PHP 7.4, but fails for PHP 8.0+.

I am not sure what changed in PHP 8+, but Apache + php_mod + CGI no longer works. I suspect this is more of a PHP issue, so perhaps I should move this discussion there, though I suspect as per your comments, moving to FPM is probably the best approach. Your comments have been very helpful.

The short term solution for me is to run EL-7, php_mod (7.4) and CGI (8.0+) which does work.

In the longer term it looks like as we move to EL-8 (we are also looking at going straight to EL-9) we should also move to FPM rather than module. Our plan for this migration was to take some years ( we still haven't totally finished EL-6 + PHP 5.6 migration - we still have one legacy VM for sites that wont work on EL-7 + PHP 7.4)

Another issue for us is we have hundreds of low use web sites (vhosts) per VM that work well with php_mod. We couldn't have a separate FPM or pool for each site as this would use too many resources. Even using CGI, the average number of processes per vhost is < 0.1.

However, we can probably separate out the php_mod to FPM migration and bring it forward to the existing EL-7 VMs and keep CGI for its special use.

I note tht cPanel uses FPM with a per vhost pool. However, our cPanel VMs need significantly more resources than our non cPanel VMs.

Cheers, Simon

Re: PHP 8.0 CGI not working

One more thing smile

As far as I can see, using mod_php uses less resources and performs faster than FPM and CGI. It also works across multiple platforms including OpenVMS. FPM is not available on all the platforms we need to support, so for those platforms mod_php is our only choice. I will retest on such a platform to see in php_mod and CGI can work properly.

Thanks for your help

Cheers, Simon

Re: PHP 8.0 CGI not working

> OK I will try that (FPM + CGI)

This should also work with mod_php (but I don't have working mod_php config to test this)

Biggest issues with mod_php:

- only works with apache (FPM can be used by httpd, nginx, lighttpd...)
- only works in prefork mode, apache perf is much better in "event" (threaded) mode, especially for static files
- only one module (single version)
- only one user (for all sites)

> The big issue for me is that we have a large (arguably "legacy") infrastructire using EL-7 + php_mod + and CGI. This design works up to PHP 7.4, but fails for PHP 8.0+.

I don't see how PHP version may change this, handler choice is done by HTTP.

So can only be related to httpd configuration
If you only use "CGI", ensure mod_php and php-fpm are not installed (which provides the php.conf with some handler config). Config files order may also change.

> Another issue for us is we have hundreds of low use web sites (vhosts) per VM that work well with php_mod. We couldn't have a separate FPM or pool for each site as this would use too many resources.

If hundreds of sites can run with mod_php, so with the same user, they can run with the same pool.
Multiple pools make sense only for

- multiple versions
- multiple users
- multiple configuration

Also notice the "ondemand" pool configuration, useful for small sites (while big sites will take benefit of "static")

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi

Re: PHP 8.0 CGI not working

> I don't see how PHP version may change this, handler choice is done by HTTP.

I agree - the behaviour I am seeing doesn't make sense - Apache (our main HTTP) should send the request to the appropriate handler.

> So can only be related to httpd configuration

This is what I thought. However, I have not been able to detect yet where the config influences this. I am working on a set of minimal cases - the configs are less than 20 lines and have heavy logging - to see if I can detect how the issue is triggered.

> If you only use "CGI", ensure mod_php and php-fpm are not installed (which provides the php.conf with some handler config). Config files order may also change.

CGI only works fine. I haven't tested CGI + FPM yet (working on that). Only CGI + mod_php (our current widespread deployment) fails.

I'll keep you posted with what my research uncovers.

Re: PHP 8.0 CGI not working

OK, I found it.

We have been using an action definition named php-script.

<Directory /usr/local/httpd/test80/cgi-bin>
  Action php-script /cgi-bin/php80-cgi
  AddHandler php-script .php
</Directory>

It turns out this name is reserved from PHP 8.0+. In 7.4 the reserved name is php7-script.

If I change it to

<Directory /usr/local/httpd/test80/cgi-bin>
  Action my-php-script /cgi-bin/php80-cgi
  AddHandler my-php-script .php
</Directory>

it works fine.

The change is in sapi_apache2.c

(Almost) always something simple in the end!

Re: PHP 8.0 CGI not working

> It turns out this name is reserved from PHP 8.0+. In 7.4 the reserved name is php7-script.

Nice find

Laptop:  Fedora 38 + rpmfusion + remi (SCL only)
x86_64 builder: Fedora 39 + rpmfusion + remi-test
aarch64 builder: RHEL 9 with EPEL
Hosting Server: CentOS 8 Stream with EPEL, rpmfusion, remi