• bg
  • en

Hosting, Domains, Virtual Private Servers (VPS), Managed VPS, SSL Certificates

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

php, php cli, php shell, php ssh, php command line, php console, php-cli, php sapi, php sapi cli

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 -v
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)

In case you have not specified another file, PHP CLI will always use the default system php.ini file /usr/local/lib/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='/usr/local/php7.0/bin/php -c /home/cpuser/my-custom-php.ini'
/usr/local/php7.0/bin/php - replace php7.0 with the desired version;

info-iconNote: 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.

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

All rights reserved © 2005-2019 ,