PHP swich version

when on the server we have many PHP projects and different requirements, the solution is that we have to install multiple version PHP on switch, here’s how to switch version php

Let us check the default installed version of PHP using command:

$ php -v

Sample output:

PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

As you can see, the installed version of PHP is 7.2.7.

After testing your application for couple days, you find out that your application doesn’t support PHP 7.2. In that case, it is a good idea to have both PHP 5.x version and PHP 7.x version, so that you can easily switch between any supported version at any time.

You don’t need to remove PHP 7.x or reinstall LAMP stack. You can use both PHP 5.x and 7.x versions together.

I assume you didn’t uninstall php 5.6 in your system yet. Just in case, you removed it already, you can install it again using a PPA like below.

You can install PHP 5.6 from a PPA:

$ sudo add-apt-repository -y ppa:ondrej/php
$ sudo apt update
$ sudo apt install php5.6

1.1. Switch from PHP 7.x to PHP 5.x

1.1.1 First disable PHP 7.2 module using command:

$ sudo a2dismod php7.2

Sample output:

Module php7.2 disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

1.1.2. Next, enable PHP 5.6 module:

$ sudo a2enmod php5.6

1.1.3 Set PHP 5.6 as default version using command:

$ sudo update-alternatives --set php /usr/bin/php5.6

1.1.4. Alternatively, you can run the following command to set which system wide version of PHP you want to use by default.

$ sudo update-alternatives --config php

1.1.5. Enter the selection number to set it as default version or simply press ENTER to keep the current choice.

Change PHP default version
Change PHP default version

1.1.6. If you have installed other PHP extensions, set them as default as well.

$ sudo update-alternatives --set phar /usr/bin/phar5.6

1.1.7. Finally, restart your Apache web server:

$ sudo systemctl restart apache2

Now, check if PHP 5.6 is the default version or not:

$ php -v

Sample output:

PHP 5.6.37-1+ubuntu18.04.1+deb.sury.org+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *