Apache doesn’t want to load GnuPG PHP

apache-2.4gpg

I've asked this in SO but maybe here is more suitable to ask.
I'm installed GnuPG via Pecl. Generate pub/priv keys etc.. I have in php.ini

extension=gnupg.so

When I try to run simple code I've got undefined gnupg_init() error which by my understands is meaning that module isn't loaded.

Fatal error: Uncaught Error: Call to undefined function gnupg_init() 

This is the code which I trying to run

putenv('GNUPGHOME=/home/stan/.gnupg');
$res = gnupg_init();
gnupg_seterrormode($res,GNUPG_ERROR_WARNING);
gnupg_addencryptkey($res,"myfingerprint");
$enc = gnupg_encrypt($res, "just a test");
echo $enc;

The system is ubuntu + apache.
Here are permissions of the .gnupg/

srwxrwxr-x 1 stan stan    0 Aug 16 08:12 S.gpg-agent
-rw------- 1 stan stan   32 Aug 16 10:02 pubring.kbx~
drwx------ 2 stan stan 4096 Aug 16 10:04 private-keys-v1.d
-rw-rw-r-- 1 stan stan 1361 Aug 16 10:04 pubring.kbx
drwx------ 2 stan stan 4096 Aug 16 10:04 openpgp-revocs.d
-rw------- 1 stan stan 2603 Aug 16 10:19 secring.gpg
-rw------- 1 stan stan 1209 Aug 16 10:19 pubring.gpg~
-rw------- 1 stan stan 1360 Aug 16 10:19 trustdb.gpg
-rw------- 1 stan stan  600 Aug 16 10:19 random_seed
-rw------- 1 stan stan 1209 Aug 16 10:19 pubring.gpg

Here is my /var/www/

drwxrwsr-x 3 stan www-data 4096 Jul 14 11:23 www
drwxr-sr-x 7 stan www-data 4096 Aug 16 09:59 html

I have also confirmation that is installed and loaded

$ php --info | grep gnupg
gnupg
gnupg support => enabled

Best Answer

It might depend on what repository you use and packages are available. As noted in my comment, when you run phpinfo(); inside a php script and load it from your browser, you'll see that mod_php isn't loading the gnupg extension.

On my CentOS servers, we use the Remi repos for PHP. He has a package called php-pecl-gnupg. When I install it, it installs the necessary shared objects for mod_php and I can confirm it's available to my web code.

So we've seen that command line php and mod_php in apache don't load the same configurations, so doing php --info doesn't necessarily show you what mod_php in Apache is configured with. Look in your phpinfo() file and see what configuration file it's reading from (it shows in the first few lines). Then make sure you have extension=gnupg.so in that config file and make sure that it's looking in the same directory for the extension.