Applicable to:
- Plesk for Linux
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
-
Log in to Plesk GUI as Plesk Administrator
-
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
-
Connect to the server via SSH with the root user
-
Create a symlink with the relative path:
# ln -s /opt/plesk/php/7.4/bin/php /var/www/vhosts/chroot/bin/php
-
Apply chroot template to all the subscriptions:
# ./update_chroot.sh --apply all
-
Switch to the Plesk system user of the subscription:
# su example_user
-
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
-
Log in to Plesk GUI as Plesk Administrator
-
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
-
Connect to the server via SSH with the root user
-
Switch to the Plesk system user of the subscription:
# su example_user
-
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 -
Add the following content to the
.bash_profile
file:# echo "if [ -f ~/.bashrc ]; then" >> ~/.bash_profile
# echo ". ~/.bashrc" >> ~/.bash_profile
# echo "fi" >> ~/.bash_profile -
Log out to reload the changes:
# exit
-
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 )
Comments
22 comments
I tried to add .bashrc file with php alias line, but it didn't do the trick for me.
I had to add PATH to .bash_profile file:
echo "PATH=/opt/plesk/php/7.1/bin:$PATH" >> ~/.bash_profile
Log out and log in. After that php -v show version 7.1.*
Hello @Jari, thank you for sharing your experience, it may be useful for other Pleskians.
I have checked the instructions on a test server with CentOS 7 and Debian 9, the provided steps were sufficient.
Thanks Jari your solution helped!
Didn't work for me, no matter what I tried (Centos 7). Finally came across another post on stackexchange which did the trick.
Yes I installed my own version of wp-cli because the one built in with plesk has issues with DB search/replace.
Hi all,
I had a different version from the domaines>php settings and the ssh one. Thanks to your solution I have the same one now : 5.6 according to php -v.
But when I run composer require league/oauth2-google to install oauth2, I get the following message:
league/oauth2-client 2.4.1 requires php ^5.6|^7.0 -> your PHP version (5.5.9) does not satisfy that requirement.
Do you have any clue? It seems that the version used by composer is another one.
TIA.
Erwan
Hello @Erwan,
Please, check the instructions in the following article https://support.plesk.com/hc/en-us/articles/115001707605
As an example of using composer, you may check our article on how to install Laravel https://support.plesk.com/hc/en-us/articles/360000399513 :
# cd httpdocs/ && /opt/plesk/php/X.Y/bin/php /usr/lib64/plesk-9.0/composer.phar create-project laravel/laravel --prefer-dist
solution for PHP 7.2
echo "PATH=/opt/plesk/php/7.2/bin:$PATH" >> ~/.bash_profile
Hello @Jan Bludau,
Thank you for the input. Such a way also takes place.
Just for other Pleskians: do not forget to log out/login after "~/.bash_profile" file is edited.
Hello, thank you for the article. It helped very much but did not solve my problem. I have changed default php version for a specific user for php command over SSH. But it does not change the version for scheduled tasks. On SSH, when i use php -v, i can see 7.2 but not in scheduled tasks. I am creating a cronjob but i am having error so i tried this command at scheduled tasks
php -v >> httpdocs/logs/phplogs.log
and the log file shows
PHP 5.4.16 (cli) (built: Oct 30 2018 19:30:51)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.0.3, Copyright (c) 2002-2017, by ionCube Ltd.
How can i fix this? I want php 7.2 to become the default handler for scheduled tasks too.
Hello @Hüseyin Şevki TOPUZ,
Shell spawned by Cron is not equal to the shell spawned by sshd
Thus, it returns system PHP instead of Plesk PHP 7.2 you set up
In scheduled tasks, you have to use the absolute paths, e.g. "/opt/plesk/php/7.2/bin/php -v"
Check this article for details
What would i need to do to define a alias for all ssh domain users or even better provide the .bashrc from root user?
Hello Robert,
To define specific PHP version for all users, you may create file "/etc/profile.d/change_php.sh"
With the following content:
#!/bin/sh
PATH="/opt/plesk/php/7.1/bin:$PATH"
Hello, thank you for the article, it helped me but I still can't solve a problem.
When I am connected to the server via SSH and I run the following command, I have the expected result.
But when I run the following command from a local console (SSH Public Key Authentication), I have the PHP version which is installed with operating system :
Is it possible to use the version of PHP that I defined ?
Thank you for your help.
When you run command remotely without being logged to server itself, .bashrc is not executed. Use full path instead:
It was definitely necessary to create user's own .bashrc file because without creating it, I was still getting the old php version. So I recommend following all the steps above. Just to help the users like me!
Hello lostadmin
Thank you for the message.
The command created the file in question in case it doesn't exist:
echo "alias php='/opt/plesk/php/7.3/bin/php'" >> ~/.bashrc
Add a link to update_chroot.sh ?
Hello
Very sorry, but none of the above is correct. The true test comes when you work with Magento 2.4.1 which requires CLI install using Composer on php7.4 and mySQL5.7 or mariaDB 10.3. Default php binary if not 7.4 gives all sorts of errors and all "google experts" blame Magento 2.4.1 or Plesk or Centos !
On Centos 7 Plesk Onyx (OS php5.4) and Centos 8 Plesk Obsidian (OS php7.2) I promise you, this is the ONLY solution that works to set the default php binary in the most demanding of situations: Trust me NOTHING is tougher than installing Magento 2.4.1 on server running Plesk. It needs default binary to be php7.4 with numerous extensions, regardless of the OS php. So do as folllows:
$ which php
/usr/bin/php # the output
$ /usr/local/psa/admin/bin/php_handlers_control --list # this shows up all the php versions
$ 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
$ ln -s /opt/plesk/php/7.4/bin/php /usr/bin/php
$ ln -s /opt/plesk/php/7.4/bin/php-cgi /usr/bin/php-cgi
$ ln -s /opt/plesk/php/7.4/sbin/php-fpm /sbin/php-fpm
$ /usr/local/psa/admin/bin/php_handlers_control --reread
$ /usr/local/psa/admin/sbin/httpdmng --reconfigure-all #to reset all the domains to php7.4 if required (can be done through Plesk UI)
$ php -v #will confirm php7.4 but this is not enough as I discovered with other methods to set default php binary)
This method will ensure that any command you execute as root or any user will route through php7.4 (or your chosen php)
As I found out the hard way:
You need to add to content of /usr/lib/x86_64-linux-gnu/gconv/* to your chroot-environment, otherwise iconv() will fail with something like "Notice: iconv(): Wrong charset, conversion from `utf-32le' to `utf-8' is not allowed"
BR
I am running into a a weird issue when setting a php version for chrooted environments, i can set it to 7.4 but not to 8.0 or 8.1.
8.x results in bash: php: command not found where 7.4 is working just fine
bash-4.2$ php -v
bash: php: command not found
bash-4.2$ php -v
PHP 7.4.26 (cli) (built: Nov 18 2021 20:01:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with the ionCube PHP Loader + ionCube24 v10.4.5, Copyright (c) 2002-2020, by ionCube Ltd.
with Zend OPcache v7.4.26, Copyright (c), by Zend Technologies
Hello, Webhosting
Please mind that by default the full path should be used, e.g.:
# /opt/plesk/php/8.1/bin/php -v
make sure the PHP binaries are in place and the handler was added to chroot properly. You can later create an alias to call the php binary without a full pth.
This article should be updated with the information from https://docs.plesk.com/release-notes/obsidian/change-log/#plesk-18044
Please sign in to leave a comment.