Where can I find php.ini?
Asked Answered
T

14

1084

Today I needed to install the IBM DB2 library. I went through all the steps up to make install, and I found ibm_db2.so in $PHP_HOME/lib/extensions/somecomplicatedname/ibm_db2.so.

The great catch is the last step is to configure file php.ini, but there aren't any php.ini files on my system. PHP works fine, except of course for this newfangled ibm_db2 thingamajig. (I tried a small PHP script which fails and indicates that the ibm_db2 functions are not available.)

How would I go about finding or setting up the php.ini file so that I can enable the library I need?

Tenatenable answered 30/12, 2011 at 22:20 Comment(6)
90% of the time it's /etc/php5/apache2/php.iniAustronesian
Also see https://mcmap.net/q/48412/-how-can-i-find-the-php-ini-file-used-by-the-command-line/632951Cadmann
The premise of the question is wrong. "there is NO php.ini on my system" No, you simply failed to locate it. Speaking of which, locate php.ini will tell you in mere moments where the file is on your system.Moon
@Adam: unless it isn't PHP5 ;)Nevels
If you're on PHP 7 look @ /etc/php/7.0/apache2/php.iniPreconscious
php -i | grep "Loaded Configuration File" | sed 's#.* ##' | xargs vim for easy editingEcesis
P
0

according to this answer, as of PHP 7 the regular php.ini file was removed and added with php.ini-production and php.ini-devlopment.

