4

I installed(manually) Apache and PHP. I activated the curl extension from php.ini and It's working fine in PHP-CLI, but when I access PHP through browser with the help of Apache, curl(and any other) extension giving me errors that "{extension_function} is undefined"

PHP 7.2-64bit

Apache 2.4

// php.ini
extension_dir = "/server/php/ext"
...
extension=bz2
extension=curl
extension=fileinfo
;extension=gd2
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=interbase
;extension=ldap
;extension=mbstring
;extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=odbc
extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop

// httpd.conf    
LoadModule php7_module "/server/php/php7apache2_4.dll"
<IfModule php7_module>
    AddHandler application/x-httpd-php .php
    AddType application/x-httpd-php .php .html
    PHPIniDir "/server/php"
</IfModule>

Update

I'm testing using this var_dump(function_exists('curl_init')); line of code ... when I run this line from php-cli, It's returning true. When I run this line from a php file using browser, It's returning false.

From PHP-CLI

From browser

PHP File

Error

Solved

  1. I used absolute paths everywhere(no relative path)(used forward slashes)
  2. Added PHP to environment variables. I don't know why, but without this extensions aren't loading. I think php7 module for Apache requires php in environment.
  3. Restarted the computer(Apache restart won't work)

I hope someone will find this helpful.

Thank you for the help.

Inam Ul Huq
  • 732
  • 7
  • 8
  • Does PHP work when using it via Apache (the browser)? – RiggsFolly Oct 14 '18 at 14:30
  • 2
    Often the PHP used within Apache uses a different `php.ini` file. Do you have one in the `apache\bin\ folder? If so edit that like you did the one used by PHP CLI – RiggsFolly Oct 14 '18 at 14:31
  • Yes, PHP working fine in browser. I ran phpinfo(); function from a php file and it's working fine. – Inam Ul Huq Oct 14 '18 at 14:37
  • 1
    So look at the parameter called `Loaded Configuration File` on the phpinfo output. That tells you where PHP Under Apache is getting its `php.ini` file from – RiggsFolly Oct 14 '18 at 14:40
  • There isn't any `php.ini` file inside `apache\bin` or any apache directory. – Inam Ul Huq Oct 14 '18 at 14:40
  • It's loading from `C:\server\php\php.ini` that exactly where I've enabled the curl lib. – Inam Ul Huq Oct 14 '18 at 14:42
  • Can you show us the complete error message please – RiggsFolly Oct 14 '18 at 14:43
  • I've updated the question for more detail. – Inam Ul Huq Oct 14 '18 at 14:46
  • Thanks for prompt reply though :) – Inam Ul Huq Oct 14 '18 at 14:59
  • That does not actually help me, sorry – RiggsFolly Oct 14 '18 at 15:00
  • Do you need any other information ? – Inam Ul Huq Oct 14 '18 at 15:11
  • Did you try just trying to execute `curl_init()` then show us the error you get – RiggsFolly Oct 14 '18 at 15:13
  • I added error screen in the question. It's just a `function undefined` error. – Inam Ul Huq Oct 14 '18 at 15:58
  • You need to capture the phpinfo output for both PHP contexts and then compare it. That will lead you to why curl isn't available. – Will Bickford Oct 14 '18 at 16:13
  • Also make sure that the user Apache is configured as, has read access to `C:\server\php`. By default Windows will assign directory permissions based on the logged in user. Where Apache is typically configured to run as a different user. When launching PHP through the Apache mod_php, PHP will be executed as the Apache user and not your currently logged in user. As opposed to the CLI where PHP will be executed as you, which more than likely has administrative privileges. – Will B. Oct 14 '18 at 16:22
  • Another place to check is the `extension_dir` path in the `phpinfo()` which if loaded through CLI, should function fine by default when executed by Apache, but make sure it matches where your DLL files are stored. Additionally check your Apache error logs to see if there are any errors listed that can help troubleshoot. – Will B. Oct 14 '18 at 16:42
  • Thank you very much for suggestions. I'm trying all these and will let you know the result. – Inam Ul Huq Oct 14 '18 at 19:13
  • issue has been fixed(updated the question). Thank you very much for the help :) – Inam Ul Huq Oct 14 '18 at 19:36

1 Answers1

3

I had the same problem - on Windows. After changing the path to the extension directory to an absolute path, it worked.

As suggested in the default php.ini (not working):

extension_dir = "ext"

Turned into an absolute path (working)

extension_dir = "c:\amp\php\ext"

The strange thing is that this problem occurs on some machines / versions only. Could not nail it down. There is an official bug listed here:

https://bugs.php.net/bug.php?id=74866

ESP32
  • 8,089
  • 2
  • 40
  • 61
  • Thanks - changing `extension_dir` to an absolute path solved the mysterious load failures completely! – DarthGizka Apr 26 '21 at 18:36
  • In my case the problem was that I had apache and php in user env vars only and not on system env vars. Adding those to system env vars fixed the problem. – c-shubh Aug 28 '22 at 09:11