Solution – Use one single php.ini file for All Directories

When we go with shared servers, same php configuration is used for all the accounts setup on the server and support staff usually rejects users plea to make some changes with the configuration. So, users don’t have enough choice other than to use a custom php.iniĀ  file to make the required changes.

The problem with php.ini file is that it will work only for the directory it is placed in and if you want to make the same php configuration changes in other directories too, you need to copy it to all those directories too. However, there is a solution to ask the server to use the same php.ini file for all the directories. Here’s how to do that:

  1. Create a custom php.ini file with the changes you want to make in the configuration and place it in the public_html directory (You can choose any other directory too). For example, if you want to increase the memory limit of php, you need to create a php.ini file as:
    max_execution_time = 60     ; Maximum execution time of each script, in seconds
    max_input_time = 60	; Maximum amount of time each script may spend parsing request data
    memory_limit = 64M      ; Maximum amount of memory a script may consume (32MB)

    This will increase the memory limit to 64MB and will also set higher limits for execution time and input time.
    Or, you can get a copy of the server’s default php.ini file and make changes to that file itself. It will be a lot better idea and will make sure that other server set changes are not lost.

  2. Now, you need to ask the server to use this new php.ini file for all purposes. We will use suphp module to accomplish our task. For that, you need to edit the .htaccess file present in the public_html directory and copy the following code in it. If there’s no default .htaccess file present, just create a blank file with the name .htaccess and copy the following code in it.
    <IfModule mod_suphp.c>
      suPHP_ConfigPath /home/YOURUSERNAME/PHP.INI_DIRECTORY
      <Files php.ini>
        order allow,deny
        deny from all
      </Files>
    </IfModule>

    Note: You need to set the directory which contains your php.ini file in the above code before it will start working. If you have your php.ini file in the public_html folder, you need to place /home/YourUsername/public_html in the above code.

  3. Provided, if you did everything correctly and your host has not disabled the use of custom php.ini files, it will work perfectly.

It was tested on – php5, mysql5, cpanel11. If you experience any problem, the comment section below is for you. Let me know and I’ll try my best to help you out.

, ,

3 Responses to Solution – Use one single php.ini file for All Directories

  1. Phil Brighton August 3, 2016 at 3:35 pm #

    Hi

    If I use this in the ‘public_html’ folder and put a different php.ini in a sub folder (eg ‘public_html/subfolder’), which version of php.ini takes precedence ?

    Thanks

    • Narender Singh August 3, 2016 at 6:39 pm #

      The one from public_html will be used wherever a php.ini is not found in the directory. So in your case, PHP would rely on the php.ini in public_html/subfolder as long as the code to execute is in the /subfolder directory. Otherwise it will fall back to the one defined with suPHP.
      This is how a standard PHP installation should perform. If it doesn’t, consider contacting your host.

      • Phil Brighton August 3, 2016 at 9:56 pm #

        That’s really helpful. Thank you so much !

Leave a Reply