Often when dealing with Web Hosts, we find it necessary to get original php.ini file for configuring some of the PHP settings. Without the original php.ini file, we face several problems if we want to change a specific settings value (say memory limit for example). Getting the original file can either be done by emailing your host and waiting for their respond or directly using the small trick we have discussed here. Don’t worry, it is completely safe to perform all these steps. We have used our localhost server to show the demonstrations. It can be used on almost any servers. So let us see how.
#1: Getting the path of original php.ini:
This is the most important step. We need to find out the absolute path of the original php.ini file from where the default configurations are loaded. To do this we start by creating a file with the following code:
Name the file anything (phpinfo.php) and save it inside the root of your server (in most of the cases directly inside the public_html directory). Now, if your website address is, say, http://yourdomain.com then execute the file by going to http://yourdomain.com/phpinfo.php, where you need to replace the phpinfo.php URI with whatever you’ve used.
As we are using localhost, so we go directly to http://localhost/phpinfo.php
This should give you an output like this:
Note the highlighted Loaded Configuration File thing on that page. This gives you the path of the original php.ini file (If you have not uploaded any .ini file to your directory yet).
In our localhost, as we can see, it is /etc/php5/apache2/php.ini, but I have seen, in most of the web hosts, it is something like /usr/local/lib/php.ini. So, whatever is the value, just note it down for further use.
#2: Reading the located php.ini file:
Now that we have the path of the original php.ini file, all we need to do is write a small script which will read the content of the ini file and will output it to our browser for direct copy pasting. Copy the PHP code from below and save it inside a file name getphpini.php.
<?php //in most of the hosts, it is '/usr/local/lib/php.ini' //here we have used the value for our localhost $location = '/etc/php5/apache2/php.ini'; $default = file_get_contents($location); echo '<pre>'; echo htmlspecialchars($default); echo '</pre>';
Do change the variable $location value to what you have noted before. Here we have used the value for our localhost default configuration php.ini path.
Now upload the file to the public_html and access it through your website URL. In our case, it will be http://localhost/getphpini.php. Now we shall see some output like this.
That’s it. It is the default php.ini file of your host. Simply copy the whole text (Ctrl + C) and paste it inside some file named php.ini, and you are good to go.
#3: Modifying the php.ini file:
Most of the modern web hosts let you to upload your own set of php.ini file in the root directory. But the php.ini file should be complete, in the sense, it should have all the extension and other information written into it, which varies from webhost to webhost. That is why we had to get the original php.ini file. Now that we have it, it is only a matter of editing it and uploading it to the root directory (public_html directory) to get our own configuration values up and running.
For example, let us change the allocated memory to the PHP scripts:
#3.1: Example: Changing the memory limit:
- Search for the line memory_limit =
- In most of the cases it has a default of 32M memory allocation. So the line looks like memory_limit = 32M.
- Change it to something like memory_limit = 128M to set the memory limit to 128 MB (which is standard for reputed webhosts).
- Save the php.ini file.
Now upload it to the root (public_html) directory and you now have set your PHP scripts to consume a maximum of 128 MB of memory.
Note: Setting higher memory limit is not a good way to run your PHP scripts. Heavy applications, like WordPress, run smoothly under 32MB of memory. If anything is causing more memory consumption, then it should be tracked down for better performance.
#3.2: Checking & Validating the modified php.ini file:
Now if we wish to check whether the uploaded php.ini file we simply load the phpinfo file again through our URL (for our localhost, it had been http://localhost/phpinfo.php). Now if we check it again, we shall see a different value for the field Loaded Configuration File. It should correspond to the absolute path of our uploaded php.ini file.
In our case it is just the file we had uploaded. So our configuration has been loaded successfully. But if you can not see the path of your uploaded file, then you might have uploaded it to the wrong directory or your host might not support it. In both the cases you should contact your host to get things sorted.
Note: In my localhost server, I had it set to get the php.ini file from the path shown in the screenshot. It is not there by default (in case of default server setup). To do this we need to edit the virtual host files of apache and add the information there.
#4: Restoring to the default ini file:
This is the simplest. Just delete the php.ini file you have uploaded and you are back to the default configuration.
So that was all about hackish way for getting the original php.ini file of your web host. Hope you find it useful. Today when I was debugging our intechgrity.com site (as one of the plugin was consuming loads of memory) I needed immediate access to the original php.ini file and used the came up with the trick to get it myself. If you have any trouble using it then feel free to ask. Also let us know what do you think of this trick.