How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS

How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS

This tutorial provides the steps required to install multiple versions of PHP on Ubuntu 20.04 LTS. It shows how to install PHP 7 and PHP 8 on Ubuntu 20.04 LTS.

December 05, 2021

This tutorial provides all the steps required to install multiple versions of PHP including PHP 7 and PHP 8 on Ubuntu 20.04 LTS using PPA. The steps should be similar to the other versions of Ubuntu and Linux-based systems. In case you are planning to use PHP with the Apache HTTP Server and MySQL database server, you can install Apache 2 and MySQL 8 before starting the installation of PHP 8. It can be done by following the guides - How To Install Apache 2 On Ubuntu 20.04 LTS and How To Install MySQL 8 on Ubuntu 20.04 LTS. Also, PHP can be used with NGINX Web Server instead of the Apache HTTP Server. NGINX is widely used as a load balancer and reverse proxy for the Apache Web Server and other servers. You can follow How To Install And Configure Nginx on Ubuntu 20.04 LTS and Configure Virtual Host Or Server Block On Nginx.

Notes: The default version of PHP available from default distribution for Ubuntu 20.04 LTS is PHP 7.4. You can also follow How To Install PHP 7 On Ubuntu 20.04 LTS from Ubuntu official repositories.

Install PHP 7 from PPA

In this step, we will install PHP 7 using the PPA from Ondrej for Ubuntu 20.04 LTS release. Now, enable the PHP repository from Ondřej Surý using the commands as shown below.

# Refresh indexes
sudo apt update

sudo apt install software-properties-common

# Add PHP Repository
sudo add-apt-repository ppa:ondrej/php

# Press ENTER to enable the repository

We can install PHP 7 on Ubuntu 20.04 LTS using the commands shown below.

# Refresh indexes
sudo apt update

# Install PHP 7 on Ubuntu 20.04 LTS
sudo apt install php7.0

# The command will ask to confirm the installation. Press Y and hit Enter to confirm the installation.
# The command installs apache2 and libapache2-mod-php7.0 in case it's not installed
# The command will also install libapache2-mod-php7.0 in case Apache is already installed

# Verify Apache
apache2 -version

# Output
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2021-10-14T16:24:43

# Verify PHP
php --version

# Output
PHP 7.0.33-57+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:39:53) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-57+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

# Autoclean
sudo apt autoclean

# Autoremove
sudo apt autoremove
# OR
sudo apt --purge autoremove

The important paths and files of the PHP installation are /usr/lib/php/7.0, /usr/bin/php7.0, /etc/php/7.0, and /etc/php/7.0/cli/php.ini.

PHP for NGINX

We can install PHP FPM and configure it for Nginx. This will eliminate the need for the Apache Web Server and Nginx can handle the PHP requests.

# FPM
sudo apt install php7.0-fpm

# Output
----
----
Processing triggers for php7.0-fpm (7.0.33-57+ubuntu20.04.1+deb.sury.org+1) ...
NOTICE: Not enabling PHP 7.0 FPM by default.
NOTICE: To enable PHP 7.0 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php7.0-fpm
NOTICE: You are seeing this message because you have apache2 package installed.

# Check Status
systemctl status php7.0-fpm

# Output
php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 20:26:03 PST; 50s ago Docs: man:php-fpm7.0(8) Process: 21734 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.0/fpm/pool.d/www.co> Main PID: 21731 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 14.6M CGroup: /system.slice/php7.0-fpm.service ├─21731 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─21732 php-fpm: pool www └─21733 php-fpm: pool www Dec 04 20:26:03 ubuntu systemd[1]: Starting The PHP 7.0 FastCGI Process Manager... Dec 04 20:26:03 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

You can also follow How To Install PHP For Nginx On Ubuntu 20.04 LTS for further details.

Install Additional Extension

You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.

# Install MySQL Extension
sudo apt install php7.0-mysql

# Enable PDO module
sudo phpenmod pdo_mysql

# Install MySQL Extension - Required for Wordpress Installation
sudo apt install php-mysql

# Install CGI and CLI if not installed by default
sudo apt install php7.0-common php7.0-cli php7.0-cgi

# Install CURL extensions
sudo apt install php7.0-curl

# Install PHP GD and Imagick
sudo apt install php7.0-gd php-imagick

# Multibyte String, Internationalization and Spell Check
sudo apt install php7.0-mbstring php7.0-intl php7.0-pspell

# Multibyte String, Internationalization and Spell Check - Required for Wordpress Installation
sudo apt install php-mbstring php-intl php-pspell

# Emails
sudo apt install php7.0-imap

# SQLite Database
sudo apt install php7.0-sqlite3

# Tidy and XML RPC
sudo apt install php7.0-tidy php7.0-xmlrpc

