The advantages of using php with apache web server are many. The wonderful file named .htaccess makes things easier by allowing the webmaster (thats you!) to play around with many settings which would otherwise need the php.ini to be edited. This is a boon for people having their websites hosted on a shared server. Such people can’t even dream of editing the php.ini file located on their web hosting provider’s server. Thus the concept of custom php.ini was born. Webmasters who have their website hosted on a server with support for custom php.ini can create a php.ini within their directory and provide all the necessary settings they require. But custom php.ini doesn’t work out-of-the-box. This article explains how to use custom php.ini on a shared hosting server.
Note:- First ask your web hosting provider whether they support custom php.ini. If they don’t there is no point in trying anything further.
Step 1:- Create a php.ini file
Create a file named php.ini inside your root directory. By root directory I mean the directory outside your document root. If you are confused here is the clear explanation, if you upload files inside /home/username/public_html then it is your document root, you should create the php.ini inside /home/username for your safety and security.
Step 2:- Create a .htaccess file
Even for custom php.ini feature to work the infamous .htaccess is required. Create a .htaccess file inside your root directory i.e. the directory outside your document root if you upload your website file at /home/username/htdocs then the htaccess file should be in /home/username
Add the following line to the .htaccess file
SetEnv PHPRC /home/username/php.ini
Wait its not over the php.ini file needs to be secured so along with the above line add the following
<Files ~ "\.(ini)$"> order allow,deny deny from all </Files>
This will return a 403 error whenever someone tries to view your php.ini files or any file with a .ini extension. You could argue that no one can access it via the url as it is located outside the document root, but even if you accidentally copy the file into the document root you’ll be in trouble. So better safe than sorry.
Step 3:- Test your php.ini file
Create a file named info.php into your document root and add the following code
<?php phpinfo(); ?>
Save the file and access it by typing in the url (http://example.com/info.php) check the value next to “Loaded Configuration File” it should point to the location of the new php.ini file.
Now you can edit the php.ini file as you wish. Here are some sample configurations
display_errors = On date.timezone = Europe/London upload_max_filesize = 30M
IMPORTANT Remove the file containing phpinfo immediately because it exposes many sensitive information about your web server