PHP is a programming language used for developing web applications. You must install PHP packages on a Ubuntu system to run the application written on it. Generally, it is used to create e-commerce websites, blogs, and API applications. If you’re looking for an easy way to install PHP on Ubuntu, look no further. In this blog post, we’ll show you how to do it quickly and easily.

You can install any of the versions as required for your application. The new application developers are suggested to use the latest PHP version ie PHP 8.1.
In this tutorial, you will learn how to install PHP on Ubuntu 22.04 LTS system. This tutorial is also compatible with Ubuntu 20.04 and 18.04 systems.

Before We Begin

First, login to the Ubuntu 22.04 via console. Then update the Apt cache and upgrade the current packages of system using the following command:

 Currently, there are three supported versions of PHP, i.e PHP 5.6, 7.0, and 8.0. Meaning PHP 5.3, 5.4, and 5.5 have all reached the end of life; they are no longer supported with security updates.  

Install PHP (5.6, 7.x, 8.1) on Ubuntu Using PPA

Ø  First start by adding Ondřej Surý PPA to install different versions of PHP – PHP 5.6, PHP 7.x, and PHP 8.1 on the Ubuntu system.

$ sudo apt install software-properties-common

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

Ø    Next, update the system as follows 

$ sudo apt-get update

Ø    Now install different supported versions of PHP as follows.

 For Apache Web Server

  $ sudo apt install php5.6  

  $ sudo apt install php7.0  

  $ sudo apt install php7.1   

  $ sudo apt install php7.2  

  $ sudo apt install php7.3                                                                                                         

  $ sudo apt install php7.4  

  $ sudo apt install php8.0

  $ sudo apt install php8.1

Ø  Now you can install the most required PHP modules from the list.

———— Install PHP Modules ————

$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql

$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql

$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql

$ sudo apt install php7.2-cli php7.2-xml php7.2-mysql

$ sudo apt install php7.3-cli php7.3-xml php7.3-mysql

$ sudo apt install php7.3-cli php7.4-xml php7.4-mysql  

$ sudo apt install php7.3-cli php8.0-xml php8.0-mysql

$ sudo apt install php7.3-cli php8.1-xml php8.1-mysql

Ø  Finally, verify your default PHP version used on your system like this.

Check Active PHP Version

Now after installation verify that the correct version of PHP is installed by checking the version number by the below-mentioned command:

$ php -v

About PHP Configuration Files

Configuration files are stored under a directory with version number under /etc/php directory. For example PHP 8.1 configuration files located as below:

  • Main PHP configuration file location:

PHP CLI: /etc/php/8.1/cli/php.ini

Apache: /etc/php/8.1/apache2/php.ini

PHP FPM: /etc/php/8.1/fpm/php.ini

  • All the installed PHP modules are stored under /etc/php/8.1/mods-available directory.
  • PHP Active modules configuration directory location:

PHP CLI: /etc/php/8.1/cli/conf.d/

Apache: /etc/php/8.1/apache2/conf.d/

PHP FPM: /etc/php/8.1/fpm/conf.d/

To check files for the other PHP versions, just change the PHP version number (8.1 in the above example) in the files and directory path.

Switch Default PHP Version for CLI

You can use update-alternatives command to set the default PHP version.

$ sudo updates-alternatives –config php

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status

————————————————————

* 1           /usr/bin/php8.1   81        auto mode

  2           /usr/bin/php5.6   56        manual mode

  3            /usr/bin/php7.4   74        manual mode

  4            /usr/bin/php8.0   80        manual mode

  5            /usr/bin/php8.1   81        manual mode

Press  to keep the current choice[*], or type selection number: 2

The above output shows all the installed PHP versions on your system. Selection number 2 set PHP 7.4 as the default PHP version for the command line.

To enable module for current php version and disable for previous version:

$ sudo a2enmod php7.2

$ sudo a2dismod php7.4

$ systemctl restart apache2

Uninstalling PHP

If any PHP version is no more required, can be removed from the system. That will free the disk space as well as system security.

To uninstall any PHP version just type:

$ sudo apt remove php7.2

sudo apt remove php5.6

Also uninstall all the modules for that version with the following command:

$ sudo apt remove php*

Conclusion

This tutorial provides you with the instructions to install PHP on Ubuntu 22.04. The Ondrej PPA allows us to quickly install PHP on Ubuntu systems. It also allows us to install multiple PHP versions on a single system. You can switch to any PHP version as default anytime with the update-alternative utility.

Leave a Reply

Your email address will not be published. Required fields are marked *