Set up Drush globally for any/all users on Plesk with fallback

My current version of Plesk Obsidian is 18.0.55. The OS is Ubuntu 20.04.6.

Although many of these steps is generic, there are steps that are specific to Plesk. This tutorial is also for dedicated servers and will require root access.

After all steps are complete, all users will have available Drush available to them via SSH. This set up will look for a copy of Drush in your project (Drupal 8/9/10). If it doesn't find one, it'll fallback to Drush 8. This means we can use Drush for Drupal 6/7/8/9/10+.

1. Step 1: Install Drush launcher

Create a directory to download it to and enter it

mkdir ~/drush-launcher && cd ~/drush-launcher

Download the latest copy

wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/latest/download/drush.phar

Make it executable

chmod +x drush.phar

Move the file to somewhere in the PATH.

mv drush.phar /usr/local/bin/drush

2. Install Drush 8

Create a directory to download it to and enter it

mkdir ~/drush8 && cd ~/drush8

Download the latest copy

wget -O drush.phar https://github.com/drush-ops/drush/releases/download/8.4.12/drush.phar

Make it executable

chmod +x drush.phar

Move the file to somewhere in the PATH.

mv drush.phar /usr/local/bin/drush8

3. Set up the fallback

Open /root/.bashrc and add the setting at the bottom of the file

echo "export DRUSH_LAUNCHER_FALLBACK=/usr/local/bin/drush8" >> /root/.bashrc

4. Change the PHP used on the CLI for root

This is only needed if you want to use Drush as root on Drupal websites. It's not necessary on Drupal 9 and below at the time of writing. Drupal 10 however requires PHP 8.1 and Plesk uses 7.4 by default.

Confirm the version that is in use.

php --version

Confirm the location is the same as mine. It should be /usr/bin/php

which php

Look at the available versions

ls /opt/plesk/php/

Back the currently used version up, just incase.

mv /usr/bin/php /usr/bin/php-backup

Create a symbolic link to the version you want to use (replace 8.1 to a version seen earlier for your server)

ln -s /opt/plesk/php/8.1/bin/php /usr/bin/php

Confirm the expected version of PHP is being used

php --version

At this point, Drush should now be working for root. To test if the fallback is working, run this inside a Drupal 6/7 website or outside of a Drupal website.

drush --version

To test it loads Drush included in Drupal 8+, go to the project root and type the above. It should be different.

6. Update open_basedir PHP setting

In order for Drush to work for your other users, this PHP setting must be empty. This setting leads you to believe it is a strong security feature. My research taught me that malicious PHP scripts can bypass this setting anyway. Therefore negating it's importance in my mind. Please do your own research to confirm my findings.

My server only hosts Drupal websites and only my team accesses the server via SSH using keys. So I've opted to change this setting for all domains at once. 

Option 1: Update all existing domains

First I create a list of all domains

MYSQL_PWD=`cat /etc/psa/.psa.shadow` mysql psa -uadmin -Ne"select name from domains where htype='vrt_hst'" > /root/list.txt

Create a file with the PHP setting in it

echo 'open_basedir = none' > /root/php.txt

Now use the above files to make the change to all domains in the list. (This one took a while to run)

cat /root/list.txt | while read dom; do /usr/local/psa/bin/site --update-php-settings $dom -settings /root/php.txt;done

Option2: Change setting for individual domains

If you only want this available to certain domains, you can go to the domain in Plesk and turn it off via the PHP settings.

Set a default value for new domains

To turn it off by default for new domains, you need to go to Service Plans > plan_name > PHP Settings.

7. Change PHP CLI version for a subscription

This is needed if your Drupal project requires a different version of PHP to the default. Mine defaults to 7.4. That isn't compatible with Drupal 10 and so it throws Composer requirements error about PHP. It's important to understand that the PHP version your website uses is not the same as the CLI.

Open the webspaces root using your preferred tool. Ie via Plesk or a FTP client or SSH. Open the file .php-version and edit it to your requirements. 

Conclusion

Drush can now be available to root and your subscription users via SSH. You can have it available to everyone by default or selected subscriptions. When Drush isn't available in your current project, it'll fallback to Drush 8.

This took me days to piece together. I hope this saves you from the pain I went through.

Post type: 
Technical
Drupal version: 
Non specific