Edit php.ini file in ubuntu

Method 1

One way to find out exactly which

/etc/php/8.1/apache2/php.ini
2 file your web sever is using is by creating a new PHP file in document root called
/etc/php/8.1/apache2/php.ini
3.

info.php

<?php
phpinfo();
?>

Load this file in your browser, press

/etc/php/8.1/apache2/php.ini
4 +
/etc/php/8.1/apache2/php.ini
5 (or
/etc/php/8.1/apache2/php.ini
6 +
/etc/php/8.1/apache2/php.ini
5 on Mac) and search for “Loaded Configuration File”. You should see something like

/etc/php/8.1/apache2/php.ini

This will tell you the exact location of the php.ini file you want to edit.

Method 2

In Linux, run this command to locate the

/etc/php/8.1/apache2/php.ini
8 configuration file.

php -i | grep "Loaded Configuration File"

Or in Windows Command Line:

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

The result should be something like this:

Loaded Configuration File => /etc/php/8.1/cli/php.ini

In the above example, we can see that the PHP install is located in

/etc/php/8.1/apache2/php.ini
9. Note that there are three different configuration files you should we aware of:

CLI

php -i | grep "Loaded Configuration File"
0 is for the CLI PHP program. Changes to this config file will only affect PHP as it runs in the terminal – it will NOT affect the web server.

Apache

php -i | grep "Loaded Configuration File"
1 is for the PHP plugin used by Apache. This is the one you need to edit if you are using the Apache web server.

Nginx or Apache with PHP-FPM

php -i | grep "Loaded Configuration File"
2 is a fastcgi-compatible ‘wrapper’ for PHP processing. This is the one you need to edit if you’re using the Nginx web server or Apache with PHP-FPM.

Method 3

Using the

php -i | grep "Loaded Configuration File"
3 command in Linux,. If it’s not already installed, run  
php -i | grep "Loaded Configuration File"
4.

locate php.ini

You should see a list of php.ini files here. Try editing one of them and restarting you web server to see if makes the required changes.

Editing php.ini in Linux

Apache

On Apache, 

/etc/php/8.1/apache2/php.ini
2 is usually located in 
php -i | grep "Loaded Configuration File"
1. Replace 
php -i | grep "Loaded Configuration File"
7 with your own version, e.g, 
php -i | grep "Loaded Configuration File"
8, 
php -i | grep "Loaded Configuration File"
9, etc.

To edit:

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

However, if you are using PHP FPM, it may be located in 

php -i | grep "Loaded Configuration File"
2. Replace 
php -i | grep "Loaded Configuration File"
7 with your own version, e.g, 
php -i | grep "Loaded Configuration File"
8, 
php -i | grep "Loaded Configuration File"
9, etc.

To edit:

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

To save file and exit, press 

/etc/php/8.1/apache2/php.ini
4 + 
php -i | findstr /c:"Loaded Configuration File"
5, press 
php -i | findstr /c:"Loaded Configuration File"
6 and then press 
php -i | findstr /c:"Loaded Configuration File"
7

You must restart Apache after altering 

/etc/php/8.1/apache2/php.ini
2.

sudo systemctl restart apache2

If you are using PHP-FPM, you must restart that service. Replace 

php -i | findstr /c:"Loaded Configuration File"
9 with your own version, e.g, 
php -i | grep "Loaded Configuration File"
8, 
php -i | grep "Loaded Configuration File"
9, etc.

What is the php.ini file, why we use it ? Where do I find this file on Ubuntu ? How I open and edit the php.ini file on a terminal. This is the most common questions asked by beginners who are either new to PHP or Linux.

php.ini File

The php.ini file is a default configuration file used when your PHP application runs. When PHP starts it reads the configuration setting from the php.ini file.

If you have installed LAMP on a Ubuntu operating system and unable to find and edit this file then this tutorial is for you.

PHP Script to Find php.ini Location

If you are working on  Windows, Ubuntu or any operating system. You can easily find the location of php.ini configuration file using simple PHP script.

Create a PHP file. Let’s name it inipath.php (you can choose any name with a .php extension) .

Java

1

2

3

4

5

<?php

 

phpinfo();

 

?>

Open this file in your browser and you will get the path of  the php.ini.

Edit php.ini file in ubuntu

I am using Ubuntu and the location of php.ini in Ubuntu is /etc/php5/apache2/php.ini . Similarly, if you are running this script on Windows and Mac you’ll get the path as per their directory structure.

For version 7.4.3, the php.ini path is /etc/php/7.4/apache2/php.ini.

Edit php.ini file in ubuntu

php.ini File Location in Ubuntu

For PHP5

The path of php.ini in Ubuntu is /etc/php5/apache2 . Open the terminal and start typing following commands.

Java

1

2

3

$ cd  /etc/php5/apache2

 

$ ls

To view the files present in apache2 directory, type ls command. The files present in this directory (conf.d and php.ini) are displayed.

For PHP 7.4 version

The path of php.ini is

Java

1

2

3

$ cd  /etc/php/7.4/apache2

 

$ ls

If you are not familiar with Linux commands then read my previous post on Linux Commands with Examples .

The other easiest way to locate the php.ini location is using locate command.

Java

1

$ locate php.ini

How to open & edit the php.ini configuration file on a Terminal

To open & edit the php.ini file I am using vim/vi editor.

Read more about Beginners guide for vi/vim editor.

Java

1

$ sudo vim php.ini

You can directly open this file using the following command.

Java

1

$  sudo vim /etc/php5/apache2/php.ini

NOTE – Once you have done the changes in the php.ini file. You need to restart the apache web server so that the new changes get reflected.

To restart the Apache WebServer.

Java

1

$ sudo /etc/init.d/apache2 restart

or

Java

1

$ sudo service apache2 restart

Abstract vs Interface in PHP

Abstract class in PHP

Programming Videos Tutorial

Conclusion

I hope this tutorial helped you to find and edit the php.ini file. If you are still facing any issue you can ask your question through comments.

How to edit a PHP file in Ubuntu?

To open the default php. ini file for editing, use one of the following commands (depending on which Linux distribution you're using): Ubuntu 16.04: sudo nano /etc/php/7.0/apache2. CentOS 7: sudo nano /etc/php.

How to edit the PHP ini file?

Modifying the PHP..
Login to the cPanel..
Find the File Manager in File section of the cPanel. ... .
Navigate to the directory where you will either save or edit the PHP. ... .
Edit the section of the PHP. ... .
Click on SAVE CHANGES in the top right hand corner in order to save your modifications or additions to the file..

Where is the PHP ini file in Ubuntu?

Where is my PHP php..
In Linux, run this command to locate the PHP. ini configuration file..
To edit:.
Nginx uses PHP FPM and php. ... .
For versions of Ubuntu lower than 16.04, /etc/php/5.6/ , /etc/php/7.0/ , /etc/php/7.1/ , and so on, are replaced by /etc/php5/ and so on..

How do I open a .ini file in Linux terminal?

To open php. ini file in Linux terminal and just use vim or vi command. For example : vim /etc/php. ini.