Please use to below command

sudo update-alternatives --config php

After run above command select the PHP version that you need to use.

Press to keep the current choice[*], or type selection number: For example 2

After switching below command used to restart the PHP and Nginx server.

sudo service nginx restart
sudo service php7.1-fpm or php7.2-fpm  restart

Please try above code. If you have any help let me know.

Answer from Kamlesh Solanki on Stack Exchange
Discussions

How to upgrade the PHP version on my Nginx Ubuntu Droplet
I’ve been going over the community ... a question regarding the upgrade of a PHP version appears. As such I’ve decided to create this question so it can be easily found and it’s universal, at least for Ubuntu-based droplets using Nginx.... More on digitalocean.com
🌐 digitalocean.com
1
January 25, 2021
Change PHP version with Nginx
SYSTEM INFORMATION OS type and version Ubuntu Linux 20.04.4 Webmin version 1.994 Virtualmin version 7.1-1 Related packages php, nginx Hello! I am trying to change the version of PHP on my server. I can’t seem to change it at the domain or subdomain levels. As you can see from the attached ... More on forum.virtualmin.com
🌐 forum.virtualmin.com
0
0
July 27, 2022
Change PHP version Nginx uses
Installed PHP7.4 on Ubuntu 18.04, running Nginx only no Apache, the conf file is in /etc/nginx/conf.d not /etc/nginx/sites-available/default in fact the sites-available folder is not even there. And the line “fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;” is not in the conf file, I have ... More on community.spiceworks.com
🌐 community.spiceworks.com
2
7
August 9, 2020
ubuntu - switch nginx php version for some websites - Stack Overflow
I have digitalocean server and I manage it by laravel forge. The server configured with php7.3 an it is ok. I wanna use for some websites php5.6 and for some websites php7.3. So I want somehow swit... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
11

Okay, you want to run multiple PHP version through nginx, the configure file should includes the specific path where you put your PHP scripts in different version or extension name.

However, I would like to explain the SF question link given in your post.

That answer is giving you a way to modify the conf of your nginx, which assumes that questioner has background in nginx.

By giving a specific port in conf, nginx will make the script executed through that FastCGI channel.

Let's pretend you have installed different PHP version in your server, and you have modified the port in php-fpm.conf.

You wish all php file executed in version 5.5.0 and php2 file executed in 5.6.0.

Example config of nginx as follows,

    listen       8080;
    server_name  localhost;

    root   html;

    location / {
        index  index.php index.php2;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }

    location ~ \.php2$ {
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }

In this case, php is processed through port 9000 and php2 goes to 9001

This is jsut a simple example, for advanced you can make two different folders to store php files, for example, /home/php55 and /home/php56, then edit your nginx.conf.

FOR YOUR EDITED QUESTION

If you want to try adding different servers to process the scripts in different version, sure you can do that, because nginx can be a load balancer, it can deal with this kind of problem as well.

First Application

server{
listen 80;
server_name php1.localhost; 

root   /home/www/php5.5;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9002; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}

Second Application

server{
listen 80;
server_name php2.localhost; 

root   /home/www/php5.6;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9004; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}

By using the above configuration, you can easily switch the different version result of PHP script, moreover, you can use NFS(if you are in a *nix environment) to mount the disk, in this case, you will no need to put files into two folders manually.

As for Fastcgi passing method, I suggest using PORT instead of Socket.

We all know Unix Socket has a better performance due to TCP port uses the whole network stack even on the same machine.

However, the time you save is very little, moreover, you may face this problem when using Socket in a high traffic time,

connect() to unix:/var/run/php5-fpm.sock failed or apr_socket_recv: Connection reset by peer (104)

In addition, TCP port can give you a faster way to manage if you put nginx and php-fpm in separated servers.

I'm using small laptop to edit this post, so codes are not pretty but I tried....

EDITED

Remember to modify your /etc/hosts to match your hostname (server_name in nginx.conf)

2 of 2
6

For testing purposes, I wanted to configure my nginx to use different php versions under different locations in the same server section. Finally it worked with this:

#Folder to run some tests with the new php-7
location ~ "^\/test_php7.0.3\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php7-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#Folder to run some tests with a custom compiled version of php5.6
location ~ "^\/test_php5.6.18\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php56-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#The default php version:
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Hope this helps someone else :)

