How to switch between PHP versions on Ubuntu Nginx

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.


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