470

I need to enable pdo_mysql in my EasyPHP environment, so I went to the php.ini file and uncommented the following line:

extension=php_pdo_mysql.dll

Unfortunately I still have the same problem. I'm using the CLI so I suppose I need to locate the php.ini file used by the CLI. How can I find it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
  • Also see http://stackoverflow.com/q/8684609/632951 – Pacerier Aug 11 '15 at 17:06
  • @sjas It is not the wrong, it is correct and let you know the path of the loaded php.ini in the cli. – SaidbakR Feb 24 '16 at 20:22
  • EasyPHP (probably) implies [Windows](https://en.wikipedia.org/wiki/Microsoft_Windows). Perhaps tag it as such? – Peter Mortensen Aug 08 '21 at 14:31
  • [Some context](https://stackoverflow.com/questions/828899/why-cant-cakephp-bake-connect-to-mysql-running-under-easyphp-on-windows-vista-u/1961355#1961355): *"The root cause of the problem is that in EasyPHP 5.3 there isn't any* ***php.ini*** *file in the* ***php*** *folder.* – Peter Mortensen Aug 08 '21 at 14:46

15 Answers15

1102

Just run php --ini and look for Loaded Configuration File in the output for the location of php.ini used by your CLI.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 12
    @BradFJacobs why use grep when this is now cross-platform? – That Realty Programmer Guy May 22 '14 at 11:56
  • But doesn't this just show you the one that is loaded when using PHP via the CLI (because you are running the command from that). – Antony D'Andrea Oct 15 '15 at 09:06
  • 2
    @AntonyD'Andrea: which is exactly what the question is about. See? It does state so even in the title: How to find the php.ini file used by the _command line_? – Mchl Oct 26 '15 at 19:31
  • this is not my answer...in my case I type `sudo find / -iname php.ini` and it founds `/etc/php/7.0/apache2/php.ini` the right place of the right php.ini config file of my machine that it wasn't found by `php --ini` or `php -i | grep php.ini` (both found `/etc/php/7.0/cli/php.ini`) – NineCattoRules Feb 24 '17 at 10:24
  • 2
    @NineCattoRules if your cli php uses Apache's php.ini something is wrong with your setup – Mchl Feb 24 '17 at 12:37
  • maybe it's because I've recently upgrade PHP to 7 from 5.5.9 ...where should I check my setup? Everything works as usual – NineCattoRules Feb 24 '17 at 14:12
  • ...also check this out http://askubuntu.com/a/356990/413592 I don't think there's something wrong with my setup – NineCattoRules Feb 24 '17 at 14:21
  • That answer says the same as me. There's a separate php.ini for CLI and a separate one for Apache. – Mchl Feb 24 '17 at 14:26
  • A million files are displayed, would be nice to add a sentence fragment explaining which one is the CLI one. – Andrew Feb 27 '18 at 16:31
  • 1
    @Mchl: I have shamelessly edited your answer to include the informative not to look for `Loaded Configuration File` in output :) – Fr0zenFyr Jan 24 '19 at 11:52
  • 1
    @Fr0zenFyr: thank you. It's been so long time ago sine I worked with PHP I don't even recall if the output from `php --ini` was always this way, or it has changed since my answer. – Mchl Jan 25 '19 at 11:53
  • It is empty in my case. Please can you advise. # php --ini Configuration File (php.ini) Path: /usr/local/etc/php Loaded Configuration File: (none) Scan for additional .ini files in: /usr/local/etc/php/conf.d Additional .ini files parsed: /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini – Dulitha K Oct 21 '20 at 21:05
  • 1
    @DulithaKumarasiri just create your own `php.ini` file, if you really need to change any setting. What happened is that your Docker container is merely using the reasonable defaults set by the PHP core developer team; you'd only need to _create_ a `php.ini` **if and only if** you need to _change_ a setting. – Gwyneth Llewelyn Apr 12 '21 at 12:18
  • this only shows the location of the cli's php.ini file. the apach2 php.ini file is what most people usually look for and its location is `../apache2/php.ini` relatively – Meir Nov 08 '21 at 18:59
  • Thank you for this comment @Meilech. If you read the question though it specifically asks about CLI's php.ini. – Mchl Nov 09 '21 at 19:23
401

You can get a full phpinfo() using:

php -i

And, in there, there is the php.ini file used:

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

On Windows, use find instead:

php -i | find/i"configuration file"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I did that but the weird thing is that it's pointing me the C:\WINDOWS path. And I didn't find it there? – Amokrane Chentir May 01 '10 at 15:58
  • The name of the file is still: php.ini right? Or something starting with php at least? – Amokrane Chentir May 01 '10 at 16:02
  • 13
    Then this php instance used no php.ini at all but the default values. The `Configuration File` line of phpinfo() shows the last place where php looked for an ini file. That can either be the place where it found such a file or the last option which happens to be %SYSTEMROOT% on windows. – VolkerK May 01 '10 at 16:04
  • 3
    @pascal: `php -i | find "Configuration file"` should work on Windows. Definitely not as powerful as grep, but `find` will do basic string searching for you. Of course, if php -i dumps its output to stderr, you're probably SOL. – Marc B May 02 '10 at 22:43
  • 3
    `find` is case-sensitive by default, so it has to be perfect (`php -i | find "Configuration File"`) or made case-insensitive (`php -i | find /i "Configuration file"` - note the `/i` flag). – deizel. Jun 14 '11 at 10:11
  • @deizel: thanks for the tip, you were right, it only works with the `/i` switch or you have to type in the exact string - and thanks for Marc B too for mentioning `find`. I edited the original post according to your comments. – Sk8erPeter May 10 '12 at 13:27
  • 12
    Consider adding `php --ini` as an alternative; available since PHP 5.2.3 :) – Ja͢ck Jun 05 '12 at 14:35
  • PHP looks for config in 7 places as outlined here: http://php.net/manual/en/configuration.file.php. If you are getting a pointer to C:\Windows with php --ini, do this to fix it: click my computer->system properties->advanced system settings->environment variables->new...(system variable). Name the key: PHPRC and set the value to the full path to your php.ini file. Open a new console and enter php --ini again. Your config should be loaded. – Brandon G Jul 04 '12 at 01:05
  • 2
    Nobody mentioned, that it's possible to take that value from script by calling [php_ini_loaded_file](http://www.php.net/manual/en/function.php-ini-loaded-file.php) and when needed [php_ini_scanned_files](http://www.php.net/manual/en/function.php-ini-scanned-files.php) – Radek Benkel Nov 28 '12 at 13:20
  • 1
    Just another side note: `grep` *is* available on windows: http://i.imgur.com/KP5MJRV.png – eyecatchUp Apr 02 '13 at 23:34
  • Works flawlessly for me. If the default "Configuration File" was not changed, you shouldn't worry about case-sensitivity. php --ini is a nice alternative for PHP 5.2.3+ versions. – GTodorov Apr 23 '14 at 05:10
  • 1
    Please notice that this could be very misleading if you have both php-cli install and another php which interacts with the webserver. Just running php -i from the command-line will most probably return the php.ini for the php-cli and not the one for the webserver. – Tommie Jan 27 '15 at 12:58
  • 1
    On OSX Mavericks, running: $ php -i | grep 'Configuration File' Returned: Configuration File (php.ini) Path => /etc Loaded Configuration File: (none) In the `/etc/` directory was: php.ini.default (as well as `php-fpm.conf.default`) I was able to copy `php.ini.default` to `php.ini`, add `date.timezone = "US/Central"` to the top (right below `[php]`), and the problem is solved. At least the error message is gone. – MikeiLL May 28 '15 at 19:31
  • In what environment on Windows? [WAMP](https://en.wikipedia.org/wiki/LAMP_(software_bundle)#Variants)? Can you add something about this to your answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 08 '21 at 13:43
  • Sample output on [Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa) and [a LAMP equivalent](https://www.osradar.com/install-lamp-ubuntu-20-04/) (Apache, MariaDB, and PHP) - PHP 7.4.3: `/etc/php/7.4/cli/php.ini` – Peter Mortensen Aug 08 '21 at 14:15
20

You can use get_cfg_var('cfg_file_path') for that:

To check whether the system is using a configuration file, try retrieving the value of the cfg_file_path configuration setting. If this is available, a configuration file is being used.
Unlike phpinfo() it will tell if it didn't find/use a php.ini at all.
var_dump( get_cfg_var('cfg_file_path') );

And you can simply set the location of the php.ini. You're using the command line version, so using the -c parameter you can specify the location for this particular run, e.g.

php -c /home/me/php.ini -f /home/me/test.php
VolkerK
  • 95,432
  • 20
  • 163
  • 226
19

Run php --ini in your terminal, and you'll get all details about ini files:

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/apc.ini,
/etc/php.d/bcmath.ini,
/etc/php.d/curl.ini,
/etc/php.d/dba.ini,
/etc/php.d/dom.ini,
/etc/php.d/fileinfo.ini,
/etc/php.d/gd.ini,
/etc/php.d/imap.ini,
/etc/php.d/json.ini,
/etc/php.d/mbstring.ini,
/etc/php.d/memcache.ini,
/etc/php.d/mysql.ini,
/etc/php.d/mysqli.ini,
/etc/php.d/pdo.ini,
/etc/php.d/pdo_mysql.ini,
/etc/php.d/pdo_sqlite.ini,
/etc/php.d/phar.ini,
/etc/php.d/posix.ini,
/etc/php.d/sqlite3.ini,
/etc/php.d/ssh2.ini,
/etc/php.d/sysvmsg.ini,
/etc/php.d/sysvsem.ini,
/etc/php.d/sysvshm.ini,
/etc/php.d/wddx.ini,
/etc/php.d/xmlreader.ini,
/etc/php.d/xmlwriter.ini,
/etc/php.d/xsl.ini,
/etc/php.d/zip.ini

For more, use helping command php --help. It'll display all the possible options.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sathish
  • 3,477
  • 3
  • 26
  • 28
11

Sometimes things aren't always as they seem when in comes to configuration files in general. So here I'm applying my usual methods for exploring what files are opened by a process.

I use a very powerful and useful command-line program called strace to show me what's really going on behind my back!

$ strace -o strace.log php --version
$ grep php.ini strace.log

Strace digs out kernel (system) calls that your program makes and dumps the output into the file specified by -o.

It's easy to use grep to search for occurrences of file php.ini in this log. It's pretty obvious looking at the following typical response to see what is going on.

open("/usr/bin/php.ini", O_RDONLY)      = -1 ENOENT (No such file or directory)
open("/etc/php.ini", O_RDONLY)          = 3
lstat("/etc/php.ini", {st_mode=S_IFREG|0644, st_size=69105, ...}) = 0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thomas-peter
  • 7,738
  • 6
  • 43
  • 58
9

If you want all the configuration files loaded, this is will tell you:

php -i | grep "\.ini"

Some systems load things from more than one ini file. On my Ubuntu system, it looks like this:

php -i | grep "\.ini"

Output

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
Scan this dir for additional .ini files => /etc/php5/cli/conf.d
additional .ini files parsed => /etc/php5/cli/conf.d/apc.ini,
/etc/php5/cli/conf.d/curl.ini,
/etc/php5/cli/conf.d/gd.ini,
/etc/php5/cli/conf.d/mcrypt.ini,
/etc/php5/cli/conf.d/memcache.ini,
/etc/php5/cli/conf.d/mysql.ini,
/etc/php5/cli/conf.d/mysqli.ini,
/etc/php5/cli/conf.d/pdo.ini,
/etc/php5/cli/conf.d/pdo_mysql.ini
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Reece45
  • 2,771
  • 2
  • 17
  • 19
8

On OS X v10.9 (Mavericks), running:

$ php -i | grep 'Configuration File'

Returned:

Configuration File (php.ini) Path => /etc
Loaded Configuration File:         (none)

In the /etc/ directory was:

php.ini.default

(as well as php-fpm.conf.default)

I was able to copy php.ini.default to php.ini, add date.timezone = "US/Central" to the top (right below [php]), and the problem is solved.

(At least the error message is gone.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MikeiLL
  • 6,282
  • 5
  • 37
  • 68
  • 2
    Indeed, macOS (and OSX before it) never ships a `php.ini` file and relies on the PHP developers to have made sensible default choices (which is usually the case). But if you need to _change_ any setting, you have to do exactly what you've described. This actually saves a lot of time, since most users will never need to 'fix' their `php.ini` files... – Gwyneth Llewelyn Apr 12 '21 at 12:13
6

If you need to pass it to another app, you can do something like:

php --ini | grep Loaded | cut -d" " -f12

returns the path only. php -c $(php --ini | grep Loaded | cut -d" " -f12) will pass in the config file (useful for fpm)

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
5

Try this if it could help you all:

find / -type f -name "php.ini" 

This will output all files named php.ini.

Find out which one you're using, usually apache2/php.ini

Han Van Pham
  • 462
  • 1
  • 6
  • 24
5

The easiest way nowadays is to use PHP configure:

php-config --ini-dir

Output:

/usr/local/etc/php/7.4/conf.d

There's more you can find there. The --help sub command (macOS local install):

php-config --help

Output:

Usage: /usr/local/bin/php-config [OPTION]
Options:
  --prefixUsage: /usr/local/bin/php-config [OPTION]
Options:
  --prefix            [/usr/local/Cellar/php/7.4.11]
  --includes          [-I/usr/local/Cellar/php/7.4.11/include/php - …ETC…]
  --ldflags           [ -L/usr/local/Cellar/krb5/1.18.2/lib -…ETC…]
  --libs              [ -ltidy -largon2 …ETC… ]
  --extension-dir     [/usr/local/Cellar/php/7.4.11/pecl/20190902]
  --include-dir       [/usr/local/Cellar/php/7.4.11/include/php]
  --man-dir           [/usr/local/Cellar/php/7.4.11/share/man]
  --php-binary        [/usr/local/Cellar/php/7.4.11/bin/php]
  --php-sapis         [ apache2handler cli fpm phpdbg cgi]
  --ini-path          [/usr/local/etc/php/7.4]
  --ini-dir           [/usr/local/etc/php/7.4/conf.d]
  --configure-options [--prefix=/usr/local/Cellar/php/7.4.11 --…ETC…]
  --version           [7.4.11]
  --vernum            [70411]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kaiser
  • 21,817
  • 17
  • 90
  • 110
4

Save CLI phpinfo output into local file:

php -i >> phpinfo-cli.txt

Then open the file and find row Loaded Configuration File. It will contain path to actually loaded php.ini file eg.

Loaded Configuration File => D:\wamp\bin\php\php7.4.1\php.ini

Note, that PHP may load different php.ini file for CLI mode and another for HTTP mode.

lubosdz
  • 4,210
  • 2
  • 29
  • 43
  • How does that answer the question? Can you elaborate? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/57755449/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 19 '21 at 15:49
  • @PeterMortensen Sorry, I thought it was obvious. Answer updated. – lubosdz Aug 19 '21 at 18:24
3

From what I remember when I used to use EasyPHP, the php.ini file is either in C:\Windows\ or C:\Windows\System32

3

On Linux it is usually under /usr/bin/php

To find php.ini loaded in CLI on widows, run:

php.exe -i | grep "php.ini"

enter image description here

Ps. You can localize PHP installation folder with:

which php

enter image description here

or

whereis php

/c/laragon/bin/php/php-7.4.19-Win32-vc15-x64/php

DevWL
  • 17,345
  • 6
  • 90
  • 86
1

In a Docker container, "phpmyadmin/phpmyadmin". there isn't any php.ini file. But there are two files: php.ini-debug and php.ini-production.

To solve the problem, simply rename one of the files to php.ini and restart the Docker container.

Ivan
  • 241
  • 1
  • 10
-5

There is no php.ini used by the command line. You have to copy the file from ...EasyPHP-<<version>>\apache\php.ini to ...EasyPHP-<<version>>\php\php.ini and then edit the one in the php directory.

Reference:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
antitoxic
  • 3,746
  • 1
  • 37
  • 46
  • It would be better to quote the source: *"The root cause of the problem is that in EasyPHP 5.3 there isn't any* ***php.ini*** *file in the* ***php*** *folder."* – Peter Mortensen Aug 08 '21 at 14:40