Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»How to Switch between Multiple PHP Version on Debian 11/10

    How to Switch between Multiple PHP Version on Debian 11/10

    RahulBy RahulJune 20, 20183 Mins ReadUpdated:February 17, 2022

    On your system, if you have installed multiple versions of PHP (eg PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1 and 5.6). PHP 7.2 is running as default PHP for Apache and CLI. For any requirement, you need to use PHP 5.6. Then you don’t need to remove PHP 7.2. You can simply switch your PHP version to default used for Apache and command line.

    We assume you have installed multiple PHP version on your Debian system. Now you need to switch the active PHP version for CLI and Apache2. This tutorial will help you to switch between multiple PHP versions for Apache server and CLI on Debian.

    • Read => How to Install PHP on Debian 11 via PPA
    • Read => How to Install PHP on Debian 10 via PPA

    We have installed PHP 8.1, PHP 7.4 and PHP 5.6 on our Debian system. You can use the same for other PHP versions by changes commands accordingly.

    Enable PHP 8.1 as Default Version

    You need to set PHP 8.1 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

    • For Apache:-
      Run the following command to disable Apache module for other PHP versions.

      sudo a2dismod php7.4 php5.6 
      

      Now, enable PHP 8.1 module in Apache server.

      sudo a2enmod php8.1 
      sudo service apache2 restart 
      
    • For Command Line Interface:-
      sudo update-alternatives --set php /usr/bin/php8.1 
      sudo update-alternatives --set phar /usr/bin/phar8.1 
      sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.1 
      sudo update-alternatives --set phpize /usr/bin/phpize8.1 
      sudo update-alternatives --set php-config /usr/bin/php-config8.1 
      

    Note – The phpize8.1 and php-config8.1 command is available in php8.1-dev package. This is more useful for compiling PHP modules using pecl.

    Enable PHP 7.4 as Default Version

    You need to set PHP 7.4 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

    • Apache:-
      Run the following commands to disable Apache module for other PHP versions

      sudo a2dismod php8.1 php5.6 
      

      Now, enable PHP 7.4 module in Apache server.

      sudo a2enmod php7.4 
      sudo service apache2 restart 
      
    • Command Line Interface:-
      sudo update-alternatives --set php /usr/bin/php7.4 
      sudo update-alternatives --set phar /usr/bin/phar7.4 
      sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4 
      sudo update-alternatives --set phpize /usr/bin/phpize7.4 
      sudo update-alternatives --set php-config /usr/bin/php-config7.4 
      

    Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.

    Enable PHP 5.6 as Default Version

    The developers of PHP are no longer supporting PHP 5.6. It is strongly recommended to upgrade to higher PHP version like 7.4 or 8.1. But still you can install and Use php 5.6 on your Debian system if your application required PHP 5.6. Let’s set PHP 5.6 as your default version for CLI and Apache2 both.

    • Apache:-
      Run the following commands to disable other PHP versions like (php 8.1, php7.4 etc) for Apache and command line.

      sudo a2dismod php7.4 php8.1 php8.0     
      

      Now, enable PHP 5.6 module in Apache server.

      sudo a2enmod php5.6 
      sudo service apache2 restart 
      
    • Command Line:-
      sudo update-alternatives --set php /usr/bin/php5.6 
      sudo update-alternatives --set phar /usr/bin/phar5.6 
      sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6  
      sudo update-alternatives --set phpize /usr/bin/phpize5.6 
      sudo update-alternatives --set php-config /usr/bin/php-config5.6 
      

    Note – The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.

    Conclusion

    In this tutorial, you have learned about switching PHP versions on Debian system. You can change the default PHP version without uninstalling the other versions.

    debian PHP
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install and Use Cordova on CentOS/RHEL 7/6/5
    Next Article How to Install Bower on Fedora

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    10 Comments

    1. Eric Sebasta on April 19, 2021 1:54 am

      This will break php. your php info is no longer going to function?

      Reply
    2. Abdul on March 3, 2021 5:24 am

      Many thanks for your helpful instructions and information.

      Reply
    3. Karla on August 6, 2020 9:02 pm

      This is a great post, Rahul.
      Thank you for sharing. Just the process I have been looking for after upgrading from 7.2 to 7.4, my php -v shows 7.4, but my phpinfo() is still using 7.2!

      Reply
      • Karla on August 6, 2020 9:38 pm

        Follow-up to above post: Do we need to run both Apache and command line or just either one will do?

        Reply
        • Rahul on November 23, 2020 5:24 pm

          Apache instruction will change PHP for web server only. And command line instruction will change for command line only.

          So change php version as per your server requirements.

          Reply
          • florian on December 19, 2020 12:06 pm

            hi there and thanks a lot, just after applying update-alternatives my custom php app is not working on phpinfo() ; ?> I don’t see anymore a PHP output.

            Any solution to that?

            Reply
          • florian on December 19, 2020 5:20 pm

            hi there and thanks a lot, just after applying update-alternatives my custom php app is not working on phpinfo() ; ?> I don’t see anymore a PHP output. Debian 9, on alternatives do we change the path?

            Any solution to that?

            Reply
    4. Carlos on August 1, 2020 8:33 pm

      Thank you very much.

      Reply
    5. Jimmy on May 25, 2020 9:49 pm

      Thanks, great tutorial. I needed to change the CLI PHP version without forcing the change to affect Apache / Nginx

      Reply
    6. Rüdiger on December 13, 2018 7:14 pm

      Thank you very much, Rahul, this article is most helpful. In fact, it cured at least one of my headaches.
      Best regards,
      Rüdiger

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.