Where can php.ini be Found

This tutorial is dedicated to an issue that almost every developer may come across. It’s where to find the php.ini file.

As multiple versions of PHP exist with different server configurations, the php.ini file may be placed in different folders.

Here, we will provide you with three methods that will help you to find php.ini in a straightforward way.

Watch a course Learn object oriented PHP

Method 1: Creating a new PHP File

The first method to find the php.ini file is creating a new PHP file called info.php. Here is how you can implement it:

<?php
phpinfo();
?>

You need to load this file in the browser, pressing CTRL + F and looking for “Loaded Configuration File”. You may see what is demonstrated below:

/etc/php/7.2/apache2/php.in

It will tell you the exact location of the php.ini file.

Method 2: Using php -i

For locating the php.ini configuration file, you can run the code below in Linux:

$ php -i | grep "Loaded Configuration File"

In the Windows Command Line, you can invoke:

php -i | findstr /c:"Loaded Configuration File"

The output will look like this:

Loaded Configuration File => /etc/php/7.2/cli/php.in

So, from the example mentioned above, we found out that the PHP install is placed inside /etc/php/7.2. But, there is an essential thing to consider: there exist three distinct configuration files. Let’s take a look at them:

CLI

For the CLI PHP program, there exists /etc/php/7.2/cli/php.ini. Modifications in this config file will only impact PHP because it operates within the terminal. Hence, the web server will not be affected by that.

APACHE

For the plugin used for APACHE, there exists /etc/php/7.2/apache2/php.ini. You are required to edit it once you work with the APACHE web server.

Nginx either Apache with PHP-FPM

Now, let’s have a look at /etc/php/7.2/apache2/php.ini. It is considered a fastcgi-compatible ‘wrapper’. You are required to edit it once you work with Apache with PHP-FPM or Nginx web server.

Method 3: Using the locate Command

The third method is using the locate command in Linux. If you haven’t installed it yet, you can act like this:

sudo apt update && sudo apt install mlocate

Then, run the locate command as follows:

$  locate php.ini

After running this command, you will notice the list of php.ini files.

How to Edit php.ini in Linux

In this section, we will demonstrate how to edit php.ini on APACHE and Nginx or Apache with PHP-FPM.

APACHE

As a rule, the php.ini file is located in /etc/php/7.2/apache2/php.ini on APACHE. You can replace it with the version that you use. For example, php 7.1, and so on.

For editing, you can act like this:

sudo nano /etc/php/7.2/apache2/php.ini

If you work with PHP FPM, it can be placed in /etc/php/7.2/fpm/php.ini. So, change the 7.2 version to your own version.

For editing, you can act like this:

sudo nano /etc/php/7.2/fpm/php.ini

Afterwards, you need to press CTRL + X , pressing Y and then - ENTER. In this way, you will save the file and exit it.

After alerting php.ini, you need to restart APACHE.

If you use PHP-FMP, you should restart it.

Nginx or Apache with PHP-FPM

As a rule, Nginx works with PHP-FPM. The php.ini file is placed inside /etc/php/7.2/fpm/php.ini. You need to replace the 7.2 version with your own one.

sudo nano /etc/php/7.2/fpm/php.ini

For saving the file, you need to press CTRL + X. Afterwards, press Y and then ENTER.

After the alert of the php.ini file, it is necessary to restart Nginx like this:

After the alert of the php.ini file, it is necessary to restart Nginx like this:

sudo service nginx restart

So, in this tutorial, we represented to you three different and efficient ways to find the php.ini file. After checking out all the examples above, you can choose the best one for you.