• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved Set default php-version for command line

Fabs87

New Pleskian
Hi,

I need to set the default php-version of a subscription to php7.1. Of course I've done this via the plesk onyx web interface.

But now I need to run a few php commands via ssh access. Unfortunately the php-version there is 5.5.9 (connected via ssh with my webhosting-user. SSH-access-setting is set to /bin/sh.
Logged in via ssh with my root/admin account the correct version of php is available.

Is there a way to set the version permanently to php7.1? I don't want to write /opt/plesk/php/7.1/bin/php every time I need to run php commands..

Thanks!
 
Actually, even if you wanted to include the /opt/plesk/php/7.1/bin/php path, this won't work, because in the change-rooted SSH environment of a subscription, this path should not be accessible. As far as I recall this, the only options to execute a different PHP version on the command line is to update the OS vendor's PHP or to include another PHP version in the change-rooted environment. For the later I must admit, I personally never did it, so I am of no help here. Maybe @UFHH01 knows how to do that.
 
ok...
For one session I can run the following command:
Code:
export PATH=/opt/plesk/php/7.1/bin:$PATH;
Now my php-version is set to 7.1 for all following commands. After logging out and in again via ssh, the version is again back to 5.5.9.
 
The following will keep your export settings every time you login :)
Code:
touch ~/.bashrc
echo "export PATH=/opt/plesk/php/7.1/bin:$PATH;" > ~/.bashrc
 
The following will keep your export settings every time you login :)
Code:
touch ~/.bashrc
echo "export PATH=/opt/plesk/php/7.1/bin:$PATH;" > ~/.bashrc

Anyone, any idea why this doesn't work for me? After closing terminal php version resets back to 5.4 :/
 
Try adding it into your ~/.bash_profile instead of ~/.bashrc. If something like a cronjob needs to use the newer version of php you could (as an example)

Code:
*/5  *  *  *  * /opt/plesk/php/7.2/bin/php -f /var/www/vhosts/example.com/cron.php
 
This will only work, if the subscription user has full SSH access. If she has only chrooted access, /opt/plesk will be inaccessible.
 
This is what I usually do,

Create a file in the File Manager > "Home directory" called ".profile" there is a "." in the beginning.

With this content,

Code:
alias php='/opt/plesk/php/7.2/bin/php'
cd httpdocs

Then you can give /bin/bash or /bin/sh SSH access to the Subscription via Plesk Panel.

If you are already logged in as above SSH user you have to logout and log back in this to work.

To include composer as well,
Code:
alias php='/opt/plesk/php/7.2/bin/php'
alias composer='/opt/plesk/php/7.2/bin/php /usr/lib/plesk-9.0/composer.phar'
cd httpdocs
 
Last edited:
Check php-version with:
Code:
php -v
List the php handlers
Code:
/usr/local/psa/admin/bin/php_handlers_control --list
Find the default php binary
Code:
which php
Backup the php, php-cgi and php-fpm binaries.
Code:
mv /usr/bin/php /usr/bin/php.backup
mv /usr/bin/php-cgi /usr/bin/php-cgi.backup
mv /sbin/php-fpm /sbin/php-fpm.backup
Create symlink from php 5.6 binaries
Code:
ln -s /opt/plesk/php/5.6/bin/php /usr/bin/php
ln -s /opt/plesk/php/5.6/bin/php-cgi /usr/bin/php-cgi
ln -s /opt/plesk/php/5.6/sbin/php-fpm /sbin/php-fpm

You can set symlink from any other php version installed in the server. Following are the php bnaries for other versions, find the same for php-cgi and php-fpm too.
Code:
/opt/plesk/php/5.3/bin/php
/opt/plesk/php/5.4/bin/php
/opt/plesk/php/5.5/bin/php
/opt/plesk/php/5.6/bin/php
/opt/plesk/php/7.0/bin/php
/opt/plesk/php/7.1/bin/php
/opt/plesk/php/7.2/bin/php

Check the changed version with:
Code:
php -v

Next option is to update the plesk database with the changed versions. The reread option will update plesk database with the new versions.
Code:
/usr/local/psa/admin/bin/php_handlers_control --reread

After that reconfigure all the domains or the domains using the default php versions. And proceed with a service restart.

Code:
/usr/local/psa/admin/sbin/httpdmng --reconfigure-all
service httpd restart
 
Keep in mind that on the shell, 'php' is usually the distro version.
Check with 'which php', it'll probably be /usr/bin/php, and check with 'ls -l `which php`' to see whether it is a symlink and where it points to.
When you are on debian or related distro, this is a symlink to /etc/alternatives/php, which is a symlink to the real location.
You can check with 'update-alternatives --display php'. Manually select one with 'update-alternatives --config php'.
 
I had a similar issue.
And basically I couldn't execute any command. For instance:
$ composer
-bash: composer: command not found
neither use ls, cat, vi, vim, etc etc.
So seems bash_profile was empty and NO PATH was set.
So I executed:
$ PATH=/usr/local/bin:/usr/bin:bin
in the terminal so that other programs can look for commands.
Then I was able to execute ls, cat, etc

But then other issue
$ whichphp sayd not found
then I went to bash_profile and add:
export PATH=/opt/plesk/php/7.4/bin:$PATH
then
$ source ~/.bash_profile

and finally I was able to execute
$ php -v

And also execute
$ composer

FYI ^^ Just in case somebody faces the same issue.

Reference: centos 7: all command not found, except cd
 
Back
Top