🌐
Medium
medium.com › @boomerz7777 › how-you-can-install-or-use-multiple-php-version-in-nginx-ubuntu-22-04-5033cf95e128
How you can install or use multiple PHP version in NGINX [Ubuntu 22.04] | by Mas Arief | Medium
November 19, 2023 - First thing first let’s assume you already NGINX installed with your php (lets say PHP 8) Next you have to install PHP 5.6 (as an example) by typing this onto your terminal onto your terminal · #add PHP5.6 repository sudo add-apt-repository ppa:ondrej/php #install PHP5.6 sudo apt install -y php5.6 php5.6-fpm · After that, you can change your current NGINX config file by adding PHP5.6 sock as PHP configuration (in my case i use localhost file configuration)
🌐
InterServer
interserver.net › home › php › run multiple php versions with nginx on ubuntu
Run Multiple PHP versions with Nginx on Ubuntu - Interserver Tips
August 26, 2019 - Now, replace the PHP version in that line. For example, If you want to use PHP7.3 for that specific virtual host, the updated line should look like the following ... If you want to use PHP7.1, replace it with 7.1 and so on. After updating your virtual host configuration file, execute the following command to restart the Nginx server. You have to restart the Nginx server to apply the changes.
Find elsewhere
🌐
OpenSense Labs
opensenselabs.com › blog › howto-change-php-version-apache-or-nginx
HowTo: Change the PHP version on Apache or Nginx | Opensense Labs
Disable all the previously enabled ... file in the following way. ... For Nginx, we simply need to update the PHP-FPM socket in its configuration file....
🌐
AccuWeb Hosting
manage.accuwebhosting.com › knowledgebase › 3328 › How-to-Run-Multiple-PHP-Versions-with-Nginx-on-Ubuntu.html
How to Run Multiple PHP Versions with Nginx on Ubuntu? - AccuWebHosting
# Application with PHP 7.2 server ...t$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } } Create a symbolic link for both config files under the /etc/nginx/sites-enabled directory....
🌐
TecAdmin
tecadmin.net › install-multiple-php-version-nginx-ubuntu
How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04
April 26, 2025 - How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04. Step by step tutorial to host multiple website with different php versions via nginx
🌐
Tutorials24x7
php.tutorials24x7.com › blog › how-to-switch-php-version-on-ubuntu-20-04-lts
How To Switch PHP Version on Ubuntu 20.04 LTS | Tutorials24x7
December 5, 2021 - Switching PHP version for NGINX is straight-forward, since it uses PHP FPM to execute the PHP scripts. We can simply specify the PHP version while configuring the Server Block as shown below. You can also follow How To Install PHP For Nginx ...
🌐
Virtualmin
forum.virtualmin.com › t › change-php-version-with-nginx › 116197
Change PHP version with Nginx - Virtualmin - Virtualmin Community
July 27, 2022 - SYSTEM INFORMATION OS type and version Ubuntu Linux 20.04.4 Webmin version 1.994 Virtualmin version 7.1-1 Related packages php, nginx Hello! I am trying to change the version of PHP on my server. I can’t seem to change it at the domain or subdomain levels. As you can see from the attached screenshot, there is no default selection for PHP script execution mode, nor is there a default website directory, like there used to be.
🌐
CodeWithSusan
codewithsusan.com › notes › upgrade-php-nginx
Upgrading to PHP 8.2 (Ubuntu with Nginx) - CodeWithSusan.com
February 15, 2023 - Next you need to update your Nginx site configs (located in /etc/nginx/sites-enabled) to use your updated PHP handler. To do this, edit all of your site configs so that fastcgi_pass points to the appropriate php-fpm version.
🌐
Stack Overflow
stackoverflow.com › questions › 58687628 › switch-nginx-php-version-for-some-websites
ubuntu - switch nginx php version for some websites - Stack Overflow
and in laravel forge I edited this line fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; to fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; in the website nginx configuration file. SO phpinfo it changed to php5.6.
🌐
OneUptime
oneuptime.com › home › blog › how to switch between php versions on ubuntu
How to Switch Between PHP Versions on Ubuntu
3 weeks ago - Change it in the relevant server ... 's|php7.4-fpm.sock|php8.2-fpm.sock|g' /etc/nginx/sites-enabled/*.conf sudo nginx -t && sudo systemctl reload nginx ......
🌐
VSYS
vsys.host › how-to › php-7-4-to-8-0-on-ubuntu-20-04-with-nginx-the-upgrade
PHP 7.4 to 8.0 on Ubuntu 20.04 with Nginx: The Upgrade ★ VSYS Tutorials
November 14, 2022 - Reload the PHP-FPM 8.0 service to apply the changes. ... Restart the Nginx server so that your website operates on PHP 8.0 rather than 7.4.
🌐
Medium
medium.com › @krishjaiswal7562 › how-to-install-multiple-php-versions-with-nginx-on-ubuntu-753bd54e649
How to Install Multiple PHP Versions with Nginx on Ubuntu | by Krish Chaudhary | Medium
June 12, 2023 - Finally, restart Nginx and PHP-FPM service to apply all the configuration changes: $ sudo systemctl restart nginx $ sudo systemctl restart php7.4-fpm $ sudo systemctl restart php8.0-fpm · Both websites are now installed and configured run with multiple versions of PHP.
🌐
Server Fault
serverfault.com › questions › 1143951 › how-to-switch-php-versions-in-nginx-if-fastcgi-pass-is-pointing-to-a-tcp-socket
How to switch PHP versions in Nginx if fastcgi_pass is pointing to a TCP socket - Server Fault
Here's the edits I made to switch to another PHP version and as an added bonus, I figured out how to make it easy for me to switch between PHP versions in Nginx: I changed the fastcgi_pass parameter in /etc/nginx/sites-enabled/daan.conf to /var/run/php/php-fpm.sock.
🌐
Database Mart
databasemart.com › kb › install-php-for-nginx
How to Install PHP 8.1 for Nginx on Ubuntu
In this guide, we’ll walk you ... 8.1 for Nginx on Ubuntu 20.04. We’ll cover every step in detail so even beginners can easily follow along. By the end, you’ll be able to configure, test, and run your PHP website hosting environment. By default, Ubuntu 20.04 comes with PHP 7.4. To install the latest PHP versions, including ...