Liveasily Logo

Switch between any PHP versions by one custom Linux command

Switch between any PHP versions by one custom Linux command
technology php programming

Are you a web developer using multiple PHP versions on your Linux machine? If yes, Damn sure you feel irritated to switch between PHP versions frequently.

As a web developer, he/she need to have multiple versions of PHP installed on their computer to work on their daily projects. All of his/her projects may not support latest versions of PHP, So they need to switch between multiple PHP versions which is seriously irritating job.

Let's suppose you are using Apache or Nginx server on your machine and using multiple versions of PHP (7.1, 7.2 and 7.3). 

Below shown command is what every developer use to do to switch between versions.

 

 // to check current php version php -v  // process to switch to PHP 7.1 to PHP 7.3  sudo a2dismod php7.1  sudo a2enmod php7.3  sudo systemctl restart apache2  sudo ln -sfn /usr/bin/php7.3 /etc/alternatives/php

As shown above, you can get back to PHP 7.1 using 

 // to check current php version php -v  // process to switch to PHP 7.3 to PHP 7.1  sudo a2dismod php7.3  sudo a2enmod php7.1  sudo systemctl restart apache2  sudo ln -sfn /usr/bin/php7.1 /etc/alternatives/php

Ohh Gosh!! Repeating all these lines frequently... Noooooooo

Come on! Now you don't need to repeat all these boring Linux commands. Excited right?

Let's Get Started

Processes to setup

Open terminal and type following command

sudo vi ~/.bashrc

Scroll down to the last line of script and  the  visit my site function below.

switchphp ()  {     read -p "Which Version ? : "  php_version     local current_ver=`php -v | grep ^PHP | cut -d' ' -f2`     local cur_ver="$(echo $current_ver | head -c 3)"     if [[ "$php_version" == "7.1" ]]; then         echo "Switching to PHP 7.1"         sudo a2dismod php"$(echo $cur_ver)";         sudo a2enmod php7.1;         sudo systemctl restart apache2;         sudo ln -sfn /usr/bin/php7.1 /etc/alternatives/php         sudo service apache2 restart     elif [[ "$php_version" == "7.2" ]]; then         echo "Switching to PHP 7.2"         sudo a2dismod php"$(echo $cur_ver)";         sudo a2enmod php7.2;         sudo systemctl restart apache2;         sudo ln -sfn /usr/bin/php7.2 /etc/alternatives/php         sudo service apache2 restart     elif [[ "$php_version" == "7.3" ]]; then         echo "Switching to PHP 7.3"         sudo a2dismod php"$(echo $cur_ver)";         sudo a2enmod php7.3;         sudo systemctl restart apache2;         sudo ln -sfn /usr/bin/php7.3 /etc/alternatives/php         sudo service apache2 restart     else         echo "switchphp only works for php version 7.1, 7.2 and 7.3"     fi }

 

Process to use

Restart your terminal and you can access switchphp from anywhere on your terminal.

Now all you need to do is run switchphp on your terminal and type PHP version when asked and you are done.

 

SUPER DUPER EASY!

HAPPY CODING DEVELOPERS!