paint-brush
How to Have Multiple Versions of PHP on Ubuntuby@magepsycho
1,710 reads
1,710 reads

How to Have Multiple Versions of PHP on Ubuntu

by Raj KBJune 27th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to use a specific PHP version as per application? How to switch the PHP version in the Nginx’s server block? How easily you can change the PHP CLI and Web SAPI version. For Web application, once the required PHP version is set, they will work independently on specific PHP versions. For example, Magento 2 runs on Nginx with PHP 7.2, you can simply change the value of PHP version to a specific version of the application. You can find the list of PHP executables with the following command:://://://www.magepsycho.com/php-on-ubuntu-1604/
featured image - How to Have Multiple Versions of PHP on Ubuntu
Raj KB HackerNoon profile picture

There are many tools like Docker, Vagrant, VirtualBox, etc. that gives you the power to achieve the multiple versions of PHP in your system. But I want to stay away as much as possible from third-party tools and try to leverage the native as much as I can.

So want to learn how to install multiple versions of PHP on the Ubuntu system? Okay, that’s great.

Let’s assume, you have

  • OS: Ubuntu 16.04
  • PHP: 7.1 (PHP-FPM)
  • Nginx: 1.10

And want to install

PHP 7.2
.

Install PHP 7.2

Run the following commands to update the PHP repository & the Ubuntu system

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Now you are able to install the new version of PHP

sudo apt install -y php7.2-fpm
sudo apt install -y php7.2-dev
php -v

If

php -v
still pointing to the old PHP version, then run the following command

sudo update-alternatives --set php /usr/bin/php7.2


Install PHP 7.2 Modules

You may need some additional PHP modules in order for PHP to work with your applications.

You can install the most commonly needed modules especially required for Magento 2 with:

sudo apt install php7.2-mysql php7.2-bcmath php7.2-curl php7.2-xml php7.2-gd php7.2-intl php7.2-mcrypt php7.2-mbstring php7.2-soap php7.2-zip

To check installed PHP modules:

php -m | grep -E 'bcmath|ctype|curl|dom|gd|hash|iconv|intl|json|mbstring|mcrypt|openssl|pdo|soap|spl|xml|xsl|zip'

To search the PHP module:

sudo apt-cache search php7.2

Now, you have a new PHP 7.2 version and required modules installed. Similarly, you can install PHP 7.3, 7.4, 8.0 & so on and its related modules.

Now a big question is: How to use a specific PHP version as per application?

Before that, we should know how PHP is used.

PHP can be used as a CLI or web server module (mod_php, CGI, FPM).


Switching PHP CLI Version

PHP’s

php
command is linked to
/usr/bin/php
, which is symlinked to
/etc/alternatives/php
, which again is symlinked to actual PHP, example
/usr/bin/php7.2
.

So here lies the logic of switching PHP CLI versions.

By default, the PHP installation changes the CLI version to the new one.

In case if you want to switch to a specific version, say PHP 7.2, you can use the

update-alternatives
command

sudo update-alternatives --set php /usr/bin/php7.2
php -v

You can find the list of PHP executables with

ll /usr/bin | grep php

You might need to run

update-alternatives
to set the required PHP version if you are running `composer` commands or any CLI PHP script.


Switching PHP Web Server Version

As you probably know, Nginx runs PHP code via PHP-FPM, which listens on a Unix socket.

And that makes it easier to switch the PHP version in the Nginx’s server block.

If you want to point any application, say Magento 2 running on Nginx with PHP 7.2, you can simply change the value of

fastcgi_pass
directive as:

You can find the list of PHP executables with

fastcgi_pass unix:/run/php/php7.2-fpm.sock

To find the list of Unix sockets for PHP-FPM

ll /run/php | grep sock

In order to verify the PHP version for Web SAPI, you can use

phpinfo()
in your .php file.


Conclusion

Some people might want to use Docker, Vagrant based solution for this. It’s Okay.

For me, I always prefer a native system without depending on third-party solutions. This minimizes the overhead and latency problem and hence makes the application development easier and faster.

Above you read, how easily you can change the PHP CLI and Web SAPI version. For CLI based you might have to run the

update-alternatives
command on demand. But for Web application, once the required PHP version is set, they will work independently on specific PHP versions.

Please comment below if you have any better solution for handling multiple PHP versions.

Previously published at https://blog.magepsycho.com/how-to-install-multiple-versions-of-php-on-ubuntu/