Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Class 'ZMQContext' not found ( but it is installed and works on the terminal )

Tags:

php

nginx

I want to try the ZeroMQ, and I write two php file, service.php and client.php. I use the linux terminal to run service php /web/test/service.php,it's ok, terminal print a "waiting for client connecting...".

but, I request my client.php through chrome explorer,error happened, I check my error.log,there is message "php fatal error: class 'ZMQContext' not found........"

and I use command php -m to check my php extension, zmq is already in that list.

like image 669
krosshj Avatar asked Jul 27 '13 09:07

krosshj


2 Answers

The problem is that the ZMQ module is loaded in the PHP CLI (Command Line Interface) but it's not loaded into Apache. Therefore, service.php runs smoothly from the Command Line but client.php can't use ZMQContext because Apache does not load ZMQ.

There are two different .ini-files. These probably are (but can be different, depending on your distro):

  • /etc/php5/apache2/php.ini for Apache
  • /etc/php5/cli/php.ini for CLI

However, all .ini files from the /etc/php5/conf.d/ directory are loaded into both Apache and the CLI.

See also: PHP - An external Class/library is accessible from apache but not from phpunit (the exact opposite of your problem)

like image 73
cutsoy Avatar answered Oct 25 '22 00:10

cutsoy


Check which php.ini files are loaded

Checking (with phpinfo) which php.ini files are loaded when requested via nginx (which probably means via php-fpm) - will almost certainly reveal that it loads different ini files than the cli. Assuming php-fpm usage, the following ini files are probably loaded:

/etc/php5/fpm/php.ini
/etc/php5/fpm/conf.d/*

and no zmq.ini file listed.

Loading zmq for php-fpm

Follow the instructions for installing zmq on php, and create an ini file for zeromq (or copy the one from /etc/php5/cli/conf.d/ since evidently it's loaded for cli usage):

# /etc/php5/conf.d/zeromq.ini
extension=zmq.so

Then restart php-fpm

sudo /etc/init.d/php5-fpm restart

And zeromq should be available for use.

like image 26
AD7six Avatar answered Oct 24 '22 23:10

AD7six