Zum Inhalt springen

Use specific PHP versions in the console

The PHP version for the console is independent of the PHP version in the domain administration.

In many cases, however, it is desirable that the PHP versions for the website and on the console match.

In the console, the desired PHP version can either be called up directly or set as the default.

Specific PHP versions for individual scripts

If you only need a different PHP version for certain scripts, you can also only call these scripts with a specific PHP version. To do this, you first need the absolute path to the executable PHP file. These are assembled as follows: /opt/alt/php<majorVersion><minorVersion>/usr/bin/php.

Examples:

  • PHP 8.3: /opt/alt/php83/usr/bin/php
  • PHP 8.2: /opt/alt/php82/usr/bin/php
  • PHP 8.1: /opt/alt/php81/usr/bin/php
  • PHP 8.0: /opt/alt/php80/usr/bin/php
  • PHP 7.4: /opt/alt/php74/usr/bin/php
  • PHP 7.3: /opt/alt/php73/usr/bin/php
  • PHP 7.2: /opt/alt/php72/usr/bin/php
  • PHP 7.1: /opt/alt/php71/usr/bin/php
  • PHP 7.0: /opt/alt/php70/usr/bin/php
  • PHP 5.6: /opt/alt/php56/usr/bin/php
  • PHP 5.5: /opt/alt/php55/usr/bin/php
  • PHP 5.4: /opt/alt/php54/usr/bin/php
  • PHP 5.3: /opt/alt/php53/usr/bin/php
  • PHP 5.2: /opt/alt/php52/usr/bin/php

All PHP versions can be found at: Available PHP CLI versions

Insert this file path before the script or enter this file path as PHP binary if the program has such a setting.

For example, if you execute the following command

php app/console doctrine:generate:entity

but if you need PHP 8.2 for this, you can use

/opt/alt/php82/usr/bin/php app/console doctrine:generate:entity

in the future.

Select standard PHP version for the entire package

SSH with the web server and then carry out the following steps:

1. create a .local/bin directory in the hosting package root directory:

mkdir -p $HOME/.local/bin

2. navigate to the directory just created:

cd $HOME/.local/bin

3. set the symlink to the desired PHP version. See the example above to assemble the path.

# for PHP 8.3
ln -s /opt/alt/php83/usr/bin/php $HOME/.local/bin/php

# for PHP 8.2
ln -s /opt/alt/php82/usr/bin/php $HOME/.local/bin/php

# for PHP 8.1
ln -s /opt/alt/php81/usr/bin/php $HOME/.local/bin/php

# for PHP 8.0
ln -s /opt/alt/php80/usr/bin/php $HOME/.local/bin/php

# for PHP 7.4
ln -s /opt/alt/php74/usr/bin/php $HOME/.local/bin/php

# for PHP 7.3
ln -s /opt/alt/php73/usr/bin/php $HOME/.local/bin/php

# for PHP 7.2
ln -s /opt/alt/php72/usr/bin/php $HOME/.local/bin/php

# for PHP 7.1
ln -s /opt/alt/php71/usr/bin/php $HOME/.local/bin/php

# for PHP 7.0
ln -s /opt/alt/php70/usr/bin/php $HOME/.local/bin/php

# for PHP 5.6
ln -s /opt/alt/php56/usr/bin/php $HOME/.local/bin/php

# for PHP 5.5
ln -s /opt/alt/php55/usr/bin/php $HOME/.local/bin/php

# for PHP 5.4
ln -s /opt/alt/php54/usr/bin/php $HOME/.local/bin/php

# for PHP 5.3
ln -s /opt/alt/php53/usr/bin/php $HOME/.local/bin/php

# for PHP 5.2
ln -s /opt/alt/php52/usr/bin/php $HOME/.local/bin/php

To ensure that the "php" command uses the desired version in the entire hosting package, it is easiest to add the current folder to the PATH variable. This variable contains the locations where executable files are located. This allows scripts with the PHP version to be called later as desired.

4. extend PATH variable

Create a .bash_profile file in the root directory of the package, for example with the nano editor:

nano $HOME/.bash_profile

Insert the following line into this file. These extend the PATH variable by the previously created directory.

export PATH=$HOME/.local/bin:$PATH

Then save the file and close the editor.

For these changes to take effect, the .bash_profile file must be reloaded with the following command:

source $HOME/.bash_profile

The desired PHP version can then be called up in the entire hosting package with the command "php".

This page contains automatically translated content.

Updated: 17.07.2024