Skip to content

Instantly share code, notes, and snippets.

@bigprof
Last active July 13, 2022 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigprof/467cee653ca3661ab869e4c2cf4626ab to your computer and use it in GitHub Desktop.
Save bigprof/467cee653ca3661ab869e4c2cf4626ab to your computer and use it in GitHub Desktop.
Bash script to switch PHP version on Debian/Ubuntu

Bash script to switch PHP version on Debian/Ubuntu

You can run multiple versions of PHP on the same server. This is useful in many cases. For example:

  • Testing the same PHP app under multiple PHP versions.
  • Running several PHP apps that each requires a different version of PHP

I personally find this very useful to rapidly test apps generated by AppGini under multiple PHP versions.

AppGini is a nocode/lowcode graphical tool for configuring and generating web applications for easily managing data for small businesses, organizations, teams in small/large companies, or even your indvidual projects. Apps created by AppGini support multiple users, are responsive and mobile-friendly, work on LAMP stack for easy installation and wide support, allow importing and exporting of data to CSV, and many more features. A free fully working trial of AppGini can be downloaded from here

end of shameless self-promotion ;)

Installation and usage

First, you should follow step 1 of this tutorial to install multiple versions of PHP. Next, create the below setphp file, perhaps in your home directory. Make it executable by running:

chmod +x setphp

Finally, to switch to a specific PHP version:

~/setphp 7.4

This script takes care of setting both PHP-CLI and PHP-FPM (the PHP module for Apache), as well as restarting Apache to apply the changes.

#!/usr/bin/bash
versions=("7.0" "7.1" "7.2" "7.3" "7.4" "8.0" "8.1" "8.2")
if [[ ! " ${versions[*]} " =~ " ${1} " ]]; then
echo "Usage: setphp version"
echo " Available versions: 7.0-7.4, 8.0-8.2"
exit
fi
# disable all PHP versions
for ver in ${versions[@]}; do
sudo a2disconf -q php$ver-fpm
done
sudo update-alternatives --quiet --set php /usr/bin/php$1
sudo a2enmod -q actions fcgid alias proxy_fcgi setenvif
sudo a2enconf -q php$1-fpm
sudo systemctl restart apache2
php -v | head -n 1 | cut -c 1-10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment