Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
How to use custom php.ini files on shared hosting

How to use custom php.ini files on shared hosting

September 22, 2010 PHP Jesin A 9 Comments

php category thumbnail

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.

custom php.ini file
The phpinfo will display your custom php.ini path

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

Related posts:

php category thumbnailPHP error_log .htaccess Default ThumbnailHow to create custom error page in Apache Default ThumbnailRunning PHP code in HTML files linux category thumbnailHow to make undeletable files and directories in Linux Default ThumbnailPassword Protect directories using .htpasswd

Tags: htaccess, php

Comments

  1. johnjohn808 says

    June 16, 2013 at 10:59 am

    Thank you very much!
    I learned somthing new, and also fixed my problem!

    Reply
  2. Majk says

    September 13, 2014 at 12:05 am

    Will this work on a shared hosting environment with PHP running as CGI?

    Reply
    • Jesin A says

      September 14, 2014 at 12:06 am

      It is best to ask the hosting provider themselves. Some services directly provide a php.ini file which can be edited over FTP/File Manager.

      Reply
  3. jerry says

    March 4, 2016 at 3:47 pm

    Thanks Jesin ,

    I’ve my domain on Godaddy shared host, above fix worked well for me.
    Thanks a lot.

    Jerry

    Reply
  4. IdeanShape says

    May 1, 2016 at 11:27 am

    Hi, I followed this method, But my php info still shows /usr/local/lib/php.ini not my username 🙁

    i created both php.ini & .htaccess inside root “home/username/”

    Reply
    • Jesin A says

      May 2, 2016 at 3:40 am

      Hi,

      Who is your hosting provider? Nowadays PHP settings can be modified from the “PHP selector” option in the cPanel.

      Reply
  5. Ronald says

    May 2, 2017 at 11:14 pm

    Can i just create my htaccess with that single line and with my own lines of php.ini?

    Reply
  6. Jafarov Misir says

    January 8, 2018 at 2:35 am

    Thanks

    Reply

Trackbacks

  1. How to use PHP to minify HTML output | Jesin's Blog says:
    June 30, 2013 at 3:18 pm

    […] access to the php.ini file. This method will work fine even if you are using shared hosting with custom php.ini […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.