How to Run Multiple PHP Versions with Apache on Ubuntu

Date:

How to Run Multiple PHP Versions with Apache on Ubuntu – PHP: Hypertext Preprocessor is a general-purpose programming language originally designed for web development. It was originally created by Rasmus Lerdorf in 1994; the PHP reference implementation is now produced by The PHP Group

How to Run Multiple PHP Versions with Apache on Ubuntu

Sometimes we need to run multiple PHP versions to cater multiple application requirements. So in this article we are going to discuss how to use multiple PHP version in same box.

You should already have Apache installed and serving web pages before following this guide. This guide was tested on Ubuntu 16.04 and 18.04 LTS

If you need to install Apache on Ubuntu, please see below article:

Apache, How To Configure Virtual Hosts In Ubuntu 16.04-18.04 LTS

Step 1: First start by adding Ondřej Surý PPA to install different versions of PHP

$ sudo apt install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php

Please run below command to update the system as follows

$ sudo apt-get update

Now you can start the installation of different PHP versions using below command

$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]
$ sudo apt install php7.2   [PHP 7.2]
$ sudo apt install php7.3   [PHP 7.3]

Now you can install most required PHP modules, example;

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

Please refer below article to install required PHP modules for Magento 2

Install Magento 2 On Ubuntu 16.04 | 18.04 With Apache2, Percona DB and PHP 7.2

Learn More about PHP, please visit official PHP page

To check default PHP version please run below command

$ php -v
PHP 7.2.24-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:29:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Step 2: How to change default PHP version

To change default PHP version please run bellow commands based on your requirement

$ sudo update-alternatives --set php /usr/bin/php7.0
$ sudo update-alternatives --set php /usr/bin/php7.1

How to enable / disable PHP versions on Ubuntu

$ sudo a2dismod php7.0
$ sudo a2enmod php7.1
$ sudo systemctl restart apache2

Step 3: How to mention different PHP versions for different sites using .htaccess

An .htaccess file is a directory-level configuration file supported by several web servers, used for configuration of website-access issues, such as URL redirection, URL shortening, access control, and more. The ‘dot’ before the file name makes it a hidden file in Unix-based environments.

To specify PHP version for your web application please add below lines to .htaccess file.

To switch to PHP 7.3

AddHandler application/x-httpd-php73 .php .php5 .php4 .php3

To change to PHP 7.2

AddHandler application/x-httpd-php72 .php .php5 .php4 .php3

To switch to PHP 7.1

AddHandler application/x-httpd-php71 .php .php5 .php4 .php3

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Subscribe

Popular

More like this
Related

How to Protect WordPress with Cloudflare

Cloudflare, Inc. is an American content delivery network and...

Facebook Outage

Mike Schroepfer - CTO @ Facebook. *Sincere* apologies to everyone...

Magento 2 One Page Checkout (One Step Checkout)

Magento 2 One Page Checkout - One Page checkout...