Using Custom php.ini File for PHP (cli) via SSH

While you are working with PHP in command mode, the php command automatically runs the PHP CLI binary file corresponding to the default PHP version.

To view a list with all PHP CLI options, use the following command:

php -h

To check the current PHP CLI version, you should execute:

php -
Check the version of PHP CLI
Check the version of PHP CLI

To see the current php.ini file (which will load every time by default), use:

php --ini
Check the loaded php.ini file (Loaded Configuration File)
Check the loaded php.ini file (Loaded Configuration File)

In case you have not specified another file, PHP CLI will always use the default system php.ini file /opt/cpanel/ea-phpXX/root/etc/php.ini.

If it is necessary to set specific settings for executing a PHP script, you might need to add a -c option to the command followed by the custom php.ini file. For example:

php -c /home/cpuser/my-custom-php.ini /home/cpuser/public_html/file.php

/home/cpuser/my-custom-php.ini – replace the full path and the .ini file name;
/home/cpuser/public_html/file.php – the file that will be executed;

If the custom php.ini file will be used with each PHP command in the console, add a line in the .bash_profile file so that you do not need to type the file name every next time:

alias php='php -c /home/cpuser/my-custom-php.ini'

/home/cpuser/my-custom-php.ini – replace with the full path and the .ini file name;

If you wish to work with a different PHP (cli) version, edit the line by changing the binary file path:

alias php=’/opt/cpanel/ea-phpXX/root/usr/bin/php -c /home/cpuser/my-custom-php.ini’

ea-phpXX – replace with the PHP version, for example /opt/cpanel/ea-php72/root/usr/bin/php;

ini file for PHP directives is loaded
ini file for PHP directives is loaded

Note: Also, if there are any irregularities in the .ini file such as unsupported directives from older PHP versions, you will get an error message in the console. For example: “Fatal error: Directive ‘allow_call_time_pass_reference’ is no longer available in PHP in Unknown on line 0”. Make sure that the .ini file’s content is intended for the current PHP version.

To load the ini file with PHP modules, add in .bash_profile file the line:

export PHP_INI_SCAN_DIR=/home/cpuser/.sh.phpmanager/phpXX.d

/home/cpuser/.sh.phpmanager/phpXX.d – replace with the path to the ini file directory;

The two ini files are loaded - my-custom-php.ini for PHP directives and php72.ini for PHP modules
The two ini files are loaded – my-custom-php.ini for PHP directives and php72.ini for PHP modules

If you are logged into the hosting account via SSH, log out and then log in again to activate this setting. You might also use this command:

source ~/.bash_profile

The .bash_profile file settings will load every time you access your hosting account via SSH.

Check which .ini file is loaded with:

php --ini

This time the command result should display the custom .ini file in the Loaded Configuration File field.

In the command line you can set different values for certain PHP directives through the -d (configuration_directive=value) option. For example:

php -c /home/cpuser/php.ini /home/cpuser/file.php -d memory_limit=256M

According to the set parameters for script execution, the value for memory_limit will be 256MB.

This article is about: hosting services with cPanel.

Updated on 15.11.2021

Was this article helpful?

Related Articles