Plesk

How to configure a specific command line PHP version for an SSH user on a Plesk for Linux server?

Question

How to configure a specific command line PHP version for an SSH user on a Plesk for Linux server?

Answer

Note: To apply the solution described in this article it's required to connect to the server with the root user. If root access is not available via SSH, contact your hosting service provider.

By default, the php command calls the binary installed by the PHP version provided by the operating system repository. The binary path of the php command can be retrieved with the following command:

# which php
/usr/bin/php

Alternatively, it's possible to install other PHP versions from Plesk, and to execute those on the command line it's required to call the full binary path. For example:

# /opt/plesk/php/7.4/bin/php -v
PHP 7.4.11 (cli) (built: Oct 2 2020 14:09:37) ( NTS )

All binaries from the additional Plesk PHP versions installed on the server can be retrieved with the following command:

# find /opt/plesk/php/*/bin/php

 

To modify the PHP binary called by the php command for a Plesk system user, apply one of the following options according to the SSH option configured on the subscription:

Click on a section to expand

Subscriptions configured with a chrooted environment

  1. Log in to Plesk GUI as Plesk Administrator

  2. Go to Domains > example.com > Web Hosting Access and set option Access to the server over SSH to /bin/bash (chrooted) > OK to apply the changes

  3. Connect to the server via SSH with the root user

  4. Add the necessary PHP version into the chroot template

  5. Create a symlink with the relative path:

    # ln -s /opt/plesk/php/7.4/bin/php /var/www/vhosts/chroot/bin/php

  6. Apply chroot template to all the subscriptions:

    # ./update_chroot.sh --apply all

  7. Switch to the Plesk system user of the subscription:

    # su example_user

  8. Verify the PHP version running the following command:

    # bash-4.2$ php -v
    PHP 7.4.11 (cli) (built: Oct 2 2020 14:09:37) ( NTS )

    Note: If CageFS is enabled for user on CloudLinux OS, it is required to add PHP to CageFS instead

Subscriptions configured with a non-chrooted environment

  1. Log in to Plesk GUI as Plesk Administrator

  2. Go to Domains > example.com > Web Hosting Access and set option Access to the server over SSH to /bin/bash > OK to apply the changes

  3. Connect to the server via SSH with the root user

  4. Switch to the Plesk system user of the subscription:

    # su example_user

  5. Add the PHP binary path on the .bashrc file:

    # echo "alias php='/opt/plesk/php/7.4/bin/php'" >> ~/.bashrc

    * Where /opt/plesk/php/7.4/bin/php is the necessary PHP binary path

  6. Add the following content to the .bash_profile file:

    # echo "if [ -f ~/.bashrc ]; then" >> ~/.bash_profile
    # echo ". ~/.bashrc" >> ~/.bash_profile
    # echo "fi" >> ~/.bash_profile

  7. Log out to reload the changes:

    # exit

  8. Verify the PHP version running the following command:

    # su example_user
    # php -v
    PHP 7.4.11 (cli) (built: Oct 2 2020 14:09:37) ( NTS )