You are here:Home»KB»Web Server»General»Overriding the php.ini
Sunday, 22 February 2015 19:05

Overriding the php.ini

Written by

There are a couple of ways to overide the PHP settings as defined in /usr/local/lib/php.ini (the system php.ini) and i will list them below.

  • if the specified .ini file does not exist default /usr/local/lib/php.ini is loaded
  • you do not need to use php.ini as the .ini filename, the system php.ini file will still get overidden no matter what name you give it as long as it is correctely specified. The only exception is that when using the 'php.ini in a folder' method, it must be called php.ini
  • in all of these method below you make the changes in the target php.ini file and are basically telling the system to use that instead.
  • when i mention php.ini, this can be replaced with 'Loaded Configuration File'

NB: DO NOT PUT php.ini IN THE PUBLIC_HTML, they can be read and downloaded., only put them in there or any subfolder if they are protected.

php.ini in a folder

You can just place a copy of your modified php.ini file, altered to your needs, in to any folder and all scripts run from that folder will run using the php.ini in the folder, instead of the system php.ini.

  • it takes all settings from the root htaccees file /usr/local/lib/php.ini and then overides them with what is in the new php.ini
  • this will be shown as the loaded php.ini file
  • Some settings can not be overidden with this method.
  • this is not recursive
  • php.ini will not work in directories where SetEnv PHPRC has been used because the 'Loaded Configuration File' has already been set via the htaccess file

suPHP

This is set in the .htaccess file and points to a php.ini of your choice. It is preferable to place this php.ini outside the public_html folders.

  • maintains current php.ini file but appears as the loaded php.ini file used.
  • It only overides what is written in it
  • i dont know if it is recusive

Examples

1 - This is the most basic of the command and will work for most people

suPHP_ConfigPath /home/example/php-folder/php.ini

2 - This example shows how you can put the php.ini in a public_html folder and prevent access to it. The suphp command will also not run unless the module is installed and this can prevent errors upon installation.

<IfModule mod_suphp.c>
  suPHP_ConfigPath /home/example/public_html
  <Files php.ini>
    order allow,deny
    deny from all
  </Files>
</IfModule>

SetENV PHPRC

This is set in the .htaccess file and points to a php.ini of your choice. It is preferable to place this php.ini outside the public_html folders.

  • becomes the loaded php.ini file
  • this method replaces the loaded php.ini, it does not use an overide mechanism
  • it is recursive
  • SetEnv PHPRC only works when using PHP as CGI, not when using php as an Apache Module
  • this method can be used to effectively remove 'disable_functions'

Examples

SetEnv PHPRC /home/example/php-folder/php.ini

Links

This assumes that you webhost company has already sideloaded the appropriate version of PHP you want to use. This is the .htaccess code required to tell the server to use the alternative version.

Sideload alternative version of PHP

Sideloading PHP allows you to have mulitple PHP versions running on the same hosting account/server. You have the default PHP version and then the sideloaded ones which you can access by using an alternative php.ini defined by a .htaccess file. Using suPHP will override the default php.ini recursively.

This code below is put in your .htaccess file and configures the host to use a sideloaded version of PHP, in this case PHP 5.5.

# Custom Legacy PHP 5.5 handler placed by host
    AddType application/x-httpd-php55 .php5 .php4 .php .php3 .php2 .phtml
    suPHP_ConfigPath /usr/local/lib/php55.ini
# End Custom Legacy PHP 5.5 handler placed by host

The target version of PHP needs to be installed by your host provider for this to work.

Read 1189 times Last modified on Tuesday, 12 December 2017 12:49