How to Find the Correct php.ini File of Apache2 in Ubuntu?

The “php.ini” file is an essential configuration file for the PHP plugin utilized by Apache. It can change the configuration settings, register global variables, and display log errors. The system reads the “php.ini” file every time the PHP is initialized or the server starts. The PHP configured the default server when it was installed.

This post shows possible ways to find the correct path of the “php.ini” file of the apache2 server in Ubuntu.

Note: To install Apache 2 in Ubuntu, follow our article “How to Install Apache 2 in Ubuntu?”

Method 1: Using the “ls” Command 

The “ls” is the commonly used command line utility to list down the content of a file or directory. It can also be utilized to find out the correct path of the file.

Navigate Apache2 Directory:

First, execute the “cd” command to move into the “/etc/php/8.1/apache2” directory:

$ cd /etc/php/8.1/apache2

Find “php.ini” File:

Run the “ls” command to find the “php.ini” file in the “apache2” directory:

$ ls

The “php.ini” configuration file has been found in the “/etc/php/8.1/apache2” directory.

Method 2: Using the “locate” Command

The “locate” is an external command line tool to find the files/directories from the Linux database. Whereas the “find” command only searches from the system directories. That’s why it is more efficient and reliable than the “find” command.

Install “mlocate” Tool

First, install the “mlocate” command line utility in Ubuntu using the default “apt” package manager:

$ sudo apt install mlocate

The “mlocate” command utility has been successfully installed in Ubuntu 22.04. 

Find “php.ini” File

Specify the targeted “php.ini” file with the “locate” command to find its correct path:

$ locate php.ini

The output shows the list of “php.ini” file paths from which the first line shows the correct path of the “php.ini” file of apache2.

Method 3: Using the “grep” Command

The “grep” command allows the user to search a file for the specified pattern of characters & string. This also shows the lines that contain the defined pattern.

Run the “grep” command to search out the “php.ini” configuration file of the Apache2 server:

$ php -i | grep "Loaded Configuration File"

The “Loaded Configuration File” symlink shows the correct path of the “php.ini” file through the command line interface.

Method 4: Using the “php_file” File

The “php.ini” file information can also be accessed using the new file with the “.php” extension. It is stored in the “/var/www/html” directory. Let’s see how the “.php” script is created to find the “php.ini” path and other information.

Open the terminal and create a new “php_file.php” file in the “nano” editor by executing the following command:

$ sudo nano /var/www/html/php_file.php

Write the following content into it:

Script

<?php
print_r (phpinfo());
?>

The “<?php” and “?>” is the opening and ending tags of the script. The “print_r” displays the output of the result of “phpinfo()”:

Press “Ctrl+S” to save and “Ctrl+X” to exit the file.

Now open the “http://localhost/php_file.php” file in the default browser of Ubuntu, i.e., Firefox. In this case, the “Apache” server is set up to find the php files from the “/var/www/html” directory and process them:

The correct location of the “php.ini” file is placed in the “Loaded Configuration File” section.

Conclusion

In Ubuntu, the “ls”, “locate”, and “grep” command-line utilities get the absolute path of the “php.ini” configuration file. The user can also perform this task by creating the php_file. The “php.ini” file is stored in the “etc/php/8.1/apache2” directory. This post has provided all possible methods to find the correct path of the “php.ini” file of apache2 in Ubuntu.