pcntl_fork not working when being used with apache but does work from CLI

380 Views Asked by At

I have a small mission critical web app that needs to respond to all requests rather fast. I need to run some other code that can potentially take a few seconds. I was trying to use pcntl_fork. If I have the simply script

<?PHP
$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // we are the parent
        echo "We are the parent!!";
     pcntl_wait($status); //Protect against Zombie children
} else {
     // we are the child
        sleep (5);
        echo "Child here!!";
}

?>

It seems to work fine. I get

[dovid@k1 php.d]# php  /var/www/html/test.php 
We are the parent!!Child here!![dovid@k1 php.d]# 

PHP shows that it's enabled

[dovid@k1 php.d]# php -i | grep pcntl
pcntl
pcntl support => enabled
[dovid@k1 php.d]#

Yet when I try to call the page via apache I get a 500 error and in the apache logs I get

[Mon Jun 13 05:20:53.953818 2022] [:error] [pid 30457] [client XX.XX.XX.XX:57500] PHP Fatal error:  Call to undefined function pcntl_fork() in /var/www/html/test.php on line 2

Is there something missing from my apache config? Is there a better way to fork thinks in PHP (e.g. using exec and &?). I am doing this on a CentOS7 clone (Sangoma Linux) box. The PHP packages installed are rather old

[dovid@k1 php.d]# rpm -qa | grep php
php56w-5.6.40-1.sng7.x86_64
php56w-xml-5.6.40-1.sng7.x86_64
php56w-pear-1.10.1-1.w7.noarch
php56w-intl-5.6.40-1.sng7.x86_64
php-digium_register-4.0-1.sng7.x86_64
php56w-pdo-5.6.40-1.sng7.x86_64
php56w-pecl-redis-2.2.7-1.w7.x86_64
php56w-gd-5.6.40-1.sng7.x86_64
php56w-pecl-ssh2-0.13-1.sng7.x86_64
php56w-pecl-igbinary-1.2.1-2.w7.x86_64
php56w-odbc-5.6.40-1.sng7.x86_64
php56w-process-5.6.40-1.sng7.x86_64
php56w-mbstring-5.6.40-1.sng7.x86_64
php56w-soap-5.6.40-1.sng7.x86_64
php56w-mysqlnd-5.6.40-1.sng7.x86_64
php56w-ldap-5.6.40-1.sng7.x86_64
php56w-common-5.6.40-1.sng7.x86_64
php56w-cli-5.6.40-1.sng7.x86_64
php56w-bcmath-5.6.40-1.sng7.x86_64
[dovid@k1 php.d]# rpm -qa | grep httpd
httpd-tools-2.4.6-93.el7.centos.x86_64
httpd-2.4.6-93.el7.centos.x86_64
[dovid@k` php.d]# 
1

There are 1 best solutions below

0
Dovid Bender On

It seems that pcntl does not work with apache. It seems like this may work https://github.com/spatie/async