83

I search the path where the php.ini file is located in our Linux Ubuntu server, and I found many php.ini files when executing the command find / -name php.ini. So how can I know exactly from a PHP script web page where the php.ini is located?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pheromix
  • 18,213
  • 29
  • 88
  • 158

7 Answers7

124
php --ini

For the webserver-SAPIs use phpinfo()

Here's some sample output:

bash-3.2# php --ini
Configuration File (php.ini) Path: /usr/local/php5/lib
Loaded Configuration File:         /usr/local/php5/lib/php.ini
Scan for additional .ini files in: /usr/local/php5/php.d
Additional .ini files parsed:      /usr/local/php5/php.d/10-extension_dir.ini,
/usr/local/php5/php.d/20-extension-opcache.ini,
/usr/local/php5/php.d/40-openssl.ini,
/usr/local/php5/php.d/50-extension-apcu.ini,
/usr/local/php5/php.d/50-extension-curl.ini,
/usr/local/php5/php.d/50-extension-gmp.ini,
/usr/local/php5/php.d/50-extension-imap.ini,
/usr/local/php5/php.d/50-extension-intl.ini,
/usr/local/php5/php.d/50-extension-mcrypt.ini,
/usr/local/php5/php.d/50-extension-mssql.ini,
/usr/local/php5/php.d/50-extension-pdo_pgsql.ini,
/usr/local/php5/php.d/50-extension-pgsql.ini,
/usr/local/php5/php.d/50-extension-propro.ini,
/usr/local/php5/php.d/50-extension-raphf.ini,
/usr/local/php5/php.d/50-extension-readline.ini,
/usr/local/php5/php.d/50-extension-xdebug.ini,
/usr/local/php5/php.d/50-extension-xsl.ini,
/usr/local/php5/php.d/60-extension-pecl_http.ini,
/usr/local/php5/php.d/99-liip-developer.ini
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • If you have conflicts (same property gets defined in multiple loaded .ini files) Does the additional .ini files overwrite the loaded configuration file or vice versa? – Lloyd Banks Sep 02 '18 at 17:43
  • Hi @LloydBanks Yes. I wouldn't name it "conflict", because it's by design, that you can define default values and override them in some environments. When I look into my local `/etc/php/conf.d` I can find filenames like `10-sqlite.ini`. The "10" is there, because the files are loaded in lexical order. With the prefix you can control the order and therefore control which (duplicate) properties takes precedence. – KingCrunch Oct 07 '18 at 20:45
88

You can use php_ini_loaded_file().

Taken from php.net:

$inipath = php_ini_loaded_file();
if ($inipath) {
    echo 'Loaded php.ini: ' . $inipath;
} else {
    echo 'A php.ini file is not loaded';
}

You may also want to check php_ini_scanned_files().

Also, you should note that if you run a PHP script from CLI, it's possible that a different php.ini file will be used than if a server (e.g., nginx or Apache) runs it.

Other options:

  • php -i|grep 'php.ini'
  • create info.php that contains <?php phpinfo(); in the webroot, and run it in your browser
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
15

Note that the configuration loaded for PHP when it's being used in the console is different from the configuration for the web process. Most PHP installations would come with a php.ini file for Apache and another for CLI.

To know which configuration file is used for the console commands, use

php -i | grep "Configuration File"

To know which configuration file is used for the web processes, follow these steps

  1. Create an info file (preferably in the root folder)
  2. The contents of this file should be <?php phpinfo(); ?>
  3. Ctrl + F and search for "Configuration File"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Igbanam
  • 5,904
  • 5
  • 44
  • 68
6

You have to use the PHP method php_ini_loaded_file (only after PHP 5.2.4)

php_ini_loaded_file — Retrieve a path to the loaded php.ini file

PHP website example:

<?php
$inipath = php_ini_loaded_file();

if ($inipath) {
    echo 'Loaded php.ini: ' . $inipath;
} else {
    echo 'A php.ini file is not loaded';
}
?>

Source: PHP site

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
darkheir
  • 8,844
  • 6
  • 45
  • 66
6

Just add a file like this:

<?php
    phpinfo();
?>

Then look for

Loaded Configuration File

You'll see the full path for the php.ini file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vahid Farahmand
  • 2,528
  • 2
  • 14
  • 20
3

Create a simple page and it will be listed there!

<?php

phpinfo();

?>
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
0

As mentioned, "find / -name php.ini" will help get all the php.ini files. The one you may be editing may not be the one from which the server is drawing the command from! I found php.ini for three different versions of PHP on my server and I got the fix instantly then.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Re *"I got the fix instantly"*: Can you elaborate on what you did? How did you decide on which one to use? – Peter Mortensen Nov 04 '21 at 04:32
  • This isn't a reliable method on many server setups where there could be many copies of the file, not all of which are being used. – AdamJones Sep 06 '22 at 11:37