Setting custom php.ini files for your website
Written by Mourad Dmeiri Thursday, 25 November 2010
Many of us have wondered, how can we change some of the PHP directives to match some script requirements? Even though this might sound complicated, it is much easier than it looks.
First, we should know a few facts about PHP directives and where they reside. We have 3 levels of php.ini file levels:
1) Server global php.ini (this file resides in the server configuration and this cannot be edited by the users in a shared hosting environment).
2) User global php.ini (this file usually resides in the main folder for a user website, like public_html and this serves all the php scripts that fall under the user account).
3) User local php.ini file (this file is not limited to a specific path and can be deployed by the user wherever he feels he needs to change some parameters for some script).
Now, an interesting question arises: how does this work? The answer is pretty simple. PHP interpreter looks for the php.ini files from down upwards, meaning it looks for a user local file, if the file is not there, it looks in the folder above and then above until it reaches the main folder for the user account. If it fails to find any custom user php.ini file, the interpreter reverts to the global php.ini file for directives.
This is very useful for creating custom php.ini files. You can have the global file serve all your PHP directive needs. If you need a certain directive overwritten (we will show how soon), you simply create a php.ini file in the folder where your script resides, and add that directive. The changes will only affect script files running on this folder and any subfolders below that folder. Parent folders will revert to the php.ini file in their respective folder or their parent folder, and so on.
Now, how to create a php.ini file? This also seems like rocket science (this analogy is obsolete nowadays) but it is very simple. There are 2 ways of doing this:
- Login into your control panel, open your favorite file manager, in my case, it is “file manager”.
- Navigate to the folder you wish to add the file, click on “new file”, enter the name of the file which is php.ini (please note that this is the only acceptable file name, all lowercase).
- Open the file in the editor by clicking on the edit button. And add the parameters you wish to change in the following order:
variable = value;
variabl2 = value2;
In my case, it is the following:
max_execution_time = 60;
memory_limit = 256M;
upload_max_filesize = 256M;
post_max_size = 256M;
- Save the file and exit.
The second way you can add this file is by logging onto your FTP and adding the file or uploading it after creating it locally.
The last portion of this tutorial will discuss how to check if your changes have been reflected or not and what php.ini file is actually being used to process scripts in a specific location. You can create a PHP file (using the same steps as above) with the following content:
You have to go to your browser and open the PHP file. This script will simply display all the information for your PHP environment. We are now interested in one field, the “loaded configuration file”. This directive will show you which file is being used to obtain the directives for the PHP file you just created and executed.
Please note that you only need to include the directives which values you want changed and not all the values that are globally defined.