Install and Run Multiple PHP Versions in Ubuntu 22.04


I wanted to run both Drupal 7 and 9, as part of my work, in my machine now running on Ubuntu 22.04. Drupal 7 best run with PHP 5.6 while Drupal 9 requires either PHP 7.4 or 8.1

I could make it work after some research and try. Here is how I made it work.

Make it Ready To Install Multiple PHP

Install some necessary software.

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https

Add the Ondrej PPA which will supply various PHP releases for our machine.

LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

Update package manager state.

sudo apt update

Install PHP Versions of Your Choice

Ubuntu 22.04 supports PHP 8.1 out of the box and I had already installed. So, I just wanted to install PHP 5.6 to run Drupal 7. We can install PHP 5.6 with this command:

sudo apt install php5.6

We can install different versions of PHP by using similar command. For example, to install PHP 7.4, run:

sudo apt install php7.4

Install PHP Extensions as You Require

We may want to install various PHP extensions depending on the application we are running. For example, I had to install these extensions for Drupal 7 to work.

sudo apt install php5.6-mysql php5.6-mcrypt php5.6-mbstring php5.6-xml php5.6-curl 

Switching to a PHP Version We Wanted

We switch to any of installed PHP version as we require. We can switch PHP version for CLI and Apache independently.

Switch CLI PHP

To see what PHP version is active for CLI, run:

php -v

If you want to switch, then run:

sudo update-alternatives --config php

It will give an output like this:

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php8.1   81        manual mode

Press <enter> to keep the current choice[*], or type selection number:

As you see, we just need to enter a selection number.

Switch PHP for Apache Web Server

To see what PHP version is currently being used by Apache, run:

ls -l /etc/apache2/mods-*/*php*

It will given an output like this:

You can see that colored lines indicating what version being currently used.

To switch, first we need to disable active one with command:

sudo a2dismod php8.1

Then enabled the required version with the command:

sudo a2enmod php5.6

Now, if we check for the version being used by Apache. We will see something like:

Make sure to restart Apache:

sudo service apache2 restart

Bonus: Make Drupal 7 Connect With MySQL

When tried to run drush commands. It gave errors like this:

PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] database.inc:335                                                                     [warning]
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] database.inc:335                                                                     [warning]
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] database.inc:335                                                                     [warning]
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] database.inc:335                                                                     [warning]
PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] database.inc:335                                                                     [warning]
<h1>Additional uncaught exception thrown while handling exception.</h1><h2>Original</h2><p>PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client in drupal_is_denied() (line 2256 of /var/www/wwmbwma562/includes/bootstrap.inc).</p><h2>Additional</h2><p>PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client in _registry_check_code() (line 3553 of /var/www/wwmbwma562/includes/bootstrap.inc).</p><hr />Drush command terminated abnormally due to an unrecoverable error.

After searching over the web, I found this as best way to solve it.

Edit the mysqld configuration file:

sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf

And add following line at the end:

default-authentication-plugin=mysql_native_password

We may comment out this line when switching back to different PHP version.


Leave a Reply

Your email address will not be published.