İçeriğe geç

Install PHP and Switching between installed PHP versions

Install PHP version 5.6

In order to install PHP version 5.6, first, open your Ubuntu Terminal and enter the following command in order to add the Ondrej PHP repository to your Ubuntu.

$ sudo add-apt-repository ppa:ondrej/php

This repository contains all the released versions of PHP till date.

Add Ondrej Ubuntu Repository

Once the Ondrej repository is added, you need to update your system’s repository indexwith that on the Internet. This way you can install the latest available version of a software on your system. Enter the following command in order to do so:

$ sudo apt-get update
Update the package list

Now is the time to install PHP 5.6 to your system. Enter the following command as sudo as only an authorized person can install/uninstall and configure software on Ubuntu:

$ sudo apt-get install -y php5.6
Install PHP 5.6

The software will then be installed on your system.

In order to check the version number of your installed PHP, run the following command:

$ php -v

or,

$ php --version

The command will also verify that PHP is now indeed installed on your system.

Check the PHP version

The output from my system shows that PHP 5.6.38 is installed on my system.

Switching between installed PHP versions

If you have two or more versions of PHP installed on your system, you can configure your system to use one of them as the default PHP version. For this, it is first important to learn which version is currently enabled as default on your Ubuntu system.

Check which version is enabled

We will describe two ways to check which PHP version is enabled on your system; one is through Apache2 and the other is through the CLI.

Through Apache2

Change the current directory to /etc/apache2 as follows:

$ cd /etc/apache2

In the apache2 directory, run the following command to list all the available modes of PHP on your system and know which one of them is currently enabled:

$ ls -l mods-*/*php*
PHP version enabled in Apache

In the output, you can see that the currently enabled version of PHP is highlighted. In our case, it is PHP 5.6.

Through CLI

It is also very simple to check the currently enabled version of PHP through the CLI. Run the following command which is used to update the default alternative to a software on Ubuntu and thus lists all the available alternatives.

$ sudo update-alternatives --config php
Change PHP CLI version

In the output of the above command, the currently enabled version of PHP is indicated by a * symbol. You can see that in our case, it is PHP 5.6.

Switch from PHP 5.6 to PHP 7.2

We will describe two ways to switch from PHP 5.6 to PHP 7.2; one is through Apache2 and the other is through the CLI.

Through Apache2

First, disable the currently enabled version of PHP through the following command:

$ sudo a2dismod php5.6
Disable PHP 5.6

And then, enable the other version of PHP through the following command:

$ sudo a2enmod php7.2
Enable PHP 7.2

Now when you restart the apache2 service through the following command, the PHP 7.2 will be enabled on your system.

$ sudo service apache2 restart
Restart Apache


Through CLI

Use the following command to update your system to now use PHP 7.2 as the default PHP version.

$ sudo update-alternatives --set php /usr/bin/php7.2
Change PHP CLI version

Alternatively, you can use the following command to achieve the same purpose:

$ sudo update-alternatives --config php
Alternative way to change CLI PHP on Ubuntu

The command lists all the available versions of PHP installed on your system. Enter the selection number of the version you want to enable on your system and hit enter. For example, if I enter 2, PHP 5.6 will be enabled on my system.

Switch from PHP 7.2 to PHP 5.6

We will describe two ways to switch from PHP 7.2 to PHP 5.6; one is through Apache2 and the other is through the CLI.

Through Apache2

First, disable the currently enabled version of PHP through the following command:

$ sudo a2dismod php7.2

And then, enable the other version of PHP through the following command:

$ sudo a2enmod php5.6

Now when you restart the apache2 service through the following command, the PHP 5.6 will be enabled on your system.

$ sudo service apache2 restart

Through CLI

Use the following command to update your system to now use PHP 5.6 as the default PHP version.

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

Alternatively, you can use the following command to achieve the same purpose:

$ sudo update-alternatives --config php

The command lists all the available versions of PHP installed on your system. Enter the selection number of the version you want to enable on your system and hit enter so that the new version will be enabled.

This article will guide you in installing your desired version of PHP on Ubuntu 18.04. If you have more than one version of PHP installed on your system, the article also helps you in checking which version is currently enabled and also how to switch from one version to the other.

Karim Buzdar

Kategori:LinuxPHPWEB

İlk Yorumu Siz Yapın

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir