Switch php versions on commandline ubuntu 16.04

前端 未结 16 1129
别那么骄傲
别那么骄傲 2020-11-30 16:53

I have installed php 5.6 and and php 7.1 on my Ubuntu 16.04

I know with Apache as my web server, I can do

a2enmod php5.6 #to enable php5
a2enmod php7.1         


        
相关标签:
16条回答
  • 2020-11-30 17:18

    You can use below command lines to switch between two PHP version.

    E.g.

    I want to switch PHP Version from 7.1 to 7.2 we can use below command

    sudo a2dismod php7.1 &&  sudo update-alternatives --set php /usr/bin/php7.2 && sudo a2enmod php7.2 && sudo service apache2 restart
    

    a2dismod is use to disable the current php version and a2enmod is use to enable the version

    0 讨论(0)
  • 2020-11-30 17:19

    I made a bash script to switch between different PHP versions on Ubuntu.

    Hope it helps someone.

    Here's the script: (save it in /usr/local/bin/sphp.sh, don't forget to add +x flag with command: sudo chmod +x /usr/local/bin/sphp.sh)

    #!/bin/bash
    
    # Usage
    if [ $# -ne 1 ]; then
      echo "Usage: sphp [phpversion]"
      echo "Example: sphp 7.2"
      exit 1
    fi
    
    currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
    newversion="$1"
    
    majorOld=${currentversion:0:1}
    minorOld=${currentversion:1:1}
    majorNew=${newversion:0:1}
    minorNew=${newversion:2:1}
    
    if [ $? -eq 0 ]; then
      if [ "${newversion}" == "${currentversion}" ]; then
        echo "PHP version [${newversion}] is already being used"
        exit 1
      fi
    
      echo "PHP version [$newversion] found"
      echo "Switching from [php${currentversion}] to [php${newversion}] ... "
    
      printf "a2dismod php$majorOld.$minorOld ... "
      sudo a2dismod "php${majorOld}.${minorOld}"
      printf "[OK] and "
    
      printf "a2enmod php${newversion} ... "
      sudo a2enmod "php${majorNew}.${minorNew}"
      printf "[OK]\n"
    
      printf "update-alternatives ... "
      sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}"
      printf "[OK]\n"
    
      sudo service apache2 restart
      printf "[OK] apache2 restarted\n"
    else
      echo "PHP version $majorNew.$minorNew was not found."
      echo "Try \`sudo apt install php@${newversion}\` first."
      exit 1
    fi
    
    echo "DONE!"

    0 讨论(0)
  • 2020-11-30 17:22

    type this in your command line, should work for all ubuntu between 16.04, 18.04 and 20.04.

    $ sudo update-alternatives --config php
    

    and this is what you will get

    There are 4 choices for the alternative php (providing /usr/bin/php).
    
      Selection    Path             Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/php7.2   72        auto mode
      1            /usr/bin/php5.6   56        manual mode
      2            /usr/bin/php7.0   70        manual mode
      3            /usr/bin/php7.1   71        manual mode
      4            /usr/bin/php7.2   72        manual mode
    Press <enter> to keep the current choice[*], or type selection number:
    

    Choose the appropriate version

    0 讨论(0)
  • 2020-11-30 17:23

    Interactive switching mode

    sudo update-alternatives --config php

    Manual Switching

    From PHP 5.6 => PHP 7.1

    Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.

    Apache:

    $ sudo a2dismod php5.6
    $ sudo a2enmod php7.1
    $ sudo service apache2 restart
    

    Command Line:

    $ sudo update-alternatives --set php /usr/bin/php7.1
    

    From PHP 7.1 => PHP 5.6

    Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.

    Apache:

    $ sudo a2dismod php7.1
    $ sudo a2enmod php5.6
    $ sudo service apache2 restart
    

    Command Line:

    $ sudo update-alternatives --set php /usr/bin/php5.6
    

    Source

    0 讨论(0)
  • 2020-11-30 17:24

    I actually wouldn't recommend using a2enmod for php 5 or 7. I would use update-alternatives. You can do sudo update-alternatives --config php to set which system wide version of PHP you want to use. This makes your command line and apache versions work the same. You can read more about update-alternatives on the man page.

    0 讨论(0)
  • 2020-11-30 17:27

    You can use the below script to switch between PHP version easily I have included phpize configuration too.

    https://github.com/anilkumararumulla/switch-php-version

    Download the script file and run

    sh switch.sh
    
    0 讨论(0)
提交回复
热议问题