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 ExchangePlease 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.
For Nginx, we simply need to update the PHP-FPM socket in its configuration file. But before that make sure that the PHP-FPM is installed for your version and is running as a service.
Take a backup of the default configuration file and then open it up in your favourite text editor.
$ cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
$ sudo vim /etc/nginx/sites-available/default
Change the FastCGI backend to use the new PHP-FPM socket, save and exit the file
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Run the configuration test
$ nginx -t
Restart the web server
$ sudo service nginx restart
For more information visit here
How to upgrade the PHP version on my Nginx Ubuntu Droplet
Change PHP version with Nginx
Change PHP version Nginx uses
ubuntu - switch nginx php version for some websites - Stack Overflow
Videos
Interactive switching mode
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
Manual Switching
From PHP 5.6 => PHP 7.1
Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.
Apache:
$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
From PHP 7.1 => PHP 5.6
Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.
Apache:
$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php5.6
Source
$ sudo update-alternatives --config php
should work for all ubuntu versions after 16.04 (18.04, 20.04 and 22.04)
This is what you should see as a response
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php7.2 72 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.0 70 manual mode
3 /usr/bin/php7.1 71 manual mode
4 /usr/bin/php7.2 72 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Choose the appropriate version
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)
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 :)
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 tried to add it but get the error " “fastcgi_pass” directive is not allowed here in" when running nginx -t
Any ideas?
Thanks
This need tl be oart lof the location setting
And you need to set the sites folder … Serve PHP with PHP-FPM and NGINX | Linode Docs