1. Home
  2. Technical
  3. How to change default php-cli in console

How to change default php-cli in console

Default PHP CLI version for Zone managed servers (shared hosting, smart dedicated servers) have the same PHP version as the main web host does.

You can check the current version used like this:

php -v

That will show similar output:

virt1234:SN-242-129.HEL01.ZONEAS.EU:~> php -v
PHP 8.3.7 (cli) (built: May 23 2024 08:53:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.7, Copyright (c) Zend Technologies
with Zend OPcache v8.3.7, Copyright (c), by Zend Technologies

Using Other Versions

Changing the PHP version of the main domain’s web host also changes the default version for the console. The PHP version can be changed in the Webserver section of the Webhosting administration in the My Zone administration.

To change the PHP version only in the console, you can create a symlink for the php command in the ~/bin directory pointing to the desired PHP CLI version file or use PHP CLI commands with the version number in the command name.

Guide To Establishing an SSH Connection

Establishing an SSH connection

Persistent php Symlink

If you want to use PHP version 8.2, you need to run command:

> mkdir -p ~/bin && ln -sf /usr/bin/php82-cli ~/bin/php

If you want to use PHP 7.4 or some other version, you need to use the corresponding version number without dot in the command for the php-cli file:

> mkdir -p ~/bin && ln -sf /usr/bin/php74-cli ~/bin/php

After running this command, re-login into SSH and after that php -v shows changed version.

NB! After creating this symlink, PHP CLI version will not change automatically in the future when the PHP version changes for main web host.
To remove the existing symlink run this command:

> rm -f ~/bin/php

PHP CLI Commands With Different Versions

There are also separate commands for each PHP CLI version that can be used as an alternative to the php command. For example:

> php83-cli -v
PHP 8.3.7 (cli) (built: May 23 2024 08:53:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.7, Copyright (c) Zend Technologies with
Zend OPcache v8.3.7, Copyright (c), by Zend Technologies

If PHP CLI 7.4 or any other version is needed to be used, you must use the corresponding version number without a period in the command.

Which version of PHP CLI binaries are usable can be checked with the command:

> l /usr/bin/php*-cli

Loading The Necessary php.ini

Since by default the same version is used for PHP CLI as for the main domain’s PHP, the main domain’s PHP configuration file php.ini is also loaded for PHP CLI.

If a different version is used for PHP CLI, the main domain’s PHP configuration file php.ini will still be used.

In order for PHP CLI to use the necessary version of the php.ini file or some specific php.ini file, the environment variable PHPRC is needed to be used, with its value set to the path of the required php.ini file.
This could be the PHP CLI php.ini of the hosting server, a php.ini of a (sub)domain, a global php.ini or a completely separate php.ini file:

> PHPRC=/etc/php/cli-php8.2/php.ini
> PHPRC=/data01/virt1234/domeenid/www.example.com/phpini/https/sub.example.com/php.ini
> PHPRC=/data01/virt1234/domeenid/www.example.com/phpini/global/php.ini
> PHPRC=/data01/virt1234/domeenid/www.example.com/custom.ini

Or use the PHPRC environment variable in the same command, combining the two commands together:

> PHPRC=/etc/php/cli-php8.2/php.ini && wp cli info

To use another version, you need to change the version number in the php.ini file path.

When additional PHP settings or directives need to be added or overwritten for the main php.ini file, you can use the environment variable PHP_INI_SCAN_DIR to load a separate php.ini. Set its value to the path to the directory containing the necessary php.ini file:

> PHP_INI_SCAN_DIR=/path/to/dir

On the contrary, if there exists a global php.ini file that should not be loaded, you need to set PHP_INI_SCAN_DIR to an empty value:

> PHP_INI_SCAN_DIR=

Persistent Configuration

When executing the previous commands that determine the loaded php.ini files, the environment variables PHPRC and PHP_INI_SCAN_DIR remain valid only for the duration of the SSH session.
If it is needed that PHP CLI uses constantly necessary php.ini file, these environment variables must be added to the ~/.bashrc file.
The ~/.bashrc file is automatically loaded at the beginning of each SSH session, and it also loads the environment variables contained within it.

In the previous command examples, adding PHPRC and PHP_INI_SCAN_DIR to ~/.bashrc and reloading it:

> echo 'PHPRC=/etc/php/cli-php8.2/php.ini' >> ~/.bashrc && source ~/.bashrc
> echo 'PHP_INI_SCAN_DIR=/path/to/dir' >> ~/.bashrc && source ~/.bashrc

The lines of the environment variables PHPRC and PHP_INI_SCAN_DIR can be deleted from the ~/.bashrc file with the following commands:

> sed -i '/^PHPRC=/d' ~/.bashrc
> sed -i '/^PHP_INI_SCAN_DIR=/d' ~/.bashrc

Most Common Errors

If an error message related to PHP CLI or php.ini is visible in the console, you should check whether this error is caused by a difference between the PHP CLI and the loaded php.ini versions.
Additionally, the error may be caused by the loaded global php.ini if it is intended for an older PHP version or if the settings in it cause conflicts.

If there is a significant difference between the PHP CLI and the loaded php.ini versions, it may prevent the use of PHP CLI in the console because the php.ini file may specify loading an extension that does not exist for the PHP CLI version.
For example, a common error message in the console in such cases is:
Warning: PHP Startup: Unable to load dynamic library 'php_xmlrpc.so'.

Whether a symlink is used for the php command or not can be checked with the following command. The use of a symlink is indicated by the inode type l (link) and the symlink form /data01/virt1234/bin/php.

> l $(whereis php)

What is the version and which php.ini files are loaded for PHP CLI, can be checked using the following command:

> php -v | head -1 && php --ini | grep -iE 'Configuration File|Additional'

To use the required version of the php.ini configuration file for PHP CLI in the console, you need to use the environment variable PHPRC (PHP doc.: PHP Runtime Configuration).

Updated on 18. Oct 2024
Was this article helpful?

Related Articles