10

I have PHP 7.0 and 7.2 installed.

For now, I use PHP 7.0.

How can I switch to PHP 7.2?

0

3 Answers 3

26

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.

1
  • If it is helpful then accept answer. Apr 29, 2019 at 13:52
8

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

2
  • 1
    This is a critical step for nginx users. I was facing difficulty after switching version of PHP, restarting the php driver and nginx server, but the site would still not load. It wasn't until I saw this answer that I remembered I have the php version specified in the configuration file. Thanks!
    – Andrew Fox
    Feb 28, 2021 at 0:39
  • What if I have fastcgi_pass set to 127.0.0.1:9250? Changing it to a unix socket breaks my server and using update-alternatives only changes the CLI PHP version. Any ideas? Sep 18 at 8:09
1

For Apache:

sudo a2dismod php7.0
sudo a2enmod php7.2
sudo systemctl restart apache2

Reference: Link

2
  • 1
    Didn't OP ask about Nginx, not Apache?
    – Klesun
    Feb 29, 2020 at 16:23
  • a2 stands for Apache 2, so this won't work for Nginx. Aug 23, 2022 at 20:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.