Csongor Halmai
Csongor Halmai

Reputation: 3915

PHP extensions not found via httpd but are found from CLI, with the same php.ini

I wanted to use some extensions of PHP 7.1 after installing it and Apache 2.4 and on my Windows 7. I wrote a small test script index.php to call some functions of the given extensions.

<?php
    var_dump(mb_strlen('p'));
    var_dump(mysqli_connect_error());

and uncommented the appropriate lines from the php.ini, like

...
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
...

and configured the extension_dir according to the windows-specific part of the php.ini

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"

extension_dir = "ext" 

I restarted the Apache web server and fetched the http://localhost/index.php. I got the error messages:

Fatal error: Uncaught Error: Call to undefined function mb_strlen() in C:\Program Files\Apache24\htdocs...

which means the extension was not loaded.

I doublechecked that the php.ini I made the modifications in is the same as the phpinfo() displayed in the browser:

Loaded Configuration File    C:\Program Files\php\php.ini 

On the contrary, if I started the same index.php not via web server but from the command line then I got different output:

C:\Program Files\Apache24\htdocs>"C:\Program Files\php\php.exe" index.php
int(1)
NULL

which means the extensions were loaded properly.

The command line PHP uses the same php.ini:

c:\Program Files\Apache24\htdocs>php -i|find "Loaded Configuration File"
Loaded Configuration File => C:\Program Files\php\php.ini

How can it be that the same php.ini file loads the extensions from command line but does not load them when used via the web server?

Upvotes: 2

Views: 2655

Answers (2)

Augusto
Augusto

Reputation: 2232

Check which modules is Apache using:

  • go to your apache config. IE: cd /etc/apache2/mods-enabled
  • list enables modulesls -lsai php* (in my case, prev version was enabled)
  • remove previous modules (IMC: rm php*)
  • enable needed module. IMC:
    • sudo ln -s ../mods-available/php7.4.load
    • sudo ln -s ../mods-available/php7.4.conf
  • reload apache (IMC: sudo /etc/init.d/apache2 restart)
  • use print/dump phpinfo(); to check which php module Apache is runnig

Command in example will work on Linux/Unix machines. Not on windows I'm afraid

Upvotes: 0

Csongor Halmai
Csongor Halmai

Reputation: 3915

Contrary to the description in the php.ini, the extension_dir should be specified with a full path, not just a relative one. Changing the line in php.ini from

extension_dir = "ext"

to

extension_dir = "C:/Progra~1/php/ext"

and restarting the web server solved the problem.

NB: I used the DOS-8.3 path instead of "C:/Program Files/php/ext" because according to the php-7.1.11-Win32-VC14-x64.zip\install.txt manual, it does not like paths containing spaces:

You may choose a different location but do not have spaces in the path (like C:\Program Files\PHP) as some web servers will crash if you do.

The path I referred to doesn't contain any spaces so I don't think I did anything wrong. Despite this, I am not sure how it would work if I had installed php to the default directory c:\php but it seems that defining the full path is a more secure way.

Upvotes: 4

Related Questions