# Excel
sudo apt install php7.0-xsl

Install OPcache to enable caching at the bytecode level.

# Install OPcache extension
sudo apt install php7.0-opcache

Install the extensions to handle compressed files.

# Install Zip
sudo apt install php7.0-zip

These are the steps required to install PHP 7 on Ubuntu 20.04 LTS using the PPA from Ondřej Surý. We have also installed the additional packages that are mostly required for the website development.

Verify PHP using phpinfo()

We can also verify the PHP and the installed packages by checking the output of the phpinfo method. Now, add the info.php file at the root of your web server and execute it using your preferred browser. It should show the output similar to Fig 1.

# info.php
<?php
echo phpinfo();

Install Multiple Versions Of PHP On Ubuntu 20.04 LTS - PHP 7 Info

Fig 1

Install PHP 8 from PPA

In this step, we will install PHP 8 using the PPA from Ondrej for Ubuntu 20.04 LTS release. We can install PHP 7 on Ubuntu 20.04 LTS using the commands shown below.

# Refresh indexes
sudo apt update

# Install PHP 8 on Ubuntu 20.04 LTS
sudo apt install php8.0

# The command will ask to confirm the installation. Press Y and hit Enter to confirm the installation.
# The command will also install libapache2-mod-php8.0

# Verify PHP
php --version

# Output
PHP 8.0.13 (cli) (built: Nov 22 2021 09:50:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies

# Autoclean
sudo apt autoclean

# Autoremove
sudo apt autoremove
# OR
sudo apt --purge autoremove

The important paths and files of the PHP installation are /usr/lib/php/8.0, /usr/bin/php8.0, /etc/php/8.0, and /etc/php/8.0/cli/php.ini.

PHP for NGINX

We can install PHP FPM and configure it for Nginx. This will eliminate the need for the Apache Web Server and Nginx can handle the PHP requests.

# FPM
sudo apt install php8.0-fpm

# Output
----
----
Processing triggers for php8.0-fpm (8.0.13-1+ubuntu20.04.1+deb.sury.org+2) ...
NOTICE: Not enabling PHP 8.0 FPM by default.
NOTICE: To enable PHP 8.0 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php8.0-fpm
NOTICE: You are seeing this message because you have apache2 package installed.

# Check Status
systemctl status php8.0-fpm

# Output
php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 21:11:34 PST; 24s ago Docs: man:php-fpm8.0(8) Process: 50827 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.co> Main PID: 50824 (php-fpm8.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 7.9M CGroup: /system.slice/php8.0-fpm.service ├─50824 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf) ├─50825 php-fpm: pool www └─50826 php-fpm: pool www Dec 04 21:11:34 ubuntu systemd[1]: Starting The PHP 8.0 FastCGI Process Manager... Dec 04 21:11:34 ubuntu systemd[1]: Started The PHP 8.0 FastCGI Process Manager.

Install Additional Extension

You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it's totally optional to install these extensions.

# Install MySQL Extension
sudo apt install php8.0-mysql

# Install CGI and CLI if not installed by default
sudo apt install php8.0-common php8.0-cli php8.0-cgi

# Install CURL extensions
sudo apt install php8.0-curl

# Install PHP GD and Imagick
sudo apt install php8.0-gd php-imagick

# Multibyte String, Internationalization and Spell Check
sudo apt install php8.0-mbstring php8.0-intl php8.0-pspell

# Emails
sudo apt install php8.0-imap

# SQLite Database
sudo apt install php8.0-sqlite3

# Tidy and XML RPC
sudo apt install php8.0-tidy php8.0-xmlrpc

# Excel
sudo apt install php8.0-xsl

Install OPcache to enable caching at the bytecode level.

# Install OPcache extension
sudo apt install php8.0-opcache

Install the extensions to handle compressed files.

# Install Zip
sudo apt install php8.0-zip

These are the steps required to install PHP 8 on Ubuntu 20.04 LTS using the PPA from Ondřej Surý. We have also installed the additional packages that are mostly required for the website development.

Other Versions

Similarly, we can also install the other versions of PHP including PHP 7.1, PHP 7.4, PHP 8.1, etc.

Verify PHP using phpinfo()

We can also verify the PHP and the installed packages by checking the output of the phpinfo method as we did for PHP 7. If we open the info.php file using the Web Browser as we did for PHP 7, it still shows PHP 7 info instead of PHP 8. You can follow How To Switch PHP Version on Ubuntu 20.04 LTS to switch the PHP versions for CLI, Apache Web Server, and NGINX.

Summary

This tutorial provided all the steps required to install multiple versions of PHP including PHP 7 and PHP 8 on Ubuntu 20.04 LTS and also enable the extension for the Apache HTTP Server. We have also installed the popular extensions for PHP which are required to host web applications.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS