我已经在我的Ubuntu 16.04上安装了php 5.6和php 7.1
我知道用Apache作为我的网络服务器,我可以做到
a2enmod php5.6 #to enable php5
a2enmod php7.1 #to enable php7
当我在Apache模块中禁用php7.1并启用php 5.6时,Apache识别出了这个变化,并按预期使用php 5.6解释器。
但是当我从命令行运行内部php web服务器时:
php -S localhost:8888
PHP使用PHP 7处理请求。那么如何在命令行中在php 5.6和php 7.1之间切换呢?
在ubuntu 20.04中切换PHP 8.0和PHP 7.4版本:
将PHP 8.0降级为PHP 7.4
sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart
将PHP 7.4升级到PHP 8.0
sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart
检查更改:
在控制台中运行PHP -v,你会得到:
PHP 8.0.3 (cli)(已构建:march 5 2021 07:54:13) (NTS)
版权所有(c) PHP集团
Zend Engine v4.0.3,版权所有(c) Zend Technologies
与Zend OPcache v8.0.3,版权(c),由Zend Technologies
OR
PHP 7.4.16 (cli)(已构建:march 5 2021 07:54:38) (NTS)
版权所有(c) PHP集团
Zend Engine v3.4.0,版权所有(c) Zend Technologies
与Zend OPcache v7.4.16,版权(c),由Zend Technologies
通过添加phpinfo()在可运行的本地环境中添加一个PHP文件,如/var/www/html/ path;并在浏览器中获取PHP信息(在页面顶部可以看到PHP版本)
我做了一个bash脚本在Ubuntu上的不同PHP版本之间切换。
希望它能帮助到别人。
剧本如下:
(保存在/usr/local/bin/sphp.sh,不要忘记用命令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!"