我已经在我的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之间切换呢?
我做了一个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!"