so instead of php.ini which does not exist in my case (I've installed php 8.1), use php.ini-production and it's located in php installation folder (something like: C:\PHP-8.1.5) and create a file and name it php.ini and then copy contents of php.ini-production in this new php.ini.

Psalter answered 28/4, 2022 at 15:2 Comment(0)
B
1083

On the command line execute:

php --ini

You will get something like:

Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/curl.ini,
/etc/php5/cli/conf.d/pdo.ini,
/etc/php5/cli/conf.d/pdo_sqlite.ini,
/etc/php5/cli/conf.d/sqlite.ini,
/etc/php5/cli/conf.d/sqlite3.ini,
/etc/php5/cli/conf.d/xdebug.ini,
/etc/php5/cli/conf.d/xsl.ini

That's from my local dev-machine. However, the second line is the interesting one. If there is nothing mentioned, have a look at the first one. That is the path, where PHP looks for the php.ini file.

You can grep the same information using phpinfo() in a script and call it with a browser. It’s mentioned in the first block of the output. php -i does the same for the command line, but it’s quite uncomfortable.

Backandforth answered 30/12, 2011 at 22:22 Comment(10)
accepting oldest and previously the simplest answer (SO forces me to wait a while before accepting). thanks for the expansion which will probably help the next person looking for information. for me the 2nd line said (none), and everything worked fine after putting a php.ini file in the path on the first line. thanks!Tenatenable
I used this command and edited the php.ini file that it directed me too and my PHP settings were not changing and I realized it was not giving me the correct php.ini file. I tried using the phpinfo() PHP function and it give me the actual correct location of the php.ini I needed to edit. I think this is very important and I hope this helps someone.Palocz
@Palocz Thank you for pointing out the problem you encountered in practice. I have changed the accepted answer to one that highlights creating a .php file. By the way, if you comment on the question then I get notified and I can fix the problem sooner.Tenatenable
@KingCrunch, Are you sure this is going to work when you have more than one PHP installation?Cadmann
@Cadmann Yes. Just make sure you execute the right executable: /path/to/one/php --ini and /path/to/another/php --ini.Backandforth
@KingCrunch, Then what about thecommonthread's comment above?Cadmann
@Cadmann This has nothing to do with different PHP installations, but the different SAPIs can use different ini files. I don't use the apache module, but as far as I remember I think there is no way around a phpinfo.php-like file. With FPM you can at least use php5-fpm -i | grep '\.ini'. Usually you use this two or three times and at the end of the day you know where the files are ;)Backandforth
Also on some lamp stacks this will return you only the php-cli php-ini.Windermere
I ended up editing the wrong ini file too, wasting an hour. This may be my own stupidity, but this is a warning to people like me- if it's not working create a <?php phpinfo(); ?> as above because that fixed it for meAlexio
/etc/php/8.2/apache2/php.ini is the desired one php --ini dont showDonothingism
O
546

The best way to find this is:

Create a PHP (.php) file and add the following code:

<?php phpinfo(); ?>

and open it in a browser. It will show the file which is actually being read!

Updates by the OP:

  1. The previously accepted answer is likely to be faster and more convenient for you, but it is not always correct. See comments on that answer.
  2. Please also note the more convenient alternative <?php echo php_ini_loaded_file(); ?> mentioned in this answer.
Oversweet answered 19/6, 2013 at 7:32 Comment(8)
This should be the accepted answer. If you have CLI installed and have apache running then it may have its own ini file (as my system does). Using the accepted answer returns what the CLI is using rather than apache's.Shoer
@Shoer Yes, you are right. I am changing the accepted answer to this one because the problem you mention was also encountered somebody in practice (who commented on the previously accepted answer). (By the way, if you had commented on the question I would have fixed this problem sooner.)Tenatenable
This does not work on my installation. Typo? Should be <?php phpinfo(); ?>Nedneda
php -r \@phpinfo\(\)\; works for me php -r \@phpinfo\(\)\; | grep 'PHP Version' -m 1 to pull specific infoFrostbitten
@zylstra Try: <?php echo php_ini_loaded_file(); ?>Ingvar
the problem with this is when php does not want to loadHolbein
maybe mention the very convenient php -i | grep "Loaded Configuration File" | sed 's#.* ##' | xargs vim for easy editing or php -i | grep "Loaded Configuration File" | sed 's#.* ##' | xargs cat for easy displaying?Ecesis
Re "it will show the file which is actually being read": Can you be more specific in your answer what to look for? E.g., is it the line with "Loaded Configuration File"?Haro
R
167

This works for me:

php -i | grep 'php.ini'

You should see something like:

Loaded Configuration File => /usr/local/lib/php.ini

P.S.

To get only the php.ini path, use:

php -i | grep /.+/php.ini -oE
Romeu answered 2/4, 2013 at 11:48 Comment(5)
This only works, if there is a php.ini, which is exactly the problem: If there is none, you still don't know where to look at, or where to place a new one.Backandforth
I've never come across a situation where there is no php.ini file.Romeu
Running this command gives me Configuration File (php.ini) Path => /usr/lib, but running <?php phpinfo(); ?> gives me Loaded Configuration File: /etc/php.ini. So, phpinfo() was more reliable in my case.Shumpert
This is not entirely correct. Every framework that uses the PHP language has it's own php.ini file. If you call that line from the command line, you will probably end up receiving something like /var/php5/cli/php.ini which is not the file used when running PHP on a web environment. For that file, you should look into the apache2 folder.Basenji
is possible get the output php -i | grep 'php.ini' from apache? (script.php)Artemus
L
74

In a command window, type

php --ini

It will show you the path something like:

Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File:         /usr/local/lib/php.ini

If the above command does not work then use this:

echo phpinfo();
Lomax answered 9/10, 2015 at 12:36 Comment(0)
F
43

Use the following command to find the php.ini file path on Linux.

locate php.ini

Output:

/etc/php.ini
/etc/php.ini.rpmnew
/usr/share/doc/php-common-5.4.45/php.ini-development
/usr/share/doc/php-common-5.4.45/php.ini-production

Or try this other way:

php --ini

It shows the path result.

Flagstone answered 17/2, 2016 at 10:58 Comment(2)
This is inferior to methods like php -i | grep 'php.ini', because it finds any php.ini and doesn’t tell you which one your PHP installation uses. Also, it may give wrong results when the locate DB is not up to date.Boilermaker
locate is not installed by default on Ubuntu MATE 20.04 (Focal Fossa).Haro
H
36

This command should help you to find it

php -r "phpinfo();" | grep php.ini
Hymn answered 9/1, 2015 at 15:41 Comment(1)
I like this a lot more than some of the other answers. It doesn't require that I create, locate a php file with those contents and load up the page in the browser.Phantasmal
U
31

PHP comes with two native functions to show which configuration file is loaded:

Depending on your setup, Apache and CLI might use different .ini files. Here are the two solutions:

Apache:

Just add the following in a PHP (.php) file and open it in your browser:

print php_ini_loaded_file();
print_r(php_ini_scanned_files());

CLI:

Copy-paste in your terminal:

php -r 'print php_ini_loaded_file(); print_r(php_ini_scanned_files());'
Unblushing answered 21/6, 2014 at 14:19 Comment(3)
Under-appreciated, thorough, and (almost) correct answer. I almost switched this to the correct answer but I am worried that php_ini_loaded_file may not always be available.Tenatenable
@zylstra what was your php version? the docs for php_ini_loaded_file say php version >= 5.2.4Tenatenable
@Tenatenable Not sure now. Forgot which system I was checking. :/Subtotal
E
27
phpinfo();

will tell you its location, or from the command line

php -i
Enki answered 30/12, 2011 at 22:24 Comment(0)
U
22

Try one of these solutions

  1. In your terminal, type find / -name "php.ini"

  2. In your terminal, type php -i | grep php.ini. It should show the file path as "Configuration File (php.ini) Path => /etc"

  3. If you can access one of your PHP files, open it in a editor (Notepad) and insert phpinfo(); after <?php on a new line. This will tell you the php.ini location.

  4. You can also talk to PHP in interactive mode. Just type php -a in the terminal and type phpinfo(); after the PHP interpreter initiated.

Urinary answered 7/6, 2019 at 16:57 Comment(1)
points 1, 2, and 4 are vulnerable to multiple installation problems; there are already great answers. no need to add an answer that doesn't add anything here.Tenatenable
E
21

Run this in the command line:

php -r "echo php_ini_loaded_file().PHP_EOL;"
Expanded answered 24/6, 2020 at 21:30 Comment(0)
O
7
find / -name php.ini

Hey... it worked for me!

Obtund answered 22/12, 2014 at 7:22 Comment(3)
for one, it takes a while; for two, if you have more than one, how do you know which is the one actually in use?Tenatenable
True, php --ini works, but this is just another option :) There is usually a /etc/php5/cli/php.ini /etc/php5/apache2/php.ini /etc/php5/cgi/php.ini and usually which one you want is pretty obvious. (cli, apache, cgi)Obtund
This is not a good solution, because we are not intended to find all the php.ini file on disk.Hypochromia
B
4

You can get more information about your configuration files using something like:

$ -> php -i | ack config # Use fgrep -i if you don't have ack

Configure Command =>  './configure'  ...
Loaded Configuration File => /path/to/php.ini
Bess answered 30/12, 2011 at 22:25 Comment(0)
L
4

For SAPI: php-fpm

There isn't any need to create a php.info file (it is not a good policy to leave it for the world to read anyway). On the command line:

php-fpm -i | more

Somewhere in its output, it will show this line:

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

Here is a more complete explanation: How to Figure out Your PHP Configuration Parameters without info.php

Leggat answered 17/11, 2015 at 15:16 Comment(0)
P
0

according to this answer, as of PHP 7 the regular php.ini file was removed and added with php.ini-production and php.ini-devlopment.

so instead of php.ini which does not exist in my case (I've installed php 8.1), use php.ini-production and it's located in php installation folder (something like: C:\PHP-8.1.5) and create a file and name it php.ini and then copy contents of php.ini-production in this new php.ini.

Psalter answered 28/4, 2022 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.