Как переключить php ubuntu

How To Switch PHP Version on Ubuntu 20.04 LTS

This tutorial provides the steps required to switch among the multiple versions of PHP installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

The previous tutorial provided the steps required to install multiple PHP versions on Ubuntu 20.04 LTS. You can follow How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

Switch PHP versions — CLI

Verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 8.0.13 (cli) (built: Nov 22 2021 09:50:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies

We can configure multiple versions of PHP installed on Ubuntu 20.04 LTS using the commands as shown below.

# PHP 7.0 
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set phar /usr/bin/phar7.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.0

# PHP 8.0
sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set phar /usr/bin/phar8.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0

Now we can switch among PHP 7.0 or PHP 8.0 using the commands as shown below.

# Switch PHP
sudo update-alternatives --config php

# Output
There are 2 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.0 70 manual mode
* 2 /usr/bin/php8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

# Switch
sudo update-alternatives --config phar

# Output
There are 2 choices for the alternative phar (providing /usr/bin/phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar8.0 80 auto mode
1 /usr/bin/phar7.0 70 manual mode
* 2 /usr/bin/phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar7.0 to provide /usr/bin/phar (phar) in manual mode

# Switch
sudo update-alternatives --config phar.phar

# Output
There are 2 choices for the alternative phar.phar (providing /usr/bin/phar.phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar.phar8.0 80 auto mode
1 /usr/bin/phar.phar7.0 70 manual mode
* 2 /usr/bin/phar.phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar.phar7.0 to provide /usr/bin/phar.phar (phar.phar) in manual mode

Now, again verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 7.0.33-57+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:39:53) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-57+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

This is helpful for the applications reliable on the PHP in CLI mode to run console based programs. We can switch among the multiple versions of PHP installed on Ubuntu systems.

Читайте также:  Java jtable добавить строку

Switch PHP versions — Apache Web Server

Similar to CLI mode, we can also switch among multiple versions of PHP for the Apache Web Server. In the previous tutorial — How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS, we saw that the PHP version remains same i.e. PHP 7.0 even after installing the recent version of PHP i.e. PHP 8.0 as shown in Fig 1.

We can switch among the PHP versions for the Apache Web server using the commands as shown below.

# Disable PHP 7.0
sudo a2dismod php7.0

# Output
Module php7.0 disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

# Enable PHP 8.0
sudo a2enmod php8.0

# Output
Considering dependency mpm_prefork for php8.0:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.0:
Enabling module php8.0.
To activate the new configuration, you need to run:
systemctl restart apache2

# Restart Apache Web Server
sudo service apache2 restart

Now, again refresh the info.php using the Web Browser. It should be similar to Fig 2.

We can also switch back to PHP 7 using the same commands.

Switch PHP versions — Apache Web Server — PHP FPM

Instead of switch PHP version for Apache Web Server which impacts all the Virtual Hosts, we can also configure the selected Virtual Host to use the PHP version specified by us. We can do so using PHP FPM installed by us as shown in How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

# Check Status - PHP 7.0 FPM
systemctl status php7.0-fpm

# Output
php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 20:35:28 PST; 1h 14min ago Docs: man:php-fpm7.0(8) Main PID: 42546 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 18.1M CGroup: /system.slice/php7.0-fpm.service ├─42546 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─42548 php-fpm: pool www └─42549 php-fpm: pool www Dec 04 20:35:28 ubuntu systemd[1]: Starting The PHP 7.0 FastCGI Process Manager. Dec 04 20:35:28 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

# Check Status - PHP 8.0 FPM
systemctl status php8.0-fpm

# Output
php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 21:15:28 PST; 35min ago Docs: man:php-fpm8.0(8) Process: 61492 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.co> Main PID: 61489 (php-fpm8.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 10.7M CGroup: /system.slice/php8.0-fpm.service ├─61489 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf) ├─61490 php-fpm: pool www └─61491 php-fpm: pool www Dec 04 21:15:28 ubuntu systemd[1]: Starting The PHP 8.0 FastCGI Process Manager. Dec 04 21:15:28 ubuntu systemd[1]: Started The PHP 8.0 FastCGI Process Manager.

We can see that both PHP 7.0 FPM and PHP 8.0 FPM are in running state. Now, enable Apache2 to use multiple versions of PHP using the command as shown below.

# Install FCGID
sudo apt install libapache2-mod-fcgid

# Enable FCGID
sudo a2enmod actions fcgid alias proxy_fcgi

# Restart Apache
sudo service apache2 restart

Also, update the Virtual Host as shown below.


----
----


# For Apache version 2.4.10 and above
SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"


----
----

Also, reload Apache as shown below.

# Reload Apache
sudo service apache2 reload

Now, check the output of info.php using the Browser. It should show the configurations specific to PHP 7. Similarly, we can change PHP 7 to PHP 8 for the selected virtual host, without impacting the other virtual hosts.

Switch PHP versions — NGINX

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 On Ubuntu 20.04 LTS for more details.

# Server Block with PHP FPM
sudo nano /etc/nginx/sites-available/example.com

server <

.
.

# pass the PHP scripts to FastCGI
location ~ \.php$ root /var/www/example.com/html;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
>

.
.
>

Summary

This tutorial provided the steps required to switch among the multiple PHP versions installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

Источник

How to change PHP version in Ubuntu 20.04 console?

How can I change the PHP version used in console on Ubuntu 20.04? I have all versions in the /etc/php folder, but I don’t know where the configuration for the command line version is.

6 Answers 6

You can do this with update-alternatives . If you would like to do this interactively, you can do this:

sudo update-alternatives --config php 

If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:

sudo update-alternatives --set php /usr/bin/php7.4 

Of course, be sure to change php7.4 to the actual version you want to switch between.

Ok but it shows me this message: There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.2 Nothing to configure . I can see the usr/bin folder and theare really only php 7.2. Dont know what need to add there.

Please use following command which will ask you to select a number against your required PHP version

sudo update-alternatives --config php 

Then select your choice and press enter.

Ok but it shows me this message: There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.2 Nothing to configure . I can see the usr/bin folder and theare really only php 7.2. Dont know what need to add there.

@Čamo it means you don’t have multiple versions installed. You can install other versions before switch to other version.

Did you install the cli? Do this first:

$ sudo apt install php7.4-cli 

After that, you should see it listed under update-alternatives

$ update-alternatives --list php /usr/bin/php7.4 /usr/bin/php8.1 

Thank you this answer work great for me, of course now in 2023 I’ve adapt it for PHP 8.2: apt install php8.2-cli And now when I do a «php -v» I get the following answer: PHP 8.2.3 (cli) (built: Feb 14 2023 16:57:30) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.3, Copyright (c) Zend Technologies with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies Thank you!

i just try in Ubuntu20.04 and it works.

// to show the list php installed: sudo update-alternatives —config php

// choose number version that you wanna switch screenshoot switch php version in ubtuntu

alias setphp="sudo update-alternatives --config php;sudo update-alternatives --config phar; update-alternatives --config phar.phar; a2dismod php*.*;systemctl restart apache2" 

Put the above alias in ~/.bashrc :

Then just run this command:

a2enmod php # like, a2enmod php7.4 

Now you can run the command setphp from your terminal.

There are two methods to switch php versions on the command line ubuntu.

Run each command one by one and input the number of the php version you want to change.

sudo update-alternatives --config php sudo update-alternatives --config phar sudo update-alternatives --config phar.phar sudo service apache2 restart 

If you want to change PHP 5.6 to 7.4 just run the below codes.

sudo a2dismod php5.6 sudo a2enmod php7.4 sudo service apache2 restart 

If you want to change from PHP 7.4 to 5.6 run the below codes.

sudo a2dismod php7.4 sudo a2enmod php5.6 sudo service apache2 restart 

Источник

Оцените статью