bash script to switch PHP versions in Plesk command line

I found myself needing to switch between PHP versions on the command line running under the Plesk environment on a CentOS server. Some apps ran under PHP 5.6, some under 7.0 and others being pushed towards 7.1. This script allows me to be quickly switch between them. It looks for the current PHP version in the PATH and changes just that.

My shell scripting is a little rusty, so you may have some improvements. It shows the PATH before and after, so you should be able to spot if it breaks things, and quickly fix it.

4 Responses to bash script to switch PHP versions in Plesk command line

  1. KooTjoo 2017-10-27 at 14:52 #

    Hi Jason,

    This would be a real time saver but can’t get it to work on my Plesk Onyx setup. If I run the script by using for instance

    setphp 7.0 I get this as output and php version for shel hasn’t changed.:

    /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
    /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

    Anny suugestions?

    Thanks
    KooTjoo

    • Jason Judge 2017-10-27 at 15:08 #

      It looks like the setphp script is not in your path. If it is in the local directory, try running it like this:

      . ./setphp 7.0

      Do you have PHP versions in /opt/plesk/php/ ?

    • Jason Judge 2018-01-27 at 14:12 #

      In case this is still an issue, don’t forget to run the script with a “. ” in front of it (a dot and a space). This will run the script in the *current* shell, otherwise the script will be run in a child process and the PATH changes will never make their way back.

  2. Jason Judge 2018-03-01 at 11:21 #

    I’ve just realised what your problem was. A new user in Plesk will not have ANY non-OS default PHP directory in its path. This script was designed to change the non-default PHP part of the path, and of course, that part did not exist, and so no changes were made.

    I have changed it now, so that if the PHP path is not found at all in the path, then it will add it to the start (it must add it before /usr/bin/). This should fix your problem.

Leave a Reply