Hi everybody , sure as a php developer, you will do many projects at a time and with many different php versions right? Realizing that installing and switching php version needs to include a guide so that I can reuse quickly, today I will write about the topic of installing and switching multi php version on ubuntu so that you can use it. Save and use offline.
Let’s get started.
1. Install php 5.6 & 7.3
First I will install the PHP version on ubuntu.
Open up your terminal and run the following commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 | sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install -y php5.6 sudo apt-get install -y php7.3 echo "* Installing PHP 5.6 extensions..." sudo apt-get install -y php5.6-common php5.6-cli php5.6-curl php5.6-mcrypt php5.6-soap php5.6-bz2 php5.6-gd php5.6-mysql php5.6-sqlite3 php5.6-json php5.6-opcache php5.6-xml php5.6-mbstring php5.6-readline php5.6-xmlrpc php5.6-zip echo "* Installing PHP 7.3 extensions..." sudo apt-get install -y php7.3-common php7.3-cli php7.3-curl php7.3-mcrypt php7.3-soap php7.3-bz2 php7.3-gd php7.3-mysql php7.3-sqlite3 php7.3-json php7.3-opcache php7.3-xml php7.3-mbstring php7.3-readline php7.3-xmlrpc php7.3-zip |
Thus, two versions of php 5.6 and 7.3 have been installed and the extentions on the two versions.
Note: Here I install php for apache.
Next comes the php verssion switch.
2. Command switch php version 5.6 => 7.3
To switch the version you need to dismoden the current php version and enmod the desired version of php and restart apache:
1 2 3 4 5 6 7 8 | sudo a2dismod php5.6 sudo a2enmod php7.3 sudo service apache2 restart sudo update-alternatives --set php /usr/bin/php7.3 sudo update-alternatives --set phpize /usr/bin/phpize7.3 sudo update-alternatives --set php-config /usr/bin/php-config7.3 |
3. Command switch PHP version 7.3 => 5.6
Similarly for php 5.6 => 7.3 you use the command:
1 2 3 4 5 6 7 8 | sudo a2dismod php7.3 sudo a2enmod php5.6 sudo service apache2 restart sudo update-alternatives --set php /usr/bin/php5.6 sudo update-alternatives --set phpize /usr/bin/phpize5.6 sudo update-alternatives --set php-config /usr/bin/php-config5.6 |
4. Create shell script file to switch version
Also you can create a shell script file to swith version for convenience:
- Create
swith_php.sh
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/bin/sh if [ "$1" = "73" ] then echo "Swith to php 7.3" sudo a2dismod php5.6 sudo a2enmod php7.3 sudo service apache2 restart sudo update-alternatives --set php /usr/bin/php7.3 sudo update-alternatives --set phpize /usr/bin/phpize7.3 sudo update-alternatives --set php-config /usr/bin/php-config7.3 echo "* Switch to PHP 7.3 complete." else echo "Swith to php 5.6" sudo a2dismod php7.3 sudo a2enmod php5.6 sudo service apache2 restart sudo update-alternatives --set php /usr/bin/php5.6 sudo update-alternatives --set phpize /usr/bin/phpize5.6 sudo update-alternatives --set php-config /usr/bin/php-config5.6 echo "* Switch to PHP 5.6 complete." fi |
- Add executable rights for the shell script file:
1 2 | chmod +x switch_php.sh |
- Convert PHP version with command:
1 2 3 4 5 6 | //Chuyển đổi sang PHP 7.3 ./switch_php.sh 73 //Chuyển đổi sang PHP 5.6 ./switch_php.sh 56 |
Note: You can do the same with other php versions
Refer
- https://www.tecmint.com/install-different-php-versions-in-ubuntu/
- https://vitux.com/how-to-install-php5-and-php7-on-ubuntu-18-04-lts/
Thank you everyone for watching my article, see you next post