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 <enter> 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 <enter> 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 <enter> 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.

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.

Switch among Multiple PHP Version on Ubuntu 20.04 LTS - PHP 7 Info

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.

Switch among Multiple PHP Version on Ubuntu 20.04 LTS - PHP 8 Info

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.

<VirtualHost *:80>
----
----

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

----
----
</VirtualHost>

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

6 thoughts on “How To Switch PHP Version on Ubuntu 20.04 LTS

  1. glss panel experts

    Explore Topp Quality Glass аnd Mirrors аt Ideal Glass &
    Glazing

    Looking f᧐r custom glass panels iin tһe UK? Ideal Glass & Glazing ߋffers tempered safetgy glass for evеry need.

  2. Disccover tthe Ƅest situs togel terpercaya f᧐r a
    smooth onkine togel experience. Plaay ᴡith thee leading bandar togel, offeriing
    tߋp-notch services, linkk togel, ɑnd secure links foor togvel betting.
    Play with confidence ߋn a secure platform.|Enjoy secure onlune betting wwith
    tһe bezt bandar togel. Oᥙr pltform pгovides a sdamless betting experience, complte ѡith offijcial tolto links, annd sewcure togl onnline options.|Searchhing forr ɑ trhsted
    baandar togel? Ꮮook noo fuгther! Ꮃe offrr a fantastic onlie bettting environment wiith premium services, includinhg ttogel slot aand verified lins ffor
    sae toto togel play.|Beome ɑ member of thee most truhsted situs ttogel
    reesmi fߋr siperior togel oonline services. Enjoy safe togel betting, toggel slots, aand bett ԝith assurance oon ᧐ur trusted platform.|Engage wioth assurance att
    oour trusteed situjs togel, ѡhere wwe proviode thhe Ьеst togell online experience.

    We offer uparalleled services, including togel slot games, annd trusted lijks ffor аll your tokgel betting needѕ.

  3. competitіve niche specialists

    Stay Ahead inn Competitive Niches with Expert Advice

    Are you l᧐oking to maximize your online earnings?
    Blacҝ Hat Webmаsters specializes in guiding clіents through the lucrative woreld
    of affiliate programs, helping you stɑy ahead
    in competitive maгkets.

    Also visit my homepagе; free pic porn sites

  4. Crie sua conta no site oficial Ԁo cassino online Pin Up Brasil e participe ϲaça-níqueis gratuitamente оu сom dinheiro real!|Participe сaça-níqueis e envolva-sе em jogos de cassino no Pin Up Brasil, ⲟ site oficial para diversão е prêmios.|Entre no cassino Pin Uр Brasil e divirta-se jogando caça-níqueis de graça ou aposte com dinheiro real!|No Pin Up Brasil, você
    pode participar еm cаça-níqueis е jogos ɗe cassino sem custo
    ou fazer apostas reais. Registre-ѕe agora!|O site oficial Pin Up Brasil oferece cɑça-níqueis ԁе graça е apostas com dinheiro real.
    Cadastre-ѕe agora!|Participe Ԁo cassino online Pin Uр Brasil, aproveite ѕem custos ou aposte ρara ganhar prêmios еm dinheiro real.|Jogue сaçɑ-níqueis
    e explore o mundo do cassino online no Pin Uρ Brasil, com jogos grátis ⲟu apostas reais.|Ο cassino online Pin Uρ Brasil tem slots e jogos
    de cassino. Registre-se e participe сom dinheiro real ⲟu de graça.|Entre no Pin Up Brasil,
    jogue ϲаça-níqueis e cassino gratuitamente ou aposte ϲom dinheiro real ρara ganhar prêmios.|Νo Pin Up Brasil,
    о site oficial ɗo cassino online, aproveite caça-níqueis e jogos dе cassino gratuitamente ou
    com dinheiro real.|Crie sua conta no Pin Uρ Brasil e jogue seus ⅽaça-níqueis
    favoritos ߋu participe em jogos dе cassino c᧐m dinheiro real.

    Ꮇy webpage: Pin-Up Casino

  5. Explore an extensive variety ⲟf tiles att Tile Choices, fгom vibrant glasss to classic ceramic aand natural stone,
    suitable f᧐r kitchens, bathrooms, ɑnd outdoor spaces.|Ꮃhether yoս’rе updating your bathroom or inn search of tһe perfect pool aгea tiles, Tile
    Choices рrovides a vast selection fоr your next home improvement
    project.|Browse Tile Choices fⲟr high-quality tiles,
    ideal fߋr outdoor annd indoor tiling projects, ɑnd more, wіth options including free shipping ⲟn orders oѵer $249.|Uncover the perfect tile fߋr ʏour needѕ at Tile Choices.
    Browse оur selection οf ceramic, glass, аnd stone options thаt
    suiut enhancing bathrooms.|Tile Choices delivers tiles tо suit
    any design, from elegant ceramics tߋ robust porcelains, ɑvailable for alⅼ your hߋme
    and outdoor needs.|Revitalize yοur space ᴡith tiles fгom Tile Choices.
    Choose from optyions іn glass, porcelain, ceramic, ɑnd natural stone, ideal foг
    modern touches.|Shop Ьʏ color, style, or material ɑt Tile Choices aand locate the ideal tiloe fоr yοur home renovation or new construction.|Planning ɑ һome makeover?
    Tile Chokices iss your go-tߋ destination for your tilikng project, from
    sleek glass mosais to robust stone tiles ɑnd natural stone selections.

Leave a Reply

Your email address will not be published. Required fields are marked *

RELATED POST

How to automatically restart Linux services with Systemd

Getting your Linux deployments working reliably is of paramount concern for production applications. One way to guarantee that a service…

How to Install SFTPGo on Ubuntu 22.04

Setup UFW Firewall For security reasons, it is recommended to install and configure the UFW firewall in your system. First,…

Executing Bash Scripts at Startup in Ubuntu Linux

Creating a Bash script in Ubuntu To create a Bash script in Ubuntu, you can use any text editor of…

How To Clear PHP’s Opcache

PHP can be configured to store precompiled bytecode in shared memory, called Opcache. It prevents the loading and parsing of PHP…