How to Install Multi-PHP versions on Ubuntu 20.04(Amazon EC2 server)

tanmaygi

Tanmay Shukla

Posted on July 13, 2022

How to Install Multi-PHP versions on Ubuntu 20.04(Amazon EC2 server)

Sometimes we need different applications (with different PHP versions running on it) but on a single server. So in this blog we’re going to do it step-by-step and configure NGINX to define our desired PHP version.
We'll use AWS EC2 server and OS as Ubuntu 20.04. but you can do it on a machine of your choice as well.

Step 1: First, Add the PHP repository

By default, Ubuntu 20.04 ships with the PHP version 7.4. So you'll need to add the PHP repository in your system to install the multiple PHP versions.

apt-get install software-properties-common gnupg2 -y
Enter fullscreen mode Exit fullscreen mode

Once all the dependencies are installed, add the PHP repository using the following command:

add-apt-repository ppa:ondrej/php
Enter fullscreen mode Exit fullscreen mode

Now, update the repository with the following command:

apt-get update -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Multiple Versions of PHP (7.2, 7.4, 8.0 and 8.1)

To install multiple PHP version, we'll use the following command:

  • for PHP 7.2
apt-get install php7.2 php7.2-fpm php7.2-cli -y
Enter fullscreen mode Exit fullscreen mode
  • for PHP 7.4
apt-get install php7.4 php7.4-fpm php7.4-cli -y
Enter fullscreen mode Exit fullscreen mode
  • For PHP 8.0
apt-get install php8.0 php8.0-fpm php8.0-cli -y
Enter fullscreen mode Exit fullscreen mode
  • For PHP 8.1
apt-get install php8.1 php8.1-fpm php8.1-cli -y
Enter fullscreen mode Exit fullscreen mode

At this point, all the PHP versions are installed in your system.

Set the default PHP version for the Command Line

Before that, first check the current version of CLI that PHP running by the following command:

php --version
Enter fullscreen mode Exit fullscreen mode

we should see the following output

PHP 8.0.5 (CLI) (built: May 3, 2021 11:30:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
   with Zend OPcache v8.0.5, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

As we can see that PHP8.0 is the default PHP command line version. Now set the default command line PHP version to PHP 7.4 using the below command:

update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode

We'll be asked to set the default PHP version as shown below.

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.2   72        manual mode
  2            /usr/bin/php7.4   74        manual mode
  3            /usr/bin/php8.0   80        manual mode

Press "enter" to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
Enter fullscreen mode Exit fullscreen mode

Now, select your desired version and hit “Enter” to set the default version.
Now verify your default PHP version using the following command:

php --version
Enter fullscreen mode Exit fullscreen mode

Now it will show 7.4 version running on CLI.

Step 4: Switch Between Multiple PHP Versions with Nginx

We can switch between multiple PHP versions easily by configuring the Nginx virtual host and PHP-FPM.
PHP-FPM (Fast CGI Process Manager) is an alternative PHP Fast CGI implementation with some additional features useful for sites of any size, especially busier sites.
First, install the Nginx server package with the following command:

apt-get install nginx -y
Enter fullscreen mode Exit fullscreen mode

Next, you will need to test which PHP version is used by your Nginx server.
To do so, create an info.php file in your nginx web root directory:

nano /var/www/html/info.php
Enter fullscreen mode Exit fullscreen mode

and add the following line inside the editor:

<?php phpinfo(); ?>
Enter fullscreen mode Exit fullscreen mode

Save and close the file.

Important Point

  • Now, to use different PHP versions on a same server, we'll need to edit the NGINX default virtual host configuration file as below:
nano /etc/nginx/sites-enabled/default
Enter fullscreen mode Exit fullscreen mode

Then remove all the line and add the following lines:

server { # php/fastcgi
        listen       80;
        server_name _;
        root /var/www/html/;
        index info.php;
        access_log /var/log/nginx/access_log;
        error_log /var/log/nginx/error_log;
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
Enter fullscreen mode Exit fullscreen mode

You should see the following output:

 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
Enter fullscreen mode Exit fullscreen mode

Next, restart NGINX to apply the changes:

sysytemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Finally, open your web browser and access the info.php page using your server IP (Public IPv4 address).
You can get your server's public IP via the following command:

curl icanhazip.com
Enter fullscreen mode Exit fullscreen mode

php 7.2

  • To use different version like PHP version 7.4 in our case with nginx server, edit the Nginx default virtual host configuration file as below:
nano /etc/nginx/sites-enabled/default
Enter fullscreen mode Exit fullscreen mode

Now , just find the following line:

fastcgi_pass unix:/run/php/php7.2-fpm.sock;
Enter fullscreen mode Exit fullscreen mode

And replace it with f=below command:

fastcgi_pass unix:/run/php/php7.4-fpm.sock;
Enter fullscreen mode Exit fullscreen mode

Save and close the file then restart the Nginx to apply the changes

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Now you'll see PHP version 7.4 running when you hit enter to http://your-server-ip/info.php
php 7.4

Summary: The goal of this blog is to help you to host multiple web applications with different PHP version, whether in LAMP or LEMP stack.

💖 💪 🙅 🚩
tanmaygi
Tanmay Shukla

Posted on July 13, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related