Imagine you are given a server and you are asked to change its PHP settings. Also, assume that several different versions of PHP are installed on this server. What do you think is the best way to find a PHP configuration file? You will find the answer to this question below.
Method 1 (phpinfo
file):
#
One way to find out exactly which php.ini file your web sever is using is by creating a new PHP file in document root called info.php
<?php phpinfo(); ?>Load this file in your browser, press CTRL + F and search for “Loaded Configuration File”. You should see something like.
This will tell you the exact location of the php.ini file you want to edit.
Method 2 (PHP.ini & grep command) #
In Linux, run this command to locate the PHP.ini configuration file.
$ php -i | grep "Loaded Configuration File"Or better way:
Or in Windows Command Line:
$ php -i | findstr /c:"Loaded Configuration File"The result should be something like this.
$ Loaded Configuration File => /etc/php/7.4/cli/php.iniMethod 3 (locate command) #
Using the locate command in Linux. If it’s not already installed, run sudo apt install mlocate [On Debian/Ubuntu] and sudo yum install mlocate [On CentOS/RHEL]
$ locate php.iniYou should see a list of php.ini files here.
How install PHP extension (like Zip) on Cantos / Ubuntu / Linux
There are several ways to find the php.ini file location in Ubuntu and Centos Linux servers. It is also interesting to note that there are usually several php.ini files on the server. Here will show you how to find the file and determine which one is the active configuration file.
Using Locate to Find php.ini File Location
Use the following command to get a list of all of the php.ini files.
$ locate php.iniThis will give an output similar to this:
/etc/php/7.2/apache2/php.ini /etc/php/7.2/cli/php.ini /etc/php/7.2/fpm/php.ini /etc/php/7.3/cli/php.ini /etc/php/7.4/apache2/php.ini /etc/php/7.4/apache2/php.ini.save /etc/php/7.4/apache2/php.ini.ucf-dist /etc/php/7.4/cli/php.ini /etc/php/7.4/cli/php.ini.ucf-dist /etc/php/7.4/fpm/php.ini /etc/php/7.4/fpm/php.ini.ucf-dist /etc/php/8.0/cli/php.ini /usr/lib/php/5.6/php.ini-development /usr/lib/php/5.6/php.ini-production /usr/lib/php/5.6/php.ini-production.cli /usr/lib/php/7.0/php.ini-development /usr/lib/php/7.0/php.ini-production /usr/lib/php/7.0/php.ini-production.cli /usr/lib/php/7.1/php.ini-development /usr/lib/php/7.1/php.ini-production /usr/lib/php/7.1/php.ini-production.cli /usr/lib/php/7.2/php.ini-development /usr/lib/php/7.2/php.ini-production /usr/lib/php/7.2/php.ini-production.cli /usr/lib/php/7.3/php.ini-development /usr/lib/php/7.3/php.ini-production /usr/lib/php/7.3/php.ini-production.cli /usr/lib/php/7.4/php.ini-development /usr/lib/php/7.4/php.ini-production /usr/lib/php/7.4/php.ini-production.cli /usr/lib/php/8.0/php.ini-development /usr/lib/php/8.0/php.ini-production /usr/lib/php/8.0/php.ini-production.cliThe above output may not be very helpful in drilling down to the active php.ini file location.
You can use the PHP CLI to get the configuration file that is being used in your setup with the following command:
$ php -i | grep 'php.ini'From the output look for the line similar to this:
Loaded Configuration File => /etc/php/7.4/cli/php.iniYou can also use the following to retrieve the same output:
With the expected result being:
/etc/php/7.4/cli/php.iniYou can use PHPs phpinfo function as follows:
$ php -r "phpinfo();" | grep php.iniWith the following as the result.
Configuration File (php.ini) Path => /etc/php/7.4/cli Loaded Configuration File => /etc/php/7.4/cli/php.iniIn case you need to get the php.ini file location for the settings used by the webserver you will need to use the following function from within a PHP file created in the web server document root and accessed from a web browser.
echo php_ini_loaded_file().PHP_EOL;There you have it you should now be able to find the php.ini file location in Ubuntu and Centos Linux servers including determining the one that is active